Skip to content

Instantly share code, notes, and snippets.

@ramntry
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save ramntry/dfc03e449f7e9ad75643 to your computer and use it in GitHub Desktop.

Select an option

Save ramntry/dfc03e449f7e9ad75643 to your computer and use it in GitHub Desktop.
Explicit polymorphism in OCaml
type 'value expression =
| Const of 'value
| IfThenElse of bool expression * 'value expression * 'value expression
let rec eval : 'value. 'value expression -> 'value = function
| Const value -> value
| IfThenElse (condition, then_expr, else_expr) ->
if eval condition
then eval then_expr
else eval else_expr
let test_expr =
IfThenElse (
IfThenElse (Const false, Const false, Const true),
Const "ok",
Const "error"
)
let () =
print_endline (eval test_expr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment