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

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


On 06/03/07, Ross Paterson <ross at soi.city.ac.uk> 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 gmail.com


More information about the Libraries mailing list