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@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@haskell.org
http://www.haskell.org/mailman/listinfo/beginners