-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Soar with Haskell
By :

Many applications of type classes can be found in libraries rather than in application-specific code. While applications usually feature concrete types for a specific use case, libraries typically aim to cover as many use cases as they can. For that reason, they prefer regular polymorphic types where possible, or constraint polymorphic types when needed. We have seen various examples of the former before. Here, we will cover a number of basic library functions with constraint-polymorphic type signatures.
The first three functions we consider can be found in the automatically imported Prelude library. They are all related to the presence of a particular element in a list. Firstly, elem
checks whether an element appears in a list:
Prelude
elem :: Eq a => [a] -> a -> Bool
elem [] y = False
elem (x:xs) y = x == y || elem xs y
This returns True
when the element is...