Blame


1 665c255d 2023-08-04 jrmu (define (test-case actual expected)
2 665c255d 2023-08-04 jrmu (newline)
3 665c255d 2023-08-04 jrmu (display "Actual: ")
4 665c255d 2023-08-04 jrmu (display actual)
5 665c255d 2023-08-04 jrmu (newline)
6 665c255d 2023-08-04 jrmu (display "Expected: ")
7 665c255d 2023-08-04 jrmu (display expected)
8 665c255d 2023-08-04 jrmu (newline))
9 665c255d 2023-08-04 jrmu
10 665c255d 2023-08-04 jrmu (define (accumulate op initial sequence)
11 665c255d 2023-08-04 jrmu (if (null? sequence)
12 665c255d 2023-08-04 jrmu initial
13 665c255d 2023-08-04 jrmu (op (car sequence)
14 665c255d 2023-08-04 jrmu (accumulate op initial (cdr sequence)))))
15 665c255d 2023-08-04 jrmu
16 665c255d 2023-08-04 jrmu (define (accumulate-n op init seqs)
17 665c255d 2023-08-04 jrmu (if (null? (car seqs))
18 665c255d 2023-08-04 jrmu '()
19 665c255d 2023-08-04 jrmu (cons (accumulate op init (map car seqs))
20 665c255d 2023-08-04 jrmu (accumulate-n op init (map cdr seqs)))))
21 665c255d 2023-08-04 jrmu
22 665c255d 2023-08-04 jrmu ;; Exercise 2.38. The accumulate procedure is also known as fold-right, because it combines the first element of the sequence with the result of combining all the elements to the right. There is also a fold-left, which is similar to fold-right, except that it combines elements working in the opposite direction:
23 665c255d 2023-08-04 jrmu
24 665c255d 2023-08-04 jrmu (define (fold-left op initial sequence)
25 665c255d 2023-08-04 jrmu (define (iter result rest)
26 665c255d 2023-08-04 jrmu (if (null? rest)
27 665c255d 2023-08-04 jrmu result
28 665c255d 2023-08-04 jrmu (iter (op result (car rest))
29 665c255d 2023-08-04 jrmu (cdr rest))))
30 665c255d 2023-08-04 jrmu (iter initial sequence))
31 665c255d 2023-08-04 jrmu
32 665c255d 2023-08-04 jrmu ;; What are the values of
33 665c255d 2023-08-04 jrmu ;;(/ 1 (/ 2 (/ 3 1)))
34 665c255d 2023-08-04 jrmu (test-case (fold-right / 1 (list 1 2 3)) 3/2)
35 665c255d 2023-08-04 jrmu ;;(/ (/ (/ 1 1) 2) 3)
36 665c255d 2023-08-04 jrmu (test-case (fold-left / 1 (list 1 2 3)) 1/6)
37 665c255d 2023-08-04 jrmu ;;(list 1 (list 2 (list 3 '())))
38 665c255d 2023-08-04 jrmu (test-case (fold-right list '() (list 1 2 3)) '(1 (2 (3 ()))))
39 665c255d 2023-08-04 jrmu ;;(list (list (list nil 1) 2) 3)
40 665c255d 2023-08-04 jrmu (test-case (fold-left list '() (list 1 2 3)) '(((() 1) 2) 3))
41 665c255d 2023-08-04 jrmu
42 665c255d 2023-08-04 jrmu ;; Give a property that op should satisfy to guarantee that fold-right and fold-left will produce the same values for any sequence.
43 665c255d 2023-08-04 jrmu
44 665c255d 2023-08-04 jrmu ;; we need both associativity and commutativity
45 665c255d 2023-08-04 jrmu ;; associativity
46 665c255d 2023-08-04 jrmu ;; (op a (op b c)) = (op (op a b) c)
47 665c255d 2023-08-04 jrmu ;; commutativity
48 665c255d 2023-08-04 jrmu ;; (op a b) = (op b a)
49 665c255d 2023-08-04 jrmu
50 665c255d 2023-08-04 jrmu ;; '(a b c)
51 665c255d 2023-08-04 jrmu ;; fold-right
52 665c255d 2023-08-04 jrmu ;; (op a (op b (op c initial)))
53 665c255d 2023-08-04 jrmu ;; fold-left
54 665c255d 2023-08-04 jrmu ;; (op (op (op initial a) b) c)
55 665c255d 2023-08-04 jrmu ;; associativity
56 665c255d 2023-08-04 jrmu ;; (op (op initial (op a b)) c)
57 665c255d 2023-08-04 jrmu ;; commutativity
58 665c255d 2023-08-04 jrmu ;; (op (op (op a b) initial) c)
59 665c255d 2023-08-04 jrmu ;; associativity
60 665c255d 2023-08-04 jrmu ;; (op (op a (op b initial)) c)
61 665c255d 2023-08-04 jrmu ;; associativity
62 665c255d 2023-08-04 jrmu ;; (op a (op (op b initial) c))
63 665c255d 2023-08-04 jrmu ;; associativity
64 665c255d 2023-08-04 jrmu ;; (op a (op b (op initial c)))
65 665c255d 2023-08-04 jrmu ;; commutativity
66 665c255d 2023-08-04 jrmu ;; (op a (op b (op c initial)))
67 665c255d 2023-08-04 jrmu
68 665c255d 2023-08-04 jrmu
69 665c255d 2023-08-04 jrmu