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 ;; Exercise 2.36. The procedure accumulate-n is similar to accumulate except that it takes as its third argument a sequence of sequences, which are all assumed to have the same number of elements. It applies the designated accumulation procedure to combine all the first elements of the sequences, all the second elements of the sequences, and so on, and returns a sequence of the results. For instance, if s is a sequence containing four sequences, ((1 2 3) (4 5 6) (7 8 9) (10 11 12)), then the value of (accumulate-n + 0 s) should be the sequence (22 26 30). Fill in the missing expressions in the following definition of accumulate-n:
17 665c255d 2023-08-04 jrmu
18 665c255d 2023-08-04 jrmu (define (accumulate-n op init seqs)
19 665c255d 2023-08-04 jrmu (if (null? (car seqs))
20 665c255d 2023-08-04 jrmu '()
21 665c255d 2023-08-04 jrmu (cons (accumulate op init (map car seqs))
22 665c255d 2023-08-04 jrmu (accumulate-n op init (map cdr seqs)))))
23 665c255d 2023-08-04 jrmu (test-case (accumulate-n + 0 '((1 2 3) (4 5 6) (7 8 9) (10 11 12))) '(22 26 30))
24 665c255d 2023-08-04 jrmu (test-case (accumulate-n + 0 '(() () ())) '())
25 665c255d 2023-08-04 jrmu ;; (test-case (accumulate-n + 0 '()) (error "Trying to car empty list"))
26 665c255d 2023-08-04 jrmu