My question is about the sequence function in Prelude, the signature of which is as follows:
sequence :: Monad m => [m a] ->
sequence acts as if it were defined like this.
sequence [] = return []
sequence (m:ms) = do
x <- m
xs <- sequence ms
return (x:xs)
(Or sequence = foldr (liftM2 (:)) (return []) but anyhow…)
Just think about what happens when applied to a list of lists.
sequence [] = [[]]
sequence (list : lists) =
[ x : xs
| x <- list
, xs <- sequence lists
]