That is a result of the implementation of the specific Monad instance, and that does depend on the type, as you say (but it isn't determined for sequence(_) specifically).<div><br></div><div>Nothing >>= f = Nothing</div>
<div>Just x >>= f = f x </div><div><br></div><div>is why a Nothing "pollutes" the sequenced lists of Maybes. If Maybe is a Monad representing computations that can fail (to produce a result), then if you sequence a bunch of such computations together, if any one computation fails, your entire computation fails. This reflects the natural behavior of the Maybe monad, where if you use "x <- maybe computation", the only way to produce that x is if the computation returned Just.</div>
<div><br></div><div>In other monads, sequence will behave in the "right way" for that monad.<br><br><div class="gmail_quote">On Sat, Oct 30, 2010 at 1:30 AM, Mark Spezzano <span dir="ltr"><<a href="mailto:mark.spezzano@chariot.net.au">mark.spezzano@chariot.net.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Not exactly. If you use the type with Maybe Int like so:<br>
<br>
sequence [Just 1, Nothing, Just 2]<br>
<br>
then the result is Nothing.<br>
<br>
Whereas sequence [Just 1, Just 2, Just 3] gives<br>
<br>
Just [1, 2, 3]<br>
<br>
Why?<br>
<br>
I assume there's special implementations of sequence and sequence_ depending on the type of monad used. If it's a sequence_ [putStrLn "hello", putStrLn "goodbye"] then this prints out hello and goodbye on separate lines.<br>
<br>
It seems to work differently for different types.<br>
<font color="#888888"><br>
Mark<br>
</font><div><div></div><div class="h5"><br>
<br>
On 30/10/2010, at 3:42 PM, Bardur Arantsson wrote:<br>
<br>
> On 2010-10-30 07:07, Mark Spezzano wrote:<br>
>> Hi,<br>
>><br>
>> Can somebody please explain exactly how the monad functions "sequence" and "sequence_" are meant to work?<br>
>><br>
>> I have almost every Haskell textbook, but there's surprisingly little information in them about the two functions.<br>
>><br>
>> From what I can gather, "sequence" and "sequence_" behave differently depending on the types of the Monads that they are processing. Is this correct? Some concrete examples would be really helpful.<br>
>><br>
><br>
> sequence [m1,m2,m3,m4,...] = do<br>
> x1 <- m1<br>
> x2 <- m2<br>
> x3 <- m3<br>
> x4 <- m4<br>
> ...<br>
> return [x1,x2,x3,x4,...]<br>
><br>
> sequence_ [m1,m2,m3,m4,...] = do<br>
> m1<br>
> m2<br>
> m3<br>
> m4<br>
> ...<br>
> return ()<br>
><br>
> Cheers,<br>
><br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
><br>
><br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br></div>