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

Soar with Haskell
By :

While we have so far been focusing on custom ADTs and functions that process them, there are actually other functions that do not care about the type of values they are processing. Such functions are (parametrically) polymorphic.
One of the most trivial functions is the identity function, which just returns its input. What should be the type of this function? It works on any possible type of input. Thus we could create many copies of this function, one for each type we want to use it with:
idInt :: Int -> Int idInt x = x idBool :: Bool -> Bool idBool x = x idChar :: Char -> Char idChar x = x
However, this leads to an unfortunate duplication of essentially the same logic. Moreover, our job is never done, as we would have to write another copy whenever we create a new ADT. Hence, instead, Haskell allows us to write a single generic definition that simultaneously works at all types:
Prelude
id :: a -> a
id x = x
...