Tutorial by Examples

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...
mconcat :: [a] -> a is another method of the Monoid typeclass: ghci> mconcat [Sum 1, Sum 2, Sum 3] Sum {getSum = 6} ghci> mconcat ["concat", "enate"] "concatenate" Its default definition is mconcat = foldr mappend mempty.
Numbers are monoidal in two ways: addition with 0 as the unit, and multiplication with 1 as the unit. Both are equally valid and useful in different circumstances. So rather than choose a preferred instance for numbers, there are two newtypes, Sum and Product to tag them for the different functional...
() is a Monoid. Since there is only one value of type (), there's only one thing mempty and mappend could do: instance Monoid () where mempty = () () `mappend` () = ()

Page 1 of 1