Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type expr =
- | Constant of int
- | Variable of string
- | Add of expr * expr
- | Zero
- let rec simplify expr =
- let (c, expr) = simplify_acc expr in
- if c = 0 then expr else Add(Constant c, expr)
- and simplify_acc = function
- | Constant x -> (x, Zero)
- | Variable v -> (0, Variable v)
- | Add (l, r) -> simplify_add(simplify_acc l, simplify_acc r)
- | Zero -> (0, Zero)
- and simplify_add = function
- | ((_, Constant _), _) |(_, (_, Constant _)) -> failwith "no"
- | ((lc, Zero), (rc, v))
- | ((lc, v), (rc, Zero)) -> (lc + rc, v)
- | ((lc, l), (rc, r)) -> (lc + rc, Add(l, r))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement