learning-sicp

My embarrassing half assed SICP run.
Log | Files | Refs

commit 642a48b26f8456bb31d1248ff40971f63ebe74fb
parent f391291916c9f8e9d5f72efa98efab26329732ff
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Sun, 19 Mar 2023 10:19:11 +0200

Add solutions to 2.61 and 2.62, but 2.60 needs some tests and I am not sure about its solution.

Diffstat:
Mguile.scm | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 137 insertions(+), 6 deletions(-)

diff --git a/guile.scm b/guile.scm @@ -3216,9 +3216,9 @@ Q1 - Q4 = (4 - 1, 1 - 4) '()) ((element-of-set? (car set1) set2) - (cons (car set1) - (intersection-set (cdr set1) - set2))) + (adjoin-set (car set1) + (intersection-set (cdr set1) + set2))) (else (intersection-set (cdr set1) set2)))) @@ -3230,9 +3230,9 @@ Q1 - Q4 = (4 - 1, 1 - 4) set2) (union-set (cdr set1) set2)) - (else (cons (car set1) - (union-set (cdr set1) - set2))))) + (else (adjoin-set (car set1) + (union-set (cdr set1) + set2))))) (test-begin "2.59") (test-equal @@ -3241,3 +3241,134 @@ Q1 - Q4 = (4 - 1, 1 - 4) (test-end "2.59")) (sets-stuff) + +;; Exercise 2.60 XXX + +(define (exercise-2.60) + ;; I don't get it. Am I supposed to let these sets explode by just appending + ;; more and more stuff to them? + + (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) + (cons x set)) + + (define (union-set set1 set2) + (append set1 set2)) + + (define (intersection-set set1 set2) + (cond + ((or (null? set1) + (null? set2)) '()) + ((equal? (car set1) (element-of-set? (car set1) + set2)) + (adjoin-set (car set1) + (intersection-set (cdr set1) + set2))) + (else (intersection-set (cdr set1) + set2)))) + + (test-begin "2.60") + (test-end "2.60")) + +(exercise-2.60) + +(define (ordered-list-set-stuff) + (define (element-of-set? x set) + (cond + ((= x (car set)) + #t) + ((> x (car set)) + #f) + (else (element-of-set? x (cdr set))))) + + (define (intersection-set set1 set2) + (cond + ((null? set1) '()) + ((null? set2) '()) + ((= (car set1) + (car set2)) + (cons (car set1) + (intersection-set (cdr set1) + (cdr set2)))) + ((< (car set1) + (car set2)) + (intersection-set (cdr set1) + set2)) + ((> (car set1) + (car set2)) + (intersection-set set1 + (cdr set2))))) + + (define (adjoin-set x set) + (cond + ((null? set) (cons x '())) + ((< x (car set)) (cons x set)) + ((= x (car set)) set) + (else (cons (car set) + (adjoin-set x (cdr set)))))) + + (define (union-set set1 set2) + (cond + ((null? set1) set2) + ((null? set2) set1) + ((< (car set1) + (car set2)) + (cons (car set1) + (union-set (cdr set1) + set2))) + ((> (car set1) + (car set2)) + (cons (car set2) + (union-set set1 + (cdr set2)))) + (else (cons (car set1) + (union-set (cdr set1) + (cdr set2)))))) + (test-begin "2.61") + (test-equal + '(1) + (adjoin-set 1 '())) + (test-equal + '(1) + (adjoin-set 1 '(1))) + (test-equal + '(1 2) + (adjoin-set 1 '(2))) + (test-equal + '(1 2) + (adjoin-set 2 '(1))) + (test-equal + '(1 2 3 4 5 6) + (adjoin-set 3 '(1 2 4 5 6))) + (test-end "2.61") + + (test-begin "2.62") + (test-equal + '() + (union-set '() '())) + (test-equal + '(1) + (union-set '(1) '())) + (test-equal + '(1) + (union-set '() '(1))) + (test-equal + '(1) + (union-set '(1) '(1))) + (test-equal + '(1 2) + (union-set '(1 2) '(1))) + (test-equal + '(1 2) + (union-set '(1) '(1 2))) + (test-equal + '(1 2 3) + (union-set '(2 3) '(1 2 3))) + (test-end "2.62")) + +(ordered-list-set-stuff)