3 ms·
From the article: The infix application function (ma>>=\a->f(a)), commonly called bind, executes the computation ma to expose its effects, calling the resultin
by nhaehnle 13y ago
From the article:
The infix application function (ma>>=\a->f(a)), commonly called bind, executes the computation ma to expose its effects, calling the resulting value a, and passes that to function f.
This is the kind of imprecise language that really made life extraordinarily difficult for me when I first learned about monads. I think this is an important point:
>>= does not execute ma!
If it did execute ma, that would violate the purity of the language.
Instead, >>= takes the computation ma and combines it with the function f to build a new, larger computation that is composed of smaller parts. The resulting computation (and ma) might never be executed (depending on the rest of the program).
- lostcolony 13y agoBefore learning about monads in Haskell, you probably should learn a bit of Haskell first. Most tutorials assume that. So the fact that function application is lazy is assumed as prior knowledge, since any approach to learning Haskell would cover that before monads.
- nhaehnle 13y agoThis has nothing to do with laziness, because laziness does not affect the semantics of a program that has bounded recursion. [0] The problem is that the quoted part of the article is written as if the >>= operator had side-effects (whether lazy or not), and that's just plain false. Now I agree that ordinarily, a student of Haskell has learned very early on that There Are No Side-Effects in Haskell, and should therefore not be confused. However, introductions to monads typically start out by stating that monads are how you can get side-effects in Haskell, and so they explicitly "deactivate" the No Side-Effects-assumption that students have. That's what causes the confusion. (In fact, the moment I finally understood monads was precisely when I realized that a useful way of thinking about it is that Haskell code with monads does not have side-effects after all. This is totally obvious in hindsight, but it seems that the best way to get this point across in teaching material has yet to be found.) [0] Obviously, this is only true in a side-effect-free language, but we're talking about Haskell here...
- rapala 13y agoThe semantics of >>= do not depend on the laziness of the language. >>= behaves exactly the same in Scala (although it's called flatMap there) as it does in Haskell. >>= does not execute effects, it only composes them. So to correct the article: The infix application function (ma>>=\a->f(a)), commonly called bind, composes two computations. When the resulting computation is executed, ma is executed first, f is called with its result and finally the computation returned by f is executed.
- Chattered 13y agoIndeed. Consider: Nothing >>= \a -> f a Here, there is no "a" and there never will be, and I could even annotate the type of "a" with something which can never be inhabited (such as Void).