Skip to content

Instantly share code, notes, and snippets.

@codebje
Last active October 26, 2015 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebje/775825fa87325f8346e4 to your computer and use it in GitHub Desktop.
Save codebje/775825fa87325f8346e4 to your computer and use it in GitHub Desktop.
Monoid with zero element
module Mzero where
import Data.Monoid
class Monoid a => MonoidZ a where
zero :: a
mconcat :: (Eq a) => [a] -> a
mconcat = go
where
go [] = mempty
go (x:xs) | x == zero = zero
| otherwise = x `mappend` go xs
instance Num a => MonoidZ (Product a) where
zero = 0
instance MonoidZ All where
zero = All False
instance MonoidZ Any where
zero = Any True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment