Skip to content

Instantly share code, notes, and snippets.

@markhibberd
Created March 30, 2011 00:16
Show Gist options
  • Save markhibberd/893630 to your computer and use it in GitHub Desktop.
Save markhibberd/893630 to your computer and use it in GitHub Desktop.
Given common usage of map, then sequence, with the
following signatures.
> sequence :: (Monad m) => [m a] -> m [a]
> map :: (a -> b) -> [a] -> [b]
e.g.
> x :: (Monad m) => [m a]
> x = map readFile ["file1.txt", "file2.txt"]
> y :: (Monad m) => m [a]
> y = sequence x
You can take anything fitting this pattern and replace with a traverse (called mapM in the standard haskell library):
> traverse :: (Monad m) => (a -> m b) -> [a] -> m [b]
> traverse f as = sequence (map as)
e.g.
> z :: (Monad m) => m [a]
> z = traverse readFile ["file1.txt", "file2.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment