I know monads are supposed to be simple, but I've been playing with Haskell on and off for years now and I still can't figure out how do-notation desugars into >>= operations.
x <- monadicFunctionA
monadicFunctionB
is rewritten as: monadicFunctionA >>= \x -> monadicFunctionB
The important thing here is that x is just the variable name in an lambda expression that you don't see. Case 2: monadicFunctionA
monadicFunctionB
is rewritten as: monadicFunctionA >> monadicFunctionB
Where >> === >>= \_ ... . Given these rules, you can approximate do-notation like so: monadicFunctionA >>=
\x -> monadicFunctionB >>
monadicFunctionC ... do
a
rest
desugars to a >>
do
rest
and do
x <- a
rest
desugars to a >>= \x ->
do
rest