9 ms·
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
by jes5199 14y ago
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.
- monkeyfacebag 14y agoIt's not intuitive, but it's straightforward once you understand the semantics. There are basically two cases. Case 1: 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 ...
- emillon 14y agodo a rest desugars to a >> do rest and do x <- a rest desugars to a >>= \x -> do rest