My learnings about monads and state monads

I wrote about monads recently here: Simple introduction to monads in OCaml

and state monads here: State monads in OCaml

thought this might be useful to someone trying to learn these as well

3 Likes

I see you do toplevel-open in your code, which is generally only good for namespaced modules not identifiers.
A more idiomatic way to access definitions is to refer to them directly by their fully-qualified names, or do a local-open.

open Mod

let f x = a ...
let g x = b ...
let h x = c ...

becomes

let f x = Mod.a ...
let g x =
  let open Mod in
  b ...
let h x = Mod.(c ...)

so for example:

let res = Monad.(
  let* a = Some 5 in
  let* b = Some 6 in
  return (a + b)
)
2 Likes