3 ms·
> they usually define identity elements for all primitive functions > using reduce with non-primitive functions can result in domain errors ml-style folds in t
by 0x69420 3y ago
> they usually define identity elements for all primitive functions
> using reduce with non-primitive functions can result in domain errors
ml-style folds in the presence of ad-hoc polymorphism solve this rather handily -- in haskell for instance monoid is the typeclass that only requires an associative operation and an identity element
typeclasses have some clunkiness in this regard; you have to wrap numeric types as "sum" or "product" etc to go "ah yes today i want to say numbers are a monoid under this operation" but at the very least it does enable formal, user-defined associations between identity elements and functions
luckily most things programmers deal with are plausibly just one kind of monoid. for instance the eleventy billion different string types haskell programmers love to use all tend to satisfy monoid under concatenation without any wrappers