haskell-lectures/VL2/content/VL2_fold3.tex

14 righe
521 B
TeX

\ifger{Jetzt wollen wir uns unsere Funktionen}{So how do our} \hinline{sum}, \hinline{prod} \ifger{und}{and} \hinline{length} \ifger{anschauen, wenn wir sie mittels \hinline{fold} implementieren:}{functions look like if we use our \hinline{fold} abstraction?}
\pause
\begin{haskellcode}
sum :: [Int] -> Int
sum xs = fold (\x y -> x + y) 0 xs
-- a Haskeller would write
sum = fold (+) 0
prod :: [Int] -> Int
prod xs = fold (\x y -> x * y) 1 xs
length :: [a] -> Int
length xs = fold (\x y -> 1 + y) 0 xs
\end{haskellcode}