commit f391291916c9f8e9d5f72efa98efab26329732ff parent 450ab6c55832d57bd4a5e8d0d309ec26b29363f5 Author: Yuval Langer <yuval.langer@gmail.com> Date: Sat, 18 Mar 2023 19:35:41 +0200 Add solution to exercise 2.59. Diffstat:
M | guile.scm | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/guile.scm b/guile.scm @@ -3196,3 +3196,48 @@ Q1 - Q4 = (4 - 1, 1 - 4) (test-end "deriv-stuff")) (deriv-stuff) + +(define (sets-stuff) + (define (element-of-set? x set) + (cond + ((null? set) #f) + ((equal? x (car set)) #t) + (else (element-of-set? x (cdr set))))) + + (define (adjoin-set x set) + (if (element-of-set? x set) + set + (cons x set))) + + (define (intersection-set set1 set2) + (cond + ((or (null? set1) + (null? set2)) + '()) + ((element-of-set? (car set1) + set2) + (cons (car set1) + (intersection-set (cdr set1) + set2))) + (else (intersection-set (cdr set1) + set2)))) + + (define (union-set set1 set2) + (cond + ((null? set1) set2) + ((null? set2) set1) + ((element-of-set? (car set1) + set2) + (union-set (cdr set1) + set2)) + (else (cons (car set1) + (union-set (cdr set1) + set2))))) + + (test-begin "2.59") + (test-equal + '(1 2 4 3 5) + (union-set '(1 3 2) '(4 3 5))) + (test-end "2.59")) + +(sets-stuff)