[Haskell-beginners] Is my understanding of ">>=" correct?
Martin.Drautzburg at web.de
Tue Feb 19 20:13:21 CET 2013
It took me quite some time to understand >>=. What confused me, was that the
second operand is a function, not just a Monad. I believe I am beginning to
see the light.
First there is no way to sequence operations by just writing one after the
other. However we can chain operations by passing the output of one operation
to the next. But this does not not allow capturing the intermediate results.
With >>= the output of one operation gets magically bound to the parameter of
the function and remains in scope all the way through.
I was confused by the signature m a -> (a -> mb) -> mb because it looks like
there is (as second parameter) a function which returns a Monad. But I doubt I
ever saw such a function, in the sense that it does something intelligent. It
is typically a function that does not do a whole lot with its parameter, but
just returns another Monad, such that its parameter is bound to the result of
the previous expression.
But it could be done:
x <- SomeMonad
y <- someCleverlyConstructedMonad x
If I want to see the function of the >>= signature I have to read the do block
right->left (following the arrow) and then diagonally to the lower right. The
diagonal steps reveal the function. In the above example we have
\x -> someCleverlyConstructedMonad
but many times I just see
x <- SomeMonad
y <- someOtherMonad
and the function does not do anything intelligent but just binds x and
enforces the sequence of operations.
is this about right?
More information about the Beginners