
30 Oct
2010
30 Oct
'10
6:07 a.m.
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.