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.  Question about example in 'using do notation with Writer'
      section of LYH (Olumide)
   2. Re:  Question about example in 'using do notation with
      Writer' section of LYH (David McBride)
   3. Re:  Question about example in 'using do notation with
      Writer' section of LYH (Imants Cekusins)
   4. Re:  Question about example in 'using do notation with
      Writer' section of LYH (Olumide)
   5. Re:  Question about example in 'using do notation with
      Writer' section of LYH (Olumide)
   6.  Semigroup Instances (Atrudyjane)


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

Message: 1
Date: Thu, 26 Jan 2017 15:41:35 +0000
From: Olumide <50...@web.de>
To: beginners@haskell.org
Subject: [Haskell-beginners] Question about example in 'using do
        notation with Writer' section of LYH
Message-ID: <41fadc2e-e456-b4fb-3943-ed896812c...@web.de>
Content-Type: text/plain; charset=utf-8; format=flowed

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


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

Message: 2
Date: Thu, 26 Jan 2017 11:02:44 -0500
From: David McBride <toa...@gmail.com>
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:
        <CAN+Tr41zT=r3d3mq-fqifzg+rcrztru649n2woktnkbnetq...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

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 <50...@web.de> 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
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

Message: 3
Date: Thu, 26 Jan 2017 17:10:55 +0100
From: Imants Cekusins <ima...@gmail.com>
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:
        <cap1qinytpuwklhqehfpdmh3s82+z6kfs2o_8sp4w6pxbr+4...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

> what becomes of the string parts of the Writer monads

multWithLog keeps [String] and outputs Int

to fully benefit from Writer [String] Int, you'd call e.g. runWriter
or execWriter

similar 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170126/4c9535b0/attachment-0001.html>

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

Message: 4
Date: Thu, 26 Jan 2017 18:00:08 +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: <23309531-2d21-c413-d3c3-dcd266ec2...@web.de>
Content-Type: text/plain; charset=utf-8; format=flowed

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




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

Message: 5
Date: Thu, 26 Jan 2017 21:34:42 +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: <a2273294-17b0-b628-53e2-912cf08ad...@web.de>
Content-Type: text/plain; charset=utf-8; format=flowed

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


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

Message: 6
Date: Thu, 26 Jan 2017 16:55:12 -0500
From: Atrudyjane <atrudyj...@protonmail.com>
To: "beginners@haskell.org" <beginners@haskell.org>
Subject: [Haskell-beginners] Semigroup Instances
Message-ID:
        
<82H33KLF09rxs-mmILyQ-aBsaKQFCiANhJ4FMZwOCjQ7AYiPtd3noxIcauSDVqrGs1xNbVMYNM_Vc5lrNgJPsN-mEe24my553xoFockn2kM=@protonmail.com>
        
Content-Type: text/plain; charset="utf-8"

I'm currently studying semigroups and trying to figure out how to determine 
which type variables need a semigroup instance. Here are a couple of examples 
from Evan Cameron's github 
(https://github.com/leshow/haskell-programming-book/blob/master/src/Ch15ex.hs):
(1)
data Validation a b
= Failure a
| Success b
deriving (Eq, Show)

instance Semigroup a => Semigroup (Validation a b) where
Success a <> Success b = Success a
Failure a <> Success b = Success b
Success a <> Failure b = Success a
Failure a <> Failure b = Failure (a <> b)

* Why doesn't 'b' need an instance of semigroup?
(2)
newtype AccumulateRight a b = AccumulateRight (Validation a b) deriving (Eq, 
Show)

instance Semigroup b => Semigroup (AccumulateRight a b) where
AccumulateRight (Success a) <>AccumulateRight (Failure b) =AccumulateRight 
(Success a)
AccumulateRight (Failure a) <>AccumulateRight (Success b) =AccumulateRight 
(Success b)
AccumulateRight (Failure a) <>AccumulateRight (Failure b) =AccumulateRight 
(Failure a)


AccumulateRight (Success a) <> AccumulateRight (Success b) = AccumulateRight 
(Success (a <> b))

* Why doesn't 'a' need an instance of semigroup?


Thank you,
Andrea





Sent with [ProtonMail](https://protonmail.com) Secure Email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170126/bd92bf53/attachment.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 103, Issue 21
******************************************

Reply via email to