haskell-lectures/VL2/content/VL2_map3.tex

16 lines
1.2 KiB
TeX

\ifger{Alle 3 Funktionen sind sehr ähnlich. In Haskell wollen wir vor allem abstrahieren und würden niemals die Funktionen derart schreiben. Stattdessen werden wir eine Funktion schreiben, die die Rekursionsstruktur aller 3 abstrakt zusammenfässt.}{All those 3 functions look almost the same. Since haskell is about abstraction, we would never really write any of those like that. Instead, we will write a function that generalizes all 3.}
\vspace{\baselineskip}
\\
\pause
\ifger{Ich gebe als Tipp die Typsignatur. Wie könnte die Implementierung aussehen?}{I'll give you the type signature, can you guess how the implementation looks like?}
\begin{haskellcode}
map :: (a -> b) -> [a] -> [b]
\end{haskellcode}
\ifger{Lösung?}{Solution?}
\pause
\begin{haskellcode}
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
\end{haskellcode}
\ifger{Wir wissen nicht was die Funktion \hinline{f} genau macht, aber wir wissen, dass sie ein Element der Liste zu etwas anderem konvertiert. Wir \emph{mappen} eine Funktion über eine Liste! Daher der Name.}{So we don't really know what the function \hinline{f} does, but we know that it converts one element of the list to something else. We \emph{map} a function over a list! Hence the name.}