[Haskell-beginners] Fwd: My first functioning haskell project - a steganography utility

Brent Yorgey byorgey at seas.upenn.edu
Tue Jul 13 10:51:08 EDT 2010


On Tue, Jul 13, 2010 at 03:39:50PM +0100, Tim Cowlishaw wrote:
> On 13 Jul 2010, at 15:31, edgar klerks wrote:
> 
> > You can use maybe from Data.Maybe: b -> (a -> b) -> Maybe a -> b
> > 
> > to create your maybe plus function: 
> > 
> > maybePlus x y = maybe 0 id $ liftM2 (+) x y

Note that 'maybe foo id' is better written 'fromMaybe foo'.


> maybeMonoid :: (a -> a -> a) -> a ->  (Maybe a -> Maybe a -> a)
> maybeMonoid f identity = maybe identity id $ liftM2 f

If it really is an instance of the Monoid type class then you could
just write:

  maybeMonoid :: (Monoid a) => Maybe a -> Maybe a -> a
  maybeMonoid x y = fromMaybe mempty $ liftM2 mappend x y


-Brent


More information about the Beginners mailing list