Personal tools

Parametric polymorphism

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
m (fixed typo; added clarification)
(Moved content to Polymorphism)
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
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.
+
#REDIRECT [[Polymorphism#Parametric polymorphism]]
 
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 <hask>map</hask> function has a parametrically polymorphic type
 
 
<haskell>
 
map :: (a -> b) -> [a] -> [b]
 
</haskell>
 
 
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 <hask>map</hask> highly polymorphic, yet there is still no risk of a runtime type mismatch.
 

Latest revision as of 21:39, 4 September 2012

  1. REDIRECT Polymorphism#Parametric polymorphism