Haskell/Solutions/Monoids

From Wikibooks, open books for an open world
< Haskell‎ | Solutions
Jump to: navigation, search

Exercises[edit]

1. Answer:

instance Monoid Integer where
  mempty = 1
  mappend = (*)

2. Answer:

mempty = mconcat []
mappend a b = mconcat [a, b]

Stated in terms of mconcat, the laws become:

mconcat [mconcat [], x] = x
mconcat [x, mconcat []] = x
mconcat [x, mconcat [y, z]] = mconcat [mconcat [x, y], z]

For instance, they do hold for concat, sum and product, which amount to mconcat for the instances we met thus far.