By looking at sequence implementation in Hugs:<div><br><div><div><font class="Apple-style-span" face="'courier new', monospace">sequence (c:cs) = do x <- c</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">                </span> xs <- sequence cs</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-tab-span" style="white-space:pre">                </span> return (x:xs)</font></div><div><br></div><div>Apply it against a list [Just 2, Nothing], we will have:</div>
<div><font class="Apple-style-span" face="'courier new', monospace"> sequence [Just 2, Nothing]</font></div><div><font class="Apple-style-span" face="'courier new', monospace">= do x <- Just 2</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> y <- sequence [Nothing]</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> return (x:y)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">= return (2:Nothing)</font></div><div><br></div><div>The question is</div><div>Why/How `return (2:Nothing)` eval to `Nothing` ?</div><div><br></div>
-Haisheng<br>
<br><br><div class="gmail_quote">On Sat, May 21, 2011 at 8:25 AM, Arlen Cuss <span dir="ltr"><<a href="mailto:celtic@sairyx.org">celtic@sairyx.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
<br>
> mapM id [Just 1, Just 2, Just 3]<br>
> result: Just [1,2,3]<br>
<br>
><br>
</div><div class="im">> mapM :: (a -> m b) -> [a] -> m [b]<br>
> So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2,<br>
> Just 3] and b = Int and m = Maybe. So id is :: Maybe Int -> Maybe Int<br>
<br>
</div>Right! So note here that 'm' is Maybe and 'b' is 'Int', thus mapM's<br>
return value is 'm [b]', i.e. 'Maybe [Int]'. The implication is that it<br>
somehow yields a Maybe of [Int], but no Maybe Int.<br>
<div class="im"><br>
> mapM id [Just 1, Nothing, Just 3]<br>
> result: Nothing.<br>
> My first guess for the result: Just [Just 1, Nothing, Just 3]<br>
<br>
</div>This is contingent of the semantics of the Maybe monad. First, mapM's<br>
definition:<br>
<div class="im"><br>
> mapM f as = sequence (map f as)<br>
<br>
</div>So the list is mapped onto the (monadic!) function, then sequenced:<br>
<br>
> sequence :: Monad m => [m a] -> m [a]<br>
> sequence = foldr mcons (return [])<br>
> where<br>
> mcons p q = p >>= \x -> q >>= \y -> return (x : y)<br>
<br>
Note that consecutive values are bound, so seeing this example should<br>
clarify why a single Nothing causes mapM to return Nothing for the lot:<br>
<br>
> Just 1 >> Just 2<br>
Just 2<br>
> Nothing >> Just 2<br>
Nothing<br>
><br>
<br>
This falls simply out of Maybe's Monad instance definition for bind:<br>
<br>
> instance Monad Maybe where<br>
> (Just x) >>= k = k x<br>
> Nothing >>= k = Nothing<br>
<br>
HTH.<br>
<font color="#888888"><br>
Arlen<br>
</font><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div></div>