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

Ben Millwood haskell at benmachine.co.uk
Sat Oct 30 06:07:02 EDT 2010


The actual, entire, complete definitions of sequence and sequence_ are
(or at least, could be):

> sequence [] = return []
> sequence (m:ms) = do
>   x <- m
>   xs <- sequence ms
>   return (x:xs)
>
> -- or, equivalently:
> sequence' = foldr (liftM2 (:)) (return [])
>
> sequence_ [] = return ()
> sequence_ (x:xs) = do
>   x
>   sequence_ xs
>
> -- or:
> sequence'_ = foldr (>>) (return ())

They're defined once for all monads, not once for each monad, so in
some sense they behave the 'same' in that they use the Monad instance
in the same way.

It's just like, say, sort :: Ord a => [a] -> [a] might do different
computations to compare elements depending on what 'a' is, but always
produces a sorted list regardless of that detail.


More information about the Haskell-Cafe mailing list