playground/ocaml/monads.ml

17 lines
331 B
OCaml

(* https://cs3110.github.io/textbook/chapters/ds/monads.html *)
module type Monad = sig
type 'a t
val return: 'a -> 'a t
val (>>=): 'a t -> ('a -> 'b t) -> 'b t
end
module Maybe : Monad = struct
type 'a t = 'a option
let return a = Some a
let (>>=) a f =
match a with
| None -> None
| Some a' -> f a'
end