haskell-lectures/VL2/content/VL2_fold1.tex

25 wiersze
808 B
TeX
Czysty Zwykły widok Historia

2015-04-23 20:57:25 +00:00
\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}
2015-04-23 20:57:25 +00:00
\ifger{Lösung?}{Solution?}
\pause
\begin{haskellcode}
sum :: [Int] -> Int
2015-05-01 14:42:06 +00:00
sum [] = 0
sum (x:xs) = x + sum xs
\end{haskellcode}
\pause
2015-04-23 20:57:25 +00:00
\ifger{Oder das Produkt:}{Or the product:}
\begin{haskellcode}
prod :: [Int] -> Int
2015-05-01 14:42:06 +00:00
prod [] = 1
prod (x:xs) = x * prod xs
\end{haskellcode}
\pause
2015-04-23 20:57:25 +00:00
\ifger{Oder die Länge:}{Or the length:}
\begin{haskellcode}
length :: [a] -> Int
2015-05-01 14:42:06 +00:00
length [] = 0
length (x:xs) = 1 + length xs
\end{haskellcode}