|
|
|
|
|
by saidajigumi
4131 days ago
|
|
For me, this is the money quote: The monad instance defines what each kind of computation has to do with these continuations. A bind has two parameters: a closure and a continuation. x >>=(f1>>=(f2 >>=f3))
Serious light-bulb moment. I'm very used to thinking about and leveraging closures in imperative languages, but the above finally connected the idea that I can deliberately build abstractions/DSLs like this in Haskell for purposes such as action history manipulation. Computation as data! Love it! |
|
More complexly, the STM implementation for monad also uses it for backtracking; should something fail it can simply roll back all the function calls it has made and restart. It's not magic, it's a huge, huge pile of closures that the implementing type can use to do all sorts of things.
This is also one of the major reasons why good implementations of the monad typeclass in other languages often end up very painful to use; without some sort of friendly syntax that makes it incredibly easy to generate a new closure, you get a lot of "line noise" as you keep typing function (...) { ... function (...) { ... function (...) { ... function (...) {...
I'm still in the "I use it" camp personally, but I am sympathetic to the idea that you should start by manually writing out the bind calls on manually-written closures, and only go to the "do" syntax when you understand it thoroughly.
Oh, and to set your mind at ease: }}}}.