haskell-lectures/VL2/content/VL2_rec_patterns4.tex

16 строки
689 B
TeX

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
I'll give you the type signature, can you guess how the implementation looks like?
\begin{haskellcode}
map :: (a -> b) -> [a] -> [b]
\end{haskellcode}
Solution?
\pause
\begin{haskellcode}
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
\end{haskellcode}
So we don't really know what the function \code{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.