[Haskell-beginners] Is my understanding of ">>=" correct?
Sylvain HENRY
hsyl20 at gmail.com
Fri Feb 22 11:34:02 CET 2013
Hi,
I tried to explain Haskell IO in an introductory talk. Slides are
available here:
http://www.sylvain-henry.info/home/data/uploads/teaching/shenry-slides-haskell-intro-2013.pdf
You may find the "Real World Haskell" section interesting to understand
monads. I use explicit World variables to explain how operations are
sequenced. It helped me to think about it this way when I learned it.
Cheers
Sylvain
Le 19/02/2013 20:13, Martin Drautzburg a écrit :
> Hello all,
>
> 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:
>
> do
> 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
>
> do
> 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
mailing list