June 2nd, 2015
In Technology
No Comments
If you enjoy this article, see the other most popular articles
If you enjoy this article, see the other most popular articles
If you enjoy this article, see the other most popular articles
Working with monoids
(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.
Simply put, Monoids describe types containing a binary function and an identity value.
When applied to the identity value and a random value x, said function leaves its argument x untouched, returning it as a result.
This short description should be enough to get the conversation started.Here’s how Haskell defines a Monoid:
class Monoid m where
mempty :: m
mappend :: m -> m -> m
mconcat :: [m] -> m
mconcat ms = foldr mappend mempty msThis type introduces three new functions so let’s walk through each one of them:
mempty – I started with a lie since mempty isn’t actually a function. You can think of it as a constant of the same type of the Monoid m. It is this monoid’s identity value.
mappend – A poorly named function, mappend is the binary function I mentioned earlier. It receives two arguments of type m and returns a value of type m
mconcat – It receives a list of Monoids m and reduces them to a single Monoid of type m. What’s interesting about this snippet is that the Monoid type class provides a default implementation for mconcat: it simply calls foldr with the binary function mappend, a starting value of mempty and the list of Monoid values ms
Post external references
- 1
http://www.leonardoborges.com/writings/2012/12/05/monads-in-small-bites-part-iii-monoids/
February 8, 2022 9:33 am
From Michael S on How I recovered from Lyme Disease: I fasted for two weeks, no food, just water
"Did you have Bartonella, too? Seems it uses autogenesis..."