2015-04-20 18:55:41 +00:00
|
|
|
So how do our \hinline{sum}, \hinline{prod} and \hinline{length} functions look like if we use our \hinline{fold} abstraction?
|
2015-04-19 22:32:50 +00:00
|
|
|
\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}
|