Proposal: Add First and Last wrappers around Maybe to Data.Monoid

David House dmhouse at
Tue Mar 6 14:09:11 EST 2007

On 06/03/07, Ross Paterson <ross at> wrote:
> There is another, combining two Justs using a Monoid instance on the
> argument.  It could be argued that this is an even better candidate
> for the "obvious" instance on Maybe.

Like this?

instance Monoid a => Monoid (Maybe a) where
  mempty = Nothing
  Just xs `mappend` Just ys = Just (xs `mappend` ys)
  _ `mappend` _ = Nothing

I was under the impression that such an instance was already in the
libraries, but looking at the Haddocks it appears I'm wrong. Indeed,
one could write a generic instance:

instance (Functor f, FunctorZero f, Monoid a) => Monoid (f a) where
  mempty = fzero
  mappend = fmap mappend

Assuming a:

class FunctorZero f where fzero :: f a

-David House, dmhouse at

More information about the Libraries mailing list