Excerpts from Paolo Donadeo's message of Mon Jul 21 23:28:36 +0200 2008:
> I'm disappointed with myself and my incredibly low IQ. Late this
> evening I decided -- and this is the third time -- to be enlightened
> by the concept of monad.
>
> I like functional programming, but monads [1] must be too little to be
> grabbed by my mind. This time the interest in monads was aroused by
> the interesting article of David Teller, Arnaud Spiwack and Till
> Varoquaux [2] about the error monad, but for using the library they
> wrote I need at least some knowledge about monads and the do-notation.
>
> I tried with some tutorials found around, but I still cannot catch the
> point: what the hell is a monad?
Two key points that helped me:
* Monads help to separate some plumbing from your code.
* Monads provide a way to abstract code over some "let" construct.
I will note that specific "let" construct "let!", it's somewhat
like the do-notation but more atomic.
Monads also come with "return", but that's not the essence of them.
Think about that example:
val div : int -> int -> int option
val square : int -> option
let f x y =
match div x y with
| None -> None (* here 'y' was equal to 0 *)
| Some z ->
match square z with
| None -> None
| Some x2 -> Some x2
In the previous example the plumbing is error handling (where errors are
represented by None), and the "let!" construct is:
let! x = e1 in e2 ===> match e1 with None -> None | Some x -> e2
And "return" is "Some".
Another similar example:
type ('a,'b) either = Left of 'a | Right of 'b
val div : int -> int -> (string, int) either
val square : int -> (string, int) either
let f x y =
match div x y with
| Left error_msg -> Left error_msg
| Right z ->
match square z with
| Left error_msg -> Left error_msg
| Right x2 -> Right x2
Here the plumbing is still there for "error handling", but is somewhat
different. The "let!" construct is:
let! x = e1 in e2 ===> match e1 with Left m -> Left m | Right x -> e2
And "return" is "Right".
Finally one could have used the "let!" construct
abstractly (and also "return").
let f x y =
let! z = div x y in
let! x2 = square z in
return x2
One can obtain the two previous versions by choosing
which "let!"/"return" we want, namely choosing the monad.
Hope that helps,
--
Nicolas Pouillard aka Ertai