Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Artem Lobanov (c) 2014
- Arithmetic expression simplifier
- *)
- type Expr =
- | Const of int
- | Var of string
- | Add of Expr * Expr
- | Sub of Expr * Expr
- | Mul of Expr * Expr
- | Div of Expr * Expr
- let rec calc expr =
- let calcable operand1 operand2 exprFunc arithmFunc =
- match (calc operand1, calc operand2) with
- | (Const aResult, Const bResult) -> Const (arithmFunc aResult bResult)
- | _ -> exprFunc (calc operand1, calc operand2)
- match expr with
- | Const a -> Const a
- | Var x -> Var x
- | Add (a, b) -> match (calc a, calc b) with
- | (Const 0, Var bResult) -> Var bResult
- | (Var aResult, Const 0) -> Var aResult
- | _ -> calcable a b Add (+)
- | Sub (a, b) -> match (calc a, calc b) with
- | (Var aResult, Var bResult) -> Const 0
- | _ -> calcable a b Add (-)
- | Mul (a, b) -> match (calc a, calc b) with
- | (Const 0, Var result) | (Var result, Const 0) -> Const 0
- | _ -> calcable a b Add (*)
- | Div (a, b) -> match (calc a, calc b) with
- | (Var aResult, Const 1) -> Var aResult
- | _ -> calcable a b Add (/)
- printfn "%A" (calc(Sub(Const 0, Var "x")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement