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.
 
 
 

21 lines
827 B

  1. There is also a function that folds from the \emph{left} which is also in the \emph{Prelude} and called \textbf{foldl}.\\
  2. To summarize:
  3. \begin{haskellcode}
  4. foldr f z [a,b,c] == a `f` (b `f` (c `f` z))
  5. foldl f z [a,b,c] == ((z `f` a) `f` b) `f` c
  6. \end{haskellcode}
  7. For \code{foldl} the \code{z} is sort of the starting value.
  8. \vspace{\baselineskip}
  9. \\
  10. \pause
  11. We can even express foldl in terms of foldr and vice versa. If you are interested, have a look here:\\ \url{http://lambda.jstolarek.com/2012/07/expressing-foldl-in-terms-of-foldr/}
  12. \vspace{\baselineskip}
  13. \\
  14. You should definitely look them up in the Prelude and play with them: \url{https://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html}
  15. \vspace{\baselineskip}
  16. \\
  17. GHCi...
  18. \begin{haskellcode}
  19. > foldr (-) 0 [1, 2, 3]
  20. > foldl (-) 0 [1, 2, 3]
  21. \end{haskellcode}