You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

14 lines
388 B

  1. So how do our \hinline{sum}, \hinline{prod} and \hinline{length} functions look like if we use our \hinline{fold} abstraction?
  2. \pause
  3. \begin{haskellcode}
  4. sum :: [Int] -> Int
  5. sum xs = fold 0 (\x y -> x + y) xs
  6. -- a Haskeller would write
  7. sum = fold 0 (+)
  8. prod :: [Int] -> Int
  9. prod xs = fold 1 (\x y -> x * y) xs
  10. length :: [a] -> Int
  11. length xs = fold 0 (\x y -> 1 + y) xs
  12. \end{haskellcode}