Last active
May 3, 2018 12:52
-
-
Save ethagnawl/744e01a832cd77de9a83da9047a8e18d to your computer and use it in GitHub Desktop.
ad hoc vs. parametric polymorphism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- The key difference between parametric polymorphism and overloading (aka ad-hoc | |
-- polymorphism) is that parameteric polymorphic functions use one algorithm to | |
-- operate on arguments of many different types, whereas overloaded functions may | |
-- use a different algorithm for each type of argument. | |
-- John Mitchell, Concepts in Programming Languages | |
-- http://stackoverflow.com/a/13043262/382982 | |
-- ad hoc/constrained => typeclasses | |
plus :: (Num a) => a -> a -> a | |
plus a b = (+) a b | |
-- parametric => generics | |
swap :: (A, B) -> (B, A) | |
swap (a, b) = (b, a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment