haskell-lectures/VL2/content/VL2_polymorphism1.tex

20 lines
1.1 KiB
TeX

\ifger{Haskell eignet sich besonders gut für Abstraktion, aber wieso eigentlich? Haben wir etwas ähnliches wie java generics oder C++ templates?}{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
\ifger{Noch besser sogar! Haskell erlaubt standardmäßig Polymorphie, ohne komplizierte APIs oder Spracherweiterungen.}{Even better! Haskell supports polymorphism for both data types and functions, out of the box without any language extensions.}\\
\ifger{Wir beginnen mit einem polymorphen Datentyp:}{Let's start with a polymorphic data type:}
\begin{haskellcode}
data List t = Empty | Cons t (List t)
\end{haskellcode}
\ifger{Wir haben soeben unsere eigene singly-linked Liste implementiert. Für jeden Typ! Ein paar Beispiele:}{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}