wiki:FunctionalProgramming/Monoid

Monoid

Types with an associative (meaning ordering does not matter) binary (meaning of two values) operation (a combinator) that has an identity, which sometimes also called 'the unit'. (applying it changes nothing).

Identity for Addition is 0, for Multiplication it is 1. For concatenation of Lists it is [], for String concatenation it is "", etc.

Basically, identity is the notion of emptiness or nothingness in a given particular context. (Multiplication is a bit tricky - 1 is at least how many times). Usually units are the base-cases of recursion (of a recursive process).

Monoid laws

Identity

x <> mempty = x
mempty <> x = x

Semigroup

x <> (y <> z) = (x <> y) <> z

Connection to Reality

There is no Monoids outside one's head. Wait! There is one...

The image you see in a mirror is an identity. An arrow to yourself.

you <> mirror = you
mirror <> you = you

But there is a problem with an associative binary operation... The one we all know is, strictly speaking, not associative.

Haskell

To be a Monoid is to implement mappend and mempty which conform to Monoid laws.

class Semigroup a => Monoid a where
        mempty  :: a

        mappend :: a -> a -> a
        mappend = (<>)

See also Semigroup

Last modified 23 months ago Last modified on May 21, 2018, 9:36:44 AM
Note: See TracWiki for help on using the wiki.