[Haskell-cafe] IO () and IO [()]
jules at jellybean.co.uk
Wed Mar 12 03:03:02 EDT 2008
Henning Thielemann wrote:
> On Mon, 10 Mar 2008, Neil Mitchell wrote:
>>> I would like to know if in fact there's any difference in practice
>>> between (), [()], i.e. if in practice the difference matters.
>> Usually, not so much. A lot of Monad functions have _ variants, i.e.
>> mapM and mapM_. If you don't need the result, use the mapM_ version,
>> as it will run faster and not space/stack leak in some circumstances.
> In my opinion, mapM_ and sequence_ are in the wrong class, because they
> do not need much of Monads, or even Functors. They could well live, say,
> in Data.Monoid class. However, it's hard to integrate that in a
> hierarchy of type classes.
> instance Monoid a => Monoid (M a) where
> mempty = return mempty
> mappend = liftM2 mappend
> where M is a monad type.
Surely you mean to say:
instance Monad m => Monoid (m ()) where
mempty = return ()
mappend = (>>)
That is the instance which is consistent with your text "don't need much
of monads". Then sequence_ becomes mconcat, and mapM_ becomes foldMap
(from Data.Foldable), or more directly mconcat $ map ...
See also Control.Applicative, for things which can be sequence_'ed or
even sequence'd without being Monads.
More information about the Haskell-Cafe