Re:Learn You a Haskell for Great Good (2008)
by dllthomas on Thursday February 19, 2015 @11:48AM
(#9075014)
"I still don't know what a Monoid is."
That is surprising. They show up all over in fairly common libraries - for instance, Writer collects a monoidal value. The very simplest notion is "monoid is for things you can combine". Taken from math, a monoid is a set of things (a type) for which you have an operation (called mappend in Haskell) which you can combine associatively, where the set contains an element (mempty in Haskell) that is an identity with respect to that operation.
Some common monoids are:
Anything list-like:
mappend appends two lists
mempty is the empty list
Integers over addition (newtype Sum)
mappend adds two integers
mempty is zero
Tuple of monoids:
mappend is componentwise mappend
mempty is (mempty, mempty)
Functions where the domain and range are the same:
mappend is function composition
mempty is id
That is surprising. They show up all over in fairly common libraries - for instance, Writer collects a monoidal value. The very simplest notion is "monoid is for things you can combine". Taken from math, a monoid is a set of things (a type) for which you have an operation (called mappend in Haskell) which you can combine associatively, where the set contains an element (mempty in Haskell) that is an identity with respect to that operation.
Some common monoids are: