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...

Type classes provide a very good abstraction for defining common behavior across data types. For example, the Eq type class is defined as follows:

    class Eq a where     (==) :: a -> a -> Bool     (/=) :: a -> a -> Bool

The preceding type class defines a set of behavior for type a. The behavior is a set of functions. The Eq class specifies two functions, equality (==) and non-equality (/=). Both functions take two arguments of type a and return Bool

The standard Haskell provides definition for both (==) and (/=) as follows:

    x == y = not (x /= y)  -- Note the definition of (==) by in-fix notation.    x /= y = not (x == y)

You can see that the behavior of equality is defined in terms of non-equality and vice versa. To be able to provide ...

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