O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The Monoid class is defined as follows:

    class Monoid a where      mempty :: a      mappend :: a -> a -> a      mconcat :: [a] -> a

The mempty function defines a default value. The mappend function defines that as a result of combining two values of a, we will get a single value of type amconcat indicates that we can combine all the values in the list to produce a single value of type aTo define a Monoid instance, we need to define at least mempty and mappend functions: 

Laws of Monoid--The monoid instance should follow this law: 

  • mappend x mempty = x  (Appending the default value should not change the value)

  • mappend mempty x = x (Appending a value to the default value is the same as value)

  • mappend x (mappend y z) = mappend (mappend ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required