início

Haskell Monoids

You’ve seen that <> syntax and noticed it is imported from Data.Monoid?

I’ve always thought <> was a pretty complex mathematical function and it was very odd that people were using it for Text values, like "whatever " <> textValue <> " end.".

It turns out Text is a Monoid. That means it implements the Monoid class (or typeclass), that means it has a particular way of being concatenated. Any list could be a Monoid, any abstraction you can think of for which it makes sense to concatenate could be a Monoid, and it would use the same <> syntax. What exactly <> would do with that value when concatenating depends on its typeclass implementation of Monoid.

We can assume, for example, that Text implements Monoid by just joining the text bytes, and now we can use <> without getting puzzled about it.

Links to this page

This article on Nostr

naddr1qqyrzcmrvcmrqwtpqyghwumn8ghj7enfv96x5ctx9e3k7mgzyqalp33lewf5vdq847t6te0wvnags0gs0mu72kz8938tn24wlfze6qcyqqq823cktq992