Some functions can't be written unless an element is in a type class. For instance, by default, there is no "==" function for a new type:
dayComment :: DayOfWeek -> String dayComment day = if day == Monday then "Ugh." else if day == Friday then "Hurray!" else "..." ERROR Instance of Eq DayOfWeek required for definition of dayComment
So we have to "overload" the "==" operator, and that requires us to make DayOfWeek an instance of the Eq class:
instance Eq DayOfWeek where
Sunday == Sunday = True
Monday == Monday = True
Tuesday == Tuesday = True
Wednesday == Wednesday = True
Thursday == Thursday = True
Friday == Friday = True
Saturday == Saturday = True
_ == _ = FalseNow we can use any function, like elemIndex (which returns the index of the element in a list) on our DayOfWeek type:
Prelude>:t elemIndex elemIndex :: Eq a => a -> [a] -> Maybe Int Prelude> elemIndex Wednesday [Sunday .. Saturday] Just 3 :: Maybe Int