Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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. Re:  Question about example in 'using do notation with
      Writer' section of LYH (Olumide)
   2.  Need help understanding the tell function in the Monad
      Writer example in LYAH (Olumide)


----------------------------------------------------------------------

Message: 1
Date: Fri, 3 Feb 2017 11:20:16 +0000
From: Olumide <50...@web.de>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Question about example in 'using do
        notation with Writer' section of LYH
Message-ID: <43a69956-d89e-492c-1a0d-c4659f7ee...@web.de>
Content-Type: text/plain; charset=utf-8; format=flowed

On 27/01/2017 08:34, Theodore Lief Gannon wrote:
> Fully expanding your program might help. One of the great things about
> Haskell is equational reasoning: if two things have been declared equal,
> you can substitute one for the other.
>
> First, let's desugar that do notation to the equivalent bind chain:
>
>     multWithLog =
>       logNumber 3 >>= \a ->
>         logNumber 5 >>= \b ->
>           return (a*b)
>
>
> Evaluate the logNumber and return calls to normal form from their
> definitions, also considering the monoid definitions of (++) and mempty
> for lists:
>
>     multWithLog =
>       Writer (3, ["Got number: 3"]) >>= \a ->
>         Writer (5, ["Got number: 5"]) >>= \b ->
>           Writer (a*b, [])
>
>
> Now, refer to the definition of (>>=) for Writer (as shown in LYAH):
>
>     (Writer (x, v)) >>= f = let (Writer (y, v')) = f x in Writer (y, v
>     `mappend` v')
>

Thank you for your explanation. The substitutions helped a lot even 
though I wasn't able to follow the equational reasoning. The expansion 
gave me enough starting information to figure out what's going on by 
starting from the innermost bind.

BTW, I thought the definition of (>>=) was supposed to come from 
Control.Monad.Writer.

Regards,

- Olumide




------------------------------

Message: 2
Date: Fri, 3 Feb 2017 11:31:32 +0000
From: Olumide <50...@web.de>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Need help understanding the tell function
        in the Monad Writer example in LYAH
Message-ID: <7195359e-cfb5-a3de-d473-76450f2d0...@web.de>
Content-Type: text/plain; charset=utf-8; format=flowed

Hello List,

Would someone kindly explain how the tell function interacts with the 
Writer Monad as show below:
(Example taken from chapter 14 of LYAH 
http://learnyouahaskell.com/for-a-few-monads-more#reader )

multWithLog :: Writer [String] Int
multWithLog = do
        a <- logNumber 3
        b <- logNumber 5
        tell ["Gonna multiply these two"]
        return (a*b)

Result:
     ghci> runWriter multWithLog
     (15,["Got number: 3","Got number: 5","Gonna multiply these two"])

I know that tell function binds to an argument that is discarded but I 
don't know how its argument "Gonna multiply these two" is concatenated 
with the other the Writer created by logNumber 3 and logNumber 5.

Also, I don't understand the paragraph following the example:

"It's important that return (a*b) is the last line, because the result 
of the last line in a do expression is the result of the whole do 
expression. Had we put tell as the last line, () would have been the 
result of this do expression. We'd lose the result of the 
multiplication. However, the log would be the same."


Regards,

- Olumide


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 104, Issue 1
*****************************************

Reply via email to