Semigroup and monoid

Semigroup and Monoid are closely related type classes for types which can be combined. The semigroup operator must be associative.

class Semigroup a where
    (<>) :: a -> a -> a
    
class Semigroup a => Monoid a where
    mempty :: a

The Monoid has a mempty value which is used as an identity. This means x <> mempty = x and mempty <> x = x.

Semigroup and monoid functions

stimes takes a semigroup value and applies it to itself a given number of times. stimes 3 "no" = "nonono".

mconcat takes a list of semigroup values and combines them all, using mempty for the empty list.

foldMap applies a function to each element in a foldable structure and then combines them all the semigroup operator.