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

Tim Cowlishaw tim at timcowlishaw.co.uk
Tue Jul 13 10:39:50 EDT 2010


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
> 
> That is somewhat cleaner.

Aah thanks Edgar - I'd meant to ask about this specifically actually, as addition is a monoid over the real numbers and  therefore has an identity element, I was wondering if there was an easier way to generalise it to cope with arguments in the Maybe monad. As far as I can see, this is precisely what you describe above. Therefore, would I be right in saying that your approach can be generalised to any function which forms a monoid over a specific type?

I'm imagining something like:

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

Thanks for the feedback!

Cheers,

Tim



More information about the Beginners mailing list