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 (filter predicate sequence)
11 665c255d 2023-08-04 jrmu (cond ((null? sequence) '())
12 665c255d 2023-08-04 jrmu ((predicate (car sequence))
13 665c255d 2023-08-04 jrmu (cons (car sequence)
14 665c255d 2023-08-04 jrmu (filter predicate (cdr sequence))))
15 665c255d 2023-08-04 jrmu (else (filter predicate (cdr sequence)))))
16 665c255d 2023-08-04 jrmu (define (accumulate op initial sequence)
17 665c255d 2023-08-04 jrmu (if (null? sequence)
18 665c255d 2023-08-04 jrmu initial
19 665c255d 2023-08-04 jrmu (op (car sequence)
20 665c255d 2023-08-04 jrmu (accumulate op initial (cdr sequence)))))
21 665c255d 2023-08-04 jrmu (define (enumerate-interval low high)
22 665c255d 2023-08-04 jrmu (if (low > high)
23 665c255d 2023-08-04 jrmu '()
24 665c255d 2023-08-04 jrmu (cons low (enumerate-interval (1+ low) high))))
25 665c255d 2023-08-04 jrmu (define (enumerate-tree tree)
26 665c255d 2023-08-04 jrmu (cond ((null? tree) '())
27 665c255d 2023-08-04 jrmu ((not (pair? tree)) (list tree))
28 665c255d 2023-08-04 jrmu (else (append (enumerate-tree (car tree))
29 665c255d 2023-08-04 jrmu (enumerate-tree (cdr tree))))))
30 665c255d 2023-08-04 jrmu
31 665c255d 2023-08-04 jrmu ;; Exercise 2.33. Fill in the missing expressions to complete the following definitions of some basic list-manipulation operations as accumulations:
32 665c255d 2023-08-04 jrmu
33 665c255d 2023-08-04 jrmu (define (map p sequence)
34 665c255d 2023-08-04 jrmu (accumulate (lambda (x y)
35 665c255d 2023-08-04 jrmu (cons (p x) y))
36 665c255d 2023-08-04 jrmu '()
37 665c255d 2023-08-04 jrmu sequence))
38 665c255d 2023-08-04 jrmu
39 665c255d 2023-08-04 jrmu (test-case (map square '()) '())
40 665c255d 2023-08-04 jrmu (test-case (map square '(1)) '(1))
41 665c255d 2023-08-04 jrmu (test-case (map square '(1 2 3 4 5)) '(1 4 9 16 25))
42 665c255d 2023-08-04 jrmu
43 665c255d 2023-08-04 jrmu (define (append seq1 seq2)
44 665c255d 2023-08-04 jrmu (accumulate cons
45 665c255d 2023-08-04 jrmu seq2
46 665c255d 2023-08-04 jrmu seq1))
47 665c255d 2023-08-04 jrmu
48 665c255d 2023-08-04 jrmu (test-case (append '() '()) '())
49 665c255d 2023-08-04 jrmu (test-case (append '(1 2 3) '()) '(1 2 3))
50 665c255d 2023-08-04 jrmu (test-case (append '() '(4 5 6)) '(4 5 6))
51 665c255d 2023-08-04 jrmu (test-case (append '(1 2 3) '(4 5 6)) '(1 2 3 4 5 6))
52 665c255d 2023-08-04 jrmu (test-case (append '((1 (2)) 3) '((4 ((5)) 6))) '((1 (2)) 3 (4 ((5)) 6)))
53 665c255d 2023-08-04 jrmu
54 665c255d 2023-08-04 jrmu (define (length sequence)
55 665c255d 2023-08-04 jrmu (accumulate (lambda (first accum)
56 665c255d 2023-08-04 jrmu (1+ accum))
57 665c255d 2023-08-04 jrmu 0
58 665c255d 2023-08-04 jrmu sequence))
59 665c255d 2023-08-04 jrmu
60 665c255d 2023-08-04 jrmu (test-case (length '()) 0)
61 665c255d 2023-08-04 jrmu (test-case (length '(1 2 3)) 3)
62 665c255d 2023-08-04 jrmu (test-case (length '((1 (2)) 3 (4 ((5)) 6))) 3)