DEV Community


Discussion on: Monoids (and semigroups)

sshine profile image
Simon Shine

This Semigroup instance breaks the semigroup laws, since

λ> mempty <> mojito
Cocktail {name = " Mojito", ingredients = ["rum","mint"]}

λ> mojito <> mempty
Cocktail {name = "Mojito ", ingredients = ["rum","mint"]}

If you want to fix this, consider making this a commutative monoid:

λ> (mojito <> robroy) == (robroy <> mojito)

...although I think sufficiently advanced drinks are not commutative because of chemistry.

samhh profile image
Sam A. Horvath-Hunt Author

You're right, I cheated a little bit there!

sshine profile image
Simon Shine • Edited

I don't think that cheating is a thing.

Also, thanks for making my new go-to example of a monoid a cocktail.

I have been using soup and tango for a while, and people don't seem to relate.

Another thought on the monoidal properties of cocktails: You may want a multiset / bag rather than a set of ingredients, since an important property of a cocktail is how many parts of each ingredient go into it.

I ended up writing a bit about it here. :-D

Forem Open with the Forem app