[Haskell-cafe] Re: Monads and Functions sequence and sequence_

Mark Spezzano mark.spezzano at chariot.net.au
Sat Oct 30 01:30:06 EDT 2010


Not exactly. If you use the type with Maybe Int like so:

sequence [Just 1, Nothing, Just 2]

then the result is Nothing.

Whereas sequence [Just 1, Just 2, Just 3] gives

Just [1, 2, 3]

Why?

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.

It seems to work differently for different types.

Mark


On 30/10/2010, at 3:42 PM, Bardur Arantsson wrote:

> On 2010-10-30 07:07, Mark Spezzano wrote:
>> Hi,
>> 
>> Can somebody please explain exactly how the monad functions "sequence" and "sequence_" are meant to work?
>> 
>> I have almost every Haskell textbook, but there's surprisingly little information in them about the two functions.
>> 
>> 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.
>> 
> 
> sequence [m1,m2,m3,m4,...] = do
>  x1 <- m1
>  x2 <- m2
>  x3 <- m3
>  x4 <- m4
>  ...
>  return [x1,x2,x3,x4,...]
> 
> sequence_ [m1,m2,m3,m4,...] = do
>  m1
>  m2
>  m3
>  m4
>  ...
>  return ()
> 
> Cheers,
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 



More information about the Haskell-Cafe mailing list