haskell-lectures/VL2/content/VL2_fold3.tex
Julian Ospald e9374aed1f
VL2: Restructure files, make the latex code more modular
Also added a few more section to make beamer and article more
compatible.
2015-04-20 18:06:58 +02:00

14 lines
376 B
TeX

So how do our \code{sum}, \code{prod} and \code{length} functions look like if we use our \code{fold} abstraction?
\pause
\begin{haskellcode}
sum :: [Int] -> Int
sum xs = fold 0 (\x y -> x + y) xs
-- a Haskeller would write
sum = fold 0 (+)
prod :: [Int] -> Int
prod xs = fold 1 (\x y -> x * y) xs
length :: [a] -> Int
length xs = fold 0 (\x y -> 1 + y) xs
\end{haskellcode}