
13 Jul
2010
13 Jul
'10
10:51 a.m.
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