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.
 
 
 

19 lines
748 B

  1. To cut the story short, the abstract solution looks like this:
  2. \begin{haskellcode}
  3. fold :: b -> (a -> b -> b) -> [a] -> b
  4. fold z f [] = z
  5. fold z f (x:xs) = x `f` (fold z f xs)
  6. \end{haskellcode}
  7. Whoa! What's going on here?\\
  8. Let's see...
  9. \begin{itemizep}
  10. \item \hinline{z} is what we return if the list is empty
  11. \item \hinline{f} is our function (e.g. \hinline{(*)} or \hinline{(+)})
  12. \item and the last remaining argument is the actual list we are working on
  13. \end{itemizep}
  14. \slidep
  15. The function application has the following form:\\
  16. \hinline{fold f z [a,b,c] == a `f` (b `f` (c `f` z))}
  17. \vspace{\baselineskip}
  18. \\
  19. This folds from the right, so the \emph{Prelude} already defines a function which is very similar to ours and called \textbf{foldr}.