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 (element-of-set? x set)
11 665c255d 2023-08-04 jrmu (cond ((null? set) false)
12 665c255d 2023-08-04 jrmu ((= x (car set)) true)
13 665c255d 2023-08-04 jrmu ((< x (car set)) false)
14 665c255d 2023-08-04 jrmu (else (element-of-set? x (cdr set)))))
15 665c255d 2023-08-04 jrmu (define (intersection-set set1 set2)
16 665c255d 2023-08-04 jrmu (if (or (null? set1) (null? set2))
17 665c255d 2023-08-04 jrmu '()
18 665c255d 2023-08-04 jrmu (let ((x1 (car set1)) (x2 (car set2)))
19 665c255d 2023-08-04 jrmu (cond ((= x1 x2) (cons x1
20 665c255d 2023-08-04 jrmu (intersection-set (cdr set1)
21 665c255d 2023-08-04 jrmu (cdr set2))))
22 665c255d 2023-08-04 jrmu ((< x1 x2) (intersection-set (cdr set1)
23 665c255d 2023-08-04 jrmu set2))
24 665c255d 2023-08-04 jrmu ((> x1 x2) (intersection-set set1
25 665c255d 2023-08-04 jrmu (cdr set2)))))))
26 665c255d 2023-08-04 jrmu
27 665c255d 2023-08-04 jrmu ;; Exercise 2.61. Give an implementation of adjoin-set using the ordered representation. By analogy with element-of-set? show how to take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation.
28 665c255d 2023-08-04 jrmu
29 665c255d 2023-08-04 jrmu (define (adjoin-set x set)
30 665c255d 2023-08-04 jrmu (cond ((null? set) (list x))
31 665c255d 2023-08-04 jrmu ((= x (car set)) set)
32 665c255d 2023-08-04 jrmu ((< x (car set)) (cons x set))
33 665c255d 2023-08-04 jrmu (else (cons (car set) (adjoin-set x (cdr set))))))
34 665c255d 2023-08-04 jrmu
35 665c255d 2023-08-04 jrmu (test-case (adjoin-set 5 '()) '(5))
36 665c255d 2023-08-04 jrmu (test-case (adjoin-set 5 '(1 2 3 4 5)) '(1 2 3 4 5))
37 665c255d 2023-08-04 jrmu (test-case (adjoin-set 5 '(1 2 3 4)) '(1 2 3 4 5))
38 665c255d 2023-08-04 jrmu (test-case (adjoin-set 5 '(6 7 8 9)) '(5 6 7 8 9))
39 665c255d 2023-08-04 jrmu (test-case (adjoin-set 5 '(1 2 3 4 6 7 8 9)) '(1 2 3 4 5 6 7 8 9))
40 665c255d 2023-08-04 jrmu
41 665c255d 2023-08-04 jrmu Exercise 2.62. Give a (n) implementation of union-set for sets represented as ordered lists.