By looking at sequence implementation in Hugs:<div><br><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">sequence (c:cs) = do x  &lt;- c</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-tab-span" style="white-space:pre">                </span>     xs &lt;- sequence cs</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, 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="&#39;courier new&#39;, monospace">  sequence [Just 2, Nothing]</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">= do x &lt;- Just 2</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">     y &lt;- sequence [Nothing]</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  return (x:y)</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, 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">&lt;<a href="mailto:celtic@sairyx.org">celtic@sairyx.org</a>&gt;</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>
&gt; mapM id [Just 1, Just 2, Just 3]<br>
&gt; result: Just [1,2,3]<br>
<br>
&gt;<br>
</div><div class="im">&gt; mapM :: (a -&gt; m b) -&gt; [a] -&gt; m [b]<br>
&gt; So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2,<br>
&gt; Just 3] and b = Int and m = Maybe. So id is :: Maybe Int -&gt; Maybe Int<br>
<br>
</div>Right! So note here that &#39;m&#39; is Maybe and &#39;b&#39; is &#39;Int&#39;, thus mapM&#39;s<br>
return value is &#39;m [b]&#39;, i.e. &#39;Maybe [Int]&#39;. The implication is that it<br>
somehow yields a Maybe of [Int], but no Maybe Int.<br>
<div class="im"><br>
&gt; mapM id [Just 1, Nothing, Just 3]<br>
&gt; result: Nothing.<br>
&gt; 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&#39;s<br>
definition:<br>
<div class="im"><br>
&gt; mapM f as  =  sequence (map f as)<br>
<br>
</div>So the list is mapped onto the (monadic!) function, then sequenced:<br>
<br>
&gt; sequence :: Monad m =&gt; [m a] -&gt; m [a]<br>
&gt; sequence = foldr mcons (return [])<br>
&gt;   where<br>
&gt;     mcons p q = p &gt;&gt;= \x -&gt; q &gt;&gt;= \y -&gt; 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>
&gt; Just 1 &gt;&gt; Just 2<br>
Just 2<br>
&gt; Nothing &gt;&gt; Just 2<br>
Nothing<br>
&gt;<br>
<br>
This falls simply out of Maybe&#39;s Monad instance definition for bind:<br>
<br>
&gt; instance Monad Maybe where<br>
&gt;   (Just x) &gt;&gt;= k   =  k x<br>
&gt;   Nothing  &gt;&gt;= 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>