4 ms·
Monoids (& monads) are one of those tools that I think help shape your thinking (like FP) and, even if you don’t use them daily or regularly... still help make
by abritinthebay 9y ago
Monoids (& monads) are one of those tools that I think help shape your thinking (like FP) and, even if you don’t use them daily or regularly... still help make you a better programmer to understand.
The concepts are actually very simple but their effects are complex. This is great introduction though I think a little high level for beginners.
- umanwizard 9y agoWhy monoids? Why not groups? Why not vector spaces? I can't tell why monoids are supposed to be more directly applicable to programming than any other concept from algebra.
- Koshkin 9y agoThey are all applicable, it's just that the notion of monoid is more general and thus its area of applicability is bigger. [Edit] On the other hand, it is important to realize that even though these structures look (and indeed are) related, each focuses on a completely different aspect of reality. Monoids are useful in describing composability (where associativity is essential); groups are instrumental in analyzing all kinds of symmetries; vector spaces form a scene on which one deals with linear independence, subspaces, and linear transformations. (All this finds its application in computational algorithms, but it would be interesting to see if the program structure also could be understood using the notion of symmetry and/or linearity.)
- tome 9y agoI'll assume this a genuine question and give a genuine answer. > Why not groups? Because many things you would want to combine don't have inverses. Does the list [1,2,3] have an inverse? No. That's why lists form a monoid and not a group. > Why not vector spaces? Because all finite dimensional real vector spaces are isomorphic to R^n so we tend just to use the concrete instances of tuples of floating point numbers.
- chas 9y agoAssociative operators with an identity are extremely common in programming, inverses are much less common. Most ways to turn a lot of data into a little data have a monoid hidden in them with the identity serving as a default value in the case of missing data. There is nothing you can comcatenate to a list to get the empty list, nothing you can OR with True to get False, and nothing you can union with a set to get the empty set.
- abritinthebay 9y agoGeneral application vs specifics, mostly. But yes, a good grounding in algebra will help anyway. It’s not an exclusive set ;)
- tikhonj 9y agoIntuitively, it's because lots of things we work with in CS don't have a good notion of inverse. In practice, this usually means you're building up a structure out of smaller structures: you can always add more to the structure, but there's nothing you can add to undo what was added previously. Semirings are interesting for the same reason. "Fun with Semirings"[1] has some great examples, if you're curious. [1]: https://pdfs.semanticscholar.org/702d/348c32133997e992db362a19697d5607ab32.pdf https://pdfs.semanticscholar.org/702d/348c32133997e992db362a...