[Haskell-cafe] mapM_ -> Monoid.Monad.map

Thomas Davie tom.davie at gmail.com
Fri Jan 23 15:58:18 EST 2009

On 23 Jan 2009, at 21:50, Henning Thielemann wrote:

> I always considered the monad functions with names ending on '_' a  
> concession to the IO monad. Would you need them for any other monad  
> than IO? For self-written monads you would certainly use a monoid  
> instead of monadic action, all returning (), but IO is a monad. (You  
> could however wrap (newtype Output = Output (IO ())) and define a  
> Monoid instance on Output.)
> However our recent Monoid discussion made me think about mapM_,  
> sequence_, and friends. I think they could be useful for many monads  
> if they would have the type:
> mapM_ :: (Monoid b) => (a -> m b) -> [a] -> m b
>  I expect that the Monoid instance of () would yield the same  
> efficiency as todays mapM_ and it is also safer since it connects  
> the monadic result types of the atomic and the sequenced actions.  
> There was a recent discussion on the topic:
> http://neilmitchell.blogspot.com/2008/12/mapm-mapm-and-monadic-statements.html

Of note btw, these don't need Monad at all...

sequence :: Applicative f => [f a] -> f [a]
sequence = foldr (liftA2 (:)) (pure [])

mapA :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)
mapA = (fmap . fmap) sequence fmap


More information about the Haskell-Cafe mailing list