Skip to content

Instantly share code, notes, and snippets.

@camilopayan
Created March 10, 2010 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camilopayan/327882 to your computer and use it in GitHub Desktop.
Save camilopayan/327882 to your computer and use it in GitHub Desktop.
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