Browse Source

Improve fold slides

Julian Ospald 4 years ago
parent
commit
7a2c1a9708
No account linked to committer's email address
2 changed files with 8 additions and 8 deletions
  1. 4
    4
      VL2/content/VL2_fold2.tex
  2. 4
    4
      VL2/content/VL2_fold3.tex

+ 4
- 4
VL2/content/VL2_fold2.tex View File

@@ -1,14 +1,14 @@
1 1
 \ifger{Um es kurz zu machen, die abstrakte Lösung ist:}{To cut the story short, the abstract solution looks like this:}
2 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)
3
+fold :: (a -> b -> b) -> b -> [a] -> b
4
+fold f z []     = z
5
+fold f z (x:xs) = x `f` (fold f z xs)
6 6
 \end{haskellcode}
7 7
 Whoa! What's going on here?\\
8 8
 \ifger{Schauen wir genauer hin...}{Let's see...}
9 9
 \begin{itemizep}
10
-\item \hinline{z} \ifger{ist was die Funktion zurückgibt, wenn die Liste leer ist}{is what we return if the list is empty}
11 10
 \item \hinline{f} \ifger{ist unsere Funktion}{is our function} (\ifger{z.b.}{e.g.} \hinline{(*)} \ifger{oder}{or} \hinline{(+)})
11
+\item \hinline{z} \ifger{ist was die Funktion zurückgibt, wenn die Liste leer ist}{is what we return if the list is empty}
12 12
 \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}
13 13
 \end{itemizep}
14 14
 \slidep

+ 4
- 4
VL2/content/VL2_fold3.tex View File

@@ -2,13 +2,13 @@
2 2
 \pause
3 3
 \begin{haskellcode}
4 4
 sum :: [Int] -> Int
5
-sum xs = fold 0 (\x y -> x + y) xs
5
+sum xs = fold (\x y -> x + y) 0 xs
6 6
 -- a Haskeller would write
7
-sum = fold 0 (+)
7
+sum = fold (+) 0
8 8
 
9 9
 prod :: [Int] -> Int
10
-prod xs = fold 1 (\x y -> x * y) xs
10
+prod xs = fold (\x y -> x * y) 1 xs
11 11
 
12 12
 length :: [a] -> Int
13
-length xs = fold 0 (\x y -> 1 + y) xs
13
+length xs = fold (\x y -> 1 + y) 0 xs
14 14
 \end{haskellcode}