Question 4-2

Define class as it relates to the Haskell programming language.

Solution

A class is a set of functions that any type that is a member (instance) of the class must define. For example, we might define a Measurable class:
class Measurable a where
    size :: a -> Integer
Now, if we have a type that we want to include in this class, we can declare it as an instance. The following declares lists of any type of data to be in the Measurable class.
instance Measurable [a] where
    size [] = 0
    size (x:xs) = 1 + size xs

Back to Review for Quiz 4