Код Ревью
Сравни свои решения
#| BEGIN (Write your solution here) |#
(define (union-set set1 set2)
(let ((lst1 (tree->list set1))
(lst2 (tree->list set2)))
(list->tree (union-set-list lst1 lst2))))
(define (intersection-set set1 set2)
(let ((lst1 (tree->list set1))
(lst2 (tree->list set2)))
(list->tree (intersection-set-list lst1 lst2))))
(define (union-set-list set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
(else
(let ((x1 (car set1))
(x2 (car set2)))
(cond ((< x1 x2) (cons x1 (union-set-list (cdr set1) set2)))
((< x2 x1) (cons x2 (union-set-list set1 (cdr set2))))
(else (cons x1 (union-set-list (cdr set1) (cdr set2)))))))))
(define (intersection-set-list set1 set2)
(if (or (null? set1) (null? set2))
'()
(let ((x1 (car set1))
(x2 (car set2)))
(cond ((= x1 x2)
(cons x1 (intersection-set-list (cdr set1) (cdr set2))))
((< x1 x2) (intersection-set-list (cdr set1) set2))
((< x2 x1) (intersection-set-list set1 (cdr set2)))))))
;; -- tree to list --
(define (tree->list tree)
(define (copy-to-list tree result-list)
(if (null? tree)
result-list
(copy-to-list (left-branch tree)
(cons (entry tree)
(copy-to-list (right-branch tree)
result-list)))))
(copy-to-list tree '()))
#| END |#