Member-only story

Explaining Monoids to the 10 years old me

Michele Riva
5 min readMay 13, 2021

A Monoid is a set that is closed under an associative binary operation and has an identity element. Wait, what? Let’s explain this to the 10 years old me!

Photo by Ashim D’Silva on Unsplash

Some years ago, I started to code using the functional paradigm in Node.js and Elixir. When you try to switch from a non-pure functional language to a pure one (such as Haskell), you can find some scary concepts like Functors (we talked about them previously) and Monoids.

What if I tell you that you’re already using them? What if I tell you that you’ve always used them without knowing it? Let’s see what the heck is a Monoid.

Monoid Axioms

Let’s do a bit of maths using the + operation:

5 + 5 = 10
(10 + 5) + 15 = (5 + 15) + 10
10 + 0 = 0 + 10

We can immediately notice three things:

  • The result will always be the same; it doesn’t matter the order in which we’re doing the sum operation. We can try to sum (10 + 5) + 15 and (5 + 15) + 10But we’ll always get the same result.
  • There is a case where adding a number; the result won’t change at all. 10 + 0 returns just 10.
  • Let’s look at the first equation: there is a kind of binary operation that takes two things of type int and returns an element of the same type (int).

--

--

Responses (1)