[Haskell-cafe] Re: Lists and monads
wren ng thornton
wren at freegeek.org
Tue Jul 27 12:02:46 EDT 2010
Kevin Jardine wrote:
> But as I said, that is just an example. I keep wanting to apply the
> usual list tools but find that they do not work inside a monad. I find
> myself wishing that f (m [a]) just automatically returned m f([a])
Are you looking for these?
import Data.Traversable as T
T.sequence :: (T.Traversable t, Monad m) => t (m a) -> m (t a)
T.mapM :: (T.Traversable t, Monad m) => (a -> m b) -> t a -> m (t b)
-- N.B. T.mapM ~ (T.sequence . fmap)
> without me needing to do anything but I expect that there are reasons
> why that is not a good idea.
Not all functors can be distributed over arbitrary monads, so "not a
good idea" is more like "not always possible" or "here be dragons"
(fluffy, intriguing, and pretty dragons to be sure; but probably deeper
than the answer you were looking for).
In addition to Applicative, the Traversable and Foldable classes should
be key tools in your toolbox. They take a number of functions typically
restricted to lists and generalize them to different functors, often
with Applicatives or Monads involved. The Typeclassopedia should have
more on them.
More information about the Haskell-Cafe