当我正在学习Haskell时,我意识到do符号只是合成糖:
a = do x <- [3..4]
[1..2]
return (x, 42)
Run Code Online (Sandbox Code Playgroud)
翻译成
a = [3..4] >>= (\x -> [1..2] >>= (\_ -> return (x, 42)))
Run Code Online (Sandbox Code Playgroud)
我意识到我可能会使用do-notation,但我想了解翻译中发生的事情.纯粹出于教学原因,ghc/ghci有没有办法给我一个用do-notation编写的相当复杂的monad的相应绑定语句?
编辑.事实证明#haskell上的lambdabot可以做到这一点:
<Guest61347> @undo do x <- [3..4] ; [1..2] ; return (x, 42)
<lambdabot> [3 .. 4] >>= \ x -> [1 .. 2] >> return (x, 42)
Run Code Online (Sandbox Code Playgroud)
这是Undo插件的源代码.