[Haskell-beginners] I don't understand mapM in mapM id (Just 1, Nothing, Just 3)

Haisheng Wu freizl at gmail.com
Mon Jun 6 03:08:40 CEST 2011


Thanks Senastian!

I would refine the equality as below:

  sequence [Just 2, Nothing]
= do x <- Just 2
     y <- sequence [Nothing]
  return (x:y)
= Just 2 >>= \x -> sequence [Nothing] >>= \y -> return (x:y)
= Nothing >>= \y -> return (2:y)
= Nothing


-Haisheng


On Mon, Jun 6, 2011 at 12:31 AM, Sebastian Hungerecker <
sepp2k at googlemail.com> wrote:

> On 05.06.2011 14:40, Haisheng Wu wrote:
>
>> By looking at sequence implementation in Hugs:
>>
>> sequence (c:cs) = do x<- c
>>      xs<- sequence cs
>>      return (x:xs)
>>
>> Apply it against a list [Just 2, Nothing], we will have:
>>   sequence [Just 2, Nothing]
>> = do x<- Just 2
>>      y<- sequence [Nothing]
>>   return (x:y)
>> = return (2:Nothing)
>>
>> The question is
>> Why/How `return (2:Nothing)` eval to `Nothing` ?
>>
>>
>
> It doesn't. You went wrong in the last equality there. y doesn't have
> the value Nothing, in fact it never gets a value at all.
> Let's first look at what sequence [Nothing] evaluates to:
>
> do x <- Nothing
>
>   y <- sequence [Nothing]
>   return (x:y)
>
> Since  Nothing >>= f  evaluates to Nothing this means that the above
> do-block evaluates to Nothing, too. So  y <- sequence [Nothing]  becomes
> y <- Nothing  and again the whole expression evaluates to Nothing and
> return (x:y)  never is evaluated.
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110606/c81ab4d2/attachment.htm>


More information about the Beginners mailing list