haskell-lectures/VL2/content/VL2_fold4.tex

18 lines
1.5 KiB
TeX

\ifger{Es gibt auch eine Funktion die von \emph{links} faltet und ebenfalls in \emph{Prelude} ist. Sie heisst \textbf{foldl}.}{There is also a function that folds from the \emph{left} which is also in the \emph{Prelude} and called \textbf{foldl}.}\\
\ifger{Zusammenfassend:}{To summarize:}
\begin{haskellcode}
foldr f z [a,b,c] == a `f` (b `f` (c `f` z))
foldl f z [a,b,c] == ((z `f` a) `f` b) `f` c
\end{haskellcode}
\ifger{Für}{For} \hinline{foldl} \ifger{ist}{the} \hinline{z} \ifger{quasi der Startwert.}{is sort of the starting value.}
\vspace{\baselineskip}
\\
\pause
\ifger{Wir können sogar foldl mittels foldr ausdrücken und umgekehrt. Wer interessiert daran ist, sollte hier\footnote{\url{http://lambda.jstolarek.com/2012/07/expressing-foldl-in-terms-of-foldr}} weiterlesen.}{We can even express foldl in terms of foldr and vice versa. If you are interested, have a look here\footnote{\url{http://lambda.jstolarek.com/2012/07/expressing-foldl-in-terms-of-foldr}}.}
\vspace{\baselineskip}
\\
\ifger{Es macht Sinn diese Funktionen in \emph{Prelude}\footnote{\url{https://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html}} anzuschauen und ein bisschen damit herumzuspielen:}{You should definitely look them up in the \emph{Prelude}\footnote{\url{https://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html}} and play with them:}
\begin{haskellcode*}{bgcolor=mygrey,frame=single,numbers=none,label=GHCi}
> foldr (-) 0 [1, 2, 3]
> foldl (-) 0 [1, 2, 3]
\end{haskellcode*}