SHOW:
|
|
- or go back to the newest paste.
1 | ;; @author Simon Hoyer | |
2 | ;; Exkurs: Funktionen höherer Ordnung - Racket / Scheme | |
3 | ||
4 | ;; Vertrag: filter: (X -> boolean) listofX -> listofX | |
5 | ;; Beschreibung: filter filtert alle Elemente aus einer Liste für die, die boolean Funktion true ergibt. | |
6 | (check-expect (filter (lambda (x)(> x 5))(list 1 2 3 4 5 6 7 8)) '(6 7 8)) | |
7 | ||
8 | ;; Vertrag: map: (X -> X) listofX -> listofX | |
9 | ;; Beschreibung: map wendet Funktion auf jedes Element der Liste an | |
10 | (check-expect (map (lambda (x)(+ x 2))(list 2 3 8 7)) '(4 5 10 9)) | |
11 | ||
12 | - | ;; foldl -> rechts nach links |
12 | + | |
13 | - | ;; foldr -> links nach rechts |
13 | + | |
14 | ;; der Liste zurückgreift und y auf das nächste. (usw) | |
15 | ;; Der mittlere formale Parameter ist hierbei die base. Diese base ist der Startwert. | |
16 | ;; Beispiel für die Auswertung von foldr (von links nach rechts) | |
17 | ;; (foldr (lambda (x y)(+ x y)) 4 '(2 3 4 5)) | |
18 | ;; ((((4+2)+3)+4)+5) | |
19 | ;; Beispiel für die Auswertung von foldl (von rechts nach links) | |
20 | ;; (foldr (lambda (x y)(+ x y)) 4 '(2 3 4 5)) | |
21 | ;; ((((4+5)+4)+3)+2) | |
22 | (check-expect (foldr (lambda (x y)(+ x y)) 0 '(2 3 4 5)) 14) | |
23 | (check-expect (foldr (lambda (x y)(+ x y)) 9 '(2 3 4 5)) (+ 14 9)) | |
24 | (check-expect (foldl (lambda (x y)(+ x y)) 0 '(2 3 4 5)) 14) | |
25 | (check-expect (foldl (lambda (x y)(+ x y)) 3 '(2 3 4 5)) (+ 14 3)) |