haskell-lectures/VL2/content/VL2_polymorphism1.tex
2015-04-20 00:51:14 +02:00

20 lines
664 B
TeX

So when we said that haskell is good for abstraction, what did we actually mean? Do we have something like java generics or C++ templates?
\vspace{\baselineskip}
\\
\pause
Even better! Haskell supports polymorphism for both data types and functions.\\
Let's start with a polymorphic data type:
\begin{haskellcode}
data List t = Empty | Cons t (List t)
\end{haskellcode}
So we just implemented our own singly-linked List. For any type! Let's use it:
\pause
\begin{haskellcode}
intList :: List Int
intList = Cons 3 (Cons 5 (Cons 2 Empty))
charList :: List Char
charList = Cons 'x' (Cons 'y' (Cons 'z' Empty))
-- whatever you can imagine goes here
\end{haskellcode}