Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Is my understanding of ">>=" correct? (Martin Drautzburg) 2. Re: Is my understanding of ">>=" correct? (Bob Ippolito) ---------------------------------------------------------------------- Message: 1 Date: Tue, 19 Feb 2013 20:13:21 +0100 From: Martin Drautzburg <martin.drautzb...@web.de> Subject: [Haskell-beginners] Is my understanding of ">>=" correct? To: beginners@haskell.org Message-ID: <201302192013.21463.martin.drautzb...@web.de> Content-Type: text/plain; charset="us-ascii" 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? -- Martin ------------------------------ Message: 2 Date: Tue, 19 Feb 2013 12:15:54 -0800 From: Bob Ippolito <b...@redivi.com> Subject: Re: [Haskell-beginners] Is my understanding of ">>=" correct? To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Message-ID: <cacwmpm_bzz3bsl40im9b_rusxbrp8j3hvmyxhzoq3utxteo...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" On Tue, Feb 19, 2013 at 11:13 AM, Martin Drautzburg < martin.drautzb...@web.de> wrote: > 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. > Well, sequencing actions is a special case of chaining actions where you simply ignore the result of the previous action. This is the >> operator. It is defined as follows: m >> k = m >>= \_ -> k There is no magic involved, just functions and lexically scoped variables. 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. > I think it's important to note that the signature is a -> (a -> m b) -> m b. The space is significant. 'm' is a separate type variable from 'a' or 'b'. > 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? > It's not really possible to translate the above example into valid code because a do block must end with an expression. Let's say you were to change it to the following valid syntax: do x <- firstAction y <- secondAction x return (x, y) The de-sugaring of that do block would look like this (newlines added to show function scope): firstAction >>= \x -> secondAction x >>= \y -> return (x, y) A concrete example: do x <- getLine y <- print x return (x, y) h> getLine >>= \x -> print x >>= \y -> return (x, y) Hello! "Hello!" ("Hello!",()) do blocks are just syntax sugar, they don't do anything you couldn't easily do with 'let ... in', '>>=' and '->'. They just make it a lot easier to read in some cases. Other cases it's a wash, such as in this example where we don't want to capture any intermediate results: do x <- getLine print x is equivalent to: getLine >>= print -bob -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130219/3bf0cb36/attachment-0001.htm> ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 56, Issue 32 *****************************************