haskell-lectures/VL2/content/VL2_fold1.tex

25 řádky
808 B
TeX

\ifger{Es gibt noch eine weitere sehr wichtige Rekursionsstruktur. Stellen wir uns vor wir wollen alle Zahlen einer Liste aufsummieren. Die Typsignatur sieht wie folgt aus:}{There's one more important recursion pattern. Imagine you want the sum of all numbers of a list, so the function type signature would be:}
\begin{haskellcode}
sum :: [Int] -> Int
\end{haskellcode}
\ifger{Lösung?}{Solution?}
\pause
\begin{haskellcode}
sum :: [Int] -> Int
sum [] = 0
sum (x:xs) = x + sum xs
\end{haskellcode}
\pause
\ifger{Oder das Produkt:}{Or the product:}
\begin{haskellcode}
prod :: [Int] -> Int
prod [] = 1
prod (x:xs) = x * prod xs
\end{haskellcode}
\pause
\ifger{Oder die Länge:}{Or the length:}
\begin{haskellcode}
length :: [a] -> Int
length [] = 0
length (x:xs) = 1 + length xs
\end{haskellcode}