HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Parametric polymorphism

Parametric polymorphism is when a function's type signature allows various arguments to take on arbitrary types, but the types must be related to each other in some way.

For example, in Java one can write a function that accepts two arguments of any possible type. However, Haskell goes further by allowing a function to accept two arguments of any type so long as they are both the same type. For example

As a specific (and slightly more complicated) example, the well-known map function has a parametrically polymorphic type

map :: (a -> b) -> [a] -> [b]

which means that the function will accept any type of list and any type of function, provided the types match up. (That is, the function's argument type and the list's element type match.) This makes map highly polymorphic, yet there is still no risk of a runtime type mismatch.

Retrieved from "http://www.haskell.org/haskellwiki/Parametric_polymorphism"

This page has been accessed 988 times. This page was last modified 06:20, 21 May 2007. Recent content is available under a simple permissive license.