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

Jeffrey Yasskin jyasskin at gmail.com
Tue Mar 6 23:05:23 EST 2007


On 3/6/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.

Good point. I see two instances that satisfy the monoid laws here too:

-- | Lift a 'Monoid' into 'Maybe'
instance Monoid a => Monoid (Maybe a) where
  mempty = Nothing
  Nothing `mappend` m = m
  m `mappend` Nothing = m
  Just m1 `mappend` Just m2 = Just (m1 `mappend` m2)

instance Monoid a => Monoid (Maybe a) where
  mempty = Just mempty
  Just m1 `mappend` Just m2 = Just (m1 `mappend` m2)
  _ `mappend` _ = Nothing


David, your instance doesn't satisfy (mempty `mappend` r == r)

I think the first instance is more useful. If the list agrees, I'm
happy to add it into my patch.

Jeffrey


More information about the Libraries mailing list