-
-
Save gdsfh/40098b24ac26a24be1e63b52e3b0a7c3 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
$ ocaml -w A | |
OCaml version 4.02.3 | |
# type 'a mtram = Mdone of 'a | Mbind : 'b mtram * ('b -> 'a mtram) -> 'a mtram;; | |
type 'a mtram = Mdone of 'a | Mbind : 'b mtram * ('b -> 'a mtram) -> 'a mtram | |
# let rec eval : 'a . 'a mtram -> 'a = function | |
| Mdone x -> x | |
| Mbind (Mdone b, b_am) -> eval (b_am b) | |
| Mbind (Mbind (cm, c_bm), b_am) -> eval (Mbind (cm, fun c -> Mbind (c_bm c, b_am)));; | |
val eval : 'a mtram -> 'a = <fun> | |
# let return a = Mdone a;; | |
val return : 'a -> 'a mtram = <fun> | |
# let ( >>= ) mb b_ma = Mbind (mb, b_ma);; | |
val ( >>= ) : 'a mtram -> ('a -> 'b mtram) -> 'b mtram = <fun> | |
# let rec is_even = function 0 -> return true | n -> return (n - 1) >>= is_odd | |
and is_odd = function 0 -> return false | n -> return (n - 1) >>= is_even;; | |
val is_even : int -> bool mtram = <fun> | |
val is_odd : int -> bool mtram = <fun> | |
# let is_sum_even a b = | |
return a >>= is_even >>= fun res_a -> | |
return b >>= is_even >>= fun res_b -> | |
return (res_a = res_b);; | |
val is_sum_even : int -> int -> bool mtram = <fun> | |
# eval @@ is_sum_even 5 7;; | |
- : bool = true | |
# eval @@ is_sum_even 4 5;; | |
- : bool = false | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment