In Haskell, why is the infix alias of mappend
(from class Monoid
) <>
instead of +
? In algebra courses +
is usually used for the binary operator of a monoid.
The function +
is specific to numbers, and moreover, it's only one way to implement Monoid for numbers (*
is equally valid). Similarly, with booleans, it would be equally valid to use &&
and ||
. Using the symbol +
suggests that Monoids are about addition specifically, when really they're just about any associative operation.
It is true that, at least in my experience, one is likely to use mappend
in a fashion resembling addition: concatenating lists or vectors, taking unions of sets or maps, etc, etc. However, the Haskell mindset favors generality and adherence to mathematical principles over (arguably) what is more intuitive. It's certainly reasonable, in my opinion, to think of mappend
as a sort of general addition, and make adjustments in the cases where it isn't.
Partly due to the principle of least astonishment and partly because there are at least two sensible monoid instances for numbers (namely, Sum
and Product
from Data.Monoid
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With