Hi Tim,<br><br>I am not too deep into category theory. But the Data.Monoid class defines the identity as mempty. And the binary operation as mappend. So that would be:<br><br>maybePlus = maybe mempty id $ liftM2 (mappend) x y<br>
<br>You only have to define Num as Monoid, because there are more monoids possible. (Multiplication, addition etc).<br><br>That would look something like this:<br><br>instance (Num a) => Monoid a where ...<br><br>Have a look at the monoid class: <a href="http://haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-Monoid.html">http://haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-Monoid.html</a><br>
<br>Greets,<br><br>Edgar<br><div class="gmail_quote">On Tue, Jul 13, 2010 at 4:39 PM, Tim Cowlishaw <span dir="ltr"><<a href="mailto:tim@timcowlishaw.co.uk">tim@timcowlishaw.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On 13 Jul 2010, at 15:31, edgar klerks wrote:<br>
<br>
> You can use maybe from Data.Maybe: b -> (a -> b) -> Maybe a -> b<br>
><br>
> to create your maybe plus function:<br>
><br>
> maybePlus x y = maybe 0 id $ liftM2 (+) x y<br>
><br>
> That is somewhat cleaner.<br>
<br>
</div>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?<br>
<br>
I'm imagining something like:<br>
<br>
maybeMonoid :: (a -> a -> a) -> a -> (Maybe a -> Maybe a -> a)<br>
maybeMonoid f identity = maybe identity id $ liftM2 f<br>
<br>
Thanks for the feedback!<br>
<br>
Cheers,<br>
<font color="#888888"><br>
Tim<br>
<br>
</font></blockquote></div><br>