# Question about example in 'using do notation with Writer' section of LYH

7 messages
Open this post in threaded view
|

## Question about example in 'using do notation with Writer' section of LYH

 Hello List, Chapter 14 of LYH ( http://learnyouahaskell.com/for-a-few-monads-more#reader ) has the following bit of code: import Control.Monad.Writer logNumber :: Int -> Writer [String] Int logNumber x = Writer (x, ["Got number: " ++ show x]) multWithLog :: Writer [String] Int multWithLog = do      a <- logNumber 3      b <- logNumber 5      return (a*b) I have a fair grasp of what's going bu the last line return(a*b) eludes me. Specifically its the a*b part that baffles me. I think 3 is bound to 'a' and 5 to 'b', then return(a*b) would put the value 15 in a context ... So what becomes of the string parts of the Writer monads created by logNumber 3 and logNumber 5 respectively. Regards, - Olumide _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Question about example in 'using do notation with Writer' section of LYH

 Nothing happens to them.  They are still in there.  Whereas logNumber always returns the number you give it, multiWithLog always returns 15 and appends two strings into its state ("got number 3 and 5"). You can return whatever value you want from a Monad.  In this case he's doing a * b, but you could do 2 * b, Just (a + b) or anything else.  As an example, here I return a tuple instead. multWithLogTuple :: Writer [String] (Int,Int,Int) multWithLogTuple = do     a <- logNumber 3     b <- logNumber 5     return (a,b,2*b) >runWriter multWithLogTuple ((3,5,10),["Got number: 3","Got number: 5"]) On Thu, Jan 26, 2017 at 10:41 AM, Olumide <[hidden email]> wrote: > Hello List, > > Chapter 14 of LYH ( http://learnyouahaskell.com/for-a-few-monads-more#reader> ) has the following bit of code: > > > import Control.Monad.Writer > > logNumber :: Int -> Writer [String] Int > logNumber x = Writer (x, ["Got number: " ++ show x]) > > multWithLog :: Writer [String] Int > multWithLog = do >     a <- logNumber 3 >     b <- logNumber 5 >     return (a*b) > > I have a fair grasp of what's going bu the last line return(a*b) eludes me. > Specifically its the a*b part that baffles me. I think 3 is bound to 'a' and > 5 to 'b', then return(a*b) would put the value 15 in a context ... So what > becomes of the string parts of the Writer monads created by logNumber 3 and > logNumber 5 respectively. > > Regards, > > - Olumide > _______________________________________________ > Beginners mailing list > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Question about example in 'using do notation with Writer' section of LYH

 In reply to this post by Olumide > what becomes of the string parts of the Writer monadsmultWithLog keeps [String] and outputs Intto fully benefit from Writer [String] Int, you'd call e.g. runWriter or execWritersimilar to other State state out monads, m state out lets you get/set/keep state as needed without passing it as an arg. state is there if you need it. _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Question about example in 'using do notation with Writer' section of LYH

 In reply to this post by David McBride On 26/01/2017 16:02, David McBride wrote: > Nothing happens to them.  They are still in there.  Whereas logNumber > always returns the number you give it, multiWithLog always returns 15 > and appends two strings into its state ("got number 3 and 5"). > > You can return whatever value you want from a Monad.  In this case > he's doing a * b, but you could do 2 * b, Just (a + b) or anything > else.  As an example, here I return a tuple instead. > > multWithLogTuple :: Writer [String] (Int,Int,Int) > multWithLogTuple = do >     a <- logNumber 3 >     b <- logNumber 5 >     return (a,b,2*b) > So return (a*b) still creates Writer [String] Int? I thought the [String] parts of 'a' and 'b' ought to be in the result but I see it needn't. - Olumide _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Question about example in 'using do notation with Writer' section of LYH

 In reply to this post by David McBride On 26/01/17 16:02, David McBride wrote: >> runWriter multWithLogTuple > ((3,5,10),["Got number: 3","Got number: 5"]) On second thoughts I don't think I understand how the logs are concatenated. I was expecting (15,["Got number: 15") in the original example. - Olumide _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners