Choosing the non-empty Monoid

Posted by Nikita Volkov on Stack Overflow See other posts from Stack Overflow or by Nikita Volkov
Published on 2012-11-28T22:22:36Z Indexed on 2012/11/28 23:04 UTC
Read the original article Hit count: 247

Filed under:
|

I need a function which will choose a non-empty monoid. For a list this will mean the following behaviour:

> [1] `mor` []
[1]
> [1] `mor` [2]
[1]
> [] `mor` [2]
[2]

Now, I've actually implemented it but am wondering wether there exists some standard alternative, because it seems to be a kind of a common case. Unfortunately Hoogle doesn't help.

Here's my implementation:

mor :: (Eq a, Monoid a) => a -> a -> a
mor a b = if a /= mempty then a else b

© Stack Overflow or respective owner

Related posts about haskell

Related posts about monoids