Improve fold slides
This commit is contained in:
parent
a16fa8e5ae
commit
049802ff92
@ -1,14 +1,14 @@
|
|||||||
\ifger{Um es kurz zu machen, die abstrakte Lösung ist:}{To cut the story short, the abstract solution looks like this:}
|
\ifger{Um es kurz zu machen, die abstrakte Lösung ist:}{To cut the story short, the abstract solution looks like this:}
|
||||||
\begin{haskellcode}
|
\begin{haskellcode}
|
||||||
fold :: b -> (a -> b -> b) -> [a] -> b
|
fold :: (a -> b -> b) -> b -> [a] -> b
|
||||||
fold z f [] = z
|
fold f z [] = z
|
||||||
fold z f (x:xs) = x `f` (fold z f xs)
|
fold f z (x:xs) = x `f` (fold f z xs)
|
||||||
\end{haskellcode}
|
\end{haskellcode}
|
||||||
Whoa! What's going on here?\\
|
Whoa! What's going on here?\\
|
||||||
\ifger{Schauen wir genauer hin...}{Let's see...}
|
\ifger{Schauen wir genauer hin...}{Let's see...}
|
||||||
\begin{itemizep}
|
\begin{itemizep}
|
||||||
\item \hinline{z} \ifger{ist was die Funktion zurückgibt, wenn die Liste leer ist}{is what we return if the list is empty}
|
|
||||||
\item \hinline{f} \ifger{ist unsere Funktion}{is our function} (\ifger{z.b.}{e.g.} \hinline{(*)} \ifger{oder}{or} \hinline{(+)})
|
\item \hinline{f} \ifger{ist unsere Funktion}{is our function} (\ifger{z.b.}{e.g.} \hinline{(*)} \ifger{oder}{or} \hinline{(+)})
|
||||||
|
\item \hinline{z} \ifger{ist was die Funktion zurückgibt, wenn die Liste leer ist}{is what we return if the list is empty}
|
||||||
\item \ifger{das letzte Argument ist die eigentliche Liste, auf der wir arbeiten}{and the last remaining argument is the actual list we are working on}
|
\item \ifger{das letzte Argument ist die eigentliche Liste, auf der wir arbeiten}{and the last remaining argument is the actual list we are working on}
|
||||||
\end{itemizep}
|
\end{itemizep}
|
||||||
\slidep
|
\slidep
|
||||||
|
@ -2,13 +2,13 @@
|
|||||||
\pause
|
\pause
|
||||||
\begin{haskellcode}
|
\begin{haskellcode}
|
||||||
sum :: [Int] -> Int
|
sum :: [Int] -> Int
|
||||||
sum xs = fold 0 (\x y -> x + y) xs
|
sum xs = fold (\x y -> x + y) 0 xs
|
||||||
-- a Haskeller would write
|
-- a Haskeller would write
|
||||||
sum = fold 0 (+)
|
sum = fold (+) 0
|
||||||
|
|
||||||
prod :: [Int] -> Int
|
prod :: [Int] -> Int
|
||||||
prod xs = fold 1 (\x y -> x * y) xs
|
prod xs = fold (\x y -> x * y) 1 xs
|
||||||
|
|
||||||
length :: [a] -> Int
|
length :: [a] -> Int
|
||||||
length xs = fold 0 (\x y -> 1 + y) xs
|
length xs = fold (\x y -> 1 + y) 0 xs
|
||||||
\end{haskellcode}
|
\end{haskellcode}
|
Loading…
Reference in New Issue
Block a user