Haskell Language Monoid An instance of Monoid for lists


Example

instance Monoid [a] where
    mempty  = []
    mappend = (++)

Checking the Monoid laws for this instance:

mempty `mappend` x = x   <->   [] ++ xs = xs  -- prepending an empty list is a no-op

x `mappend` mempty = x   <->   xs ++ [] = xs  -- appending an empty list is a no-op

x `mappend` (y `mappend` z) = (x `mappend` y) `mappend` z
    <->
xs ++ (ys ++ zs) = (xs ++ ys) ++ zs           -- appending lists is associative