[Haskell-beginners] Simplifying code

Patrick LeBoutillier patrick.leboutillier at gmail.com
Tue Feb 9 17:07:55 EST 2010


> Sure. If you don't mind that the mutations come in a different order, one
> thing that works wonders is "sequence",
> sequence :: Monad m => [m a] -> m [a]
> In particular, for m = [], sequence :: [[a]] -> [[a]]. Then, knowing what
> sequence does, we can write
> import Control.Monad (sequence)
> generateAll :: String -> [String]
> generateAll word = sequence (map f word)
>     where
>     f c = case lookup c leat of
>                Just r  -> [c,r]
>                Nothing -> [c]

That's very nice!

One question though: In the docs sequence is described as:

  "Evaluate each action in the sequence from left to right, and
collect the results."

How is one supposed to deduce what the behavior will be for the list
monad (besides looking at the source)?


Patrick LeBoutillier
Rosemère, Québec, Canada

More information about the Beginners mailing list