Created
March 10, 2010 13:59
-
-
Save camilopayan/327882 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Exp = | |
Num of int | |
| Neg of Exp | |
| Sum of Exp * Exp | |
| Diff of Exp * Exp | |
| Prod of Exp * Exp | |
| Quot of Exp * Exp | |
let rec evaluate = function | |
| Num n -> Some n | |
| Neg n -> match evaluate n with | |
| Some x -> Some (-x) | |
| _ -> None | |
| Sum (l,r) -> match (evaluate l, evaluate r) with | |
| (Some le, Some re) -> Some (le+re) | |
| _ -> None | |
| Diff (l,r) -> match (evaluate l, evaluate r) with | |
| (Some le,Some re) -> Some (le-re) | |
| _ -> None | |
| Prod (l,r) -> match (evaluate l, evaluate r) with | |
| (Some le, Some re) -> Some (le*re) | |
| _ -> None | |
| Quot (l,r) -> match (evaluate l, evaluate r) with | |
| (Some le, Some re) when re=0 -> None | |
| (Some le, Some re) -> Some (le/re) | |
| _ -> None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment