1 day ago
my attempt to do the exercises in sicp.
Monday, July 21, 2008
sicp exercise 2.62
;; Exercise 2.62. Give a (n) implementation of union-set for sets represented as ordered lists.
(define (union-set set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
((and (null? set1) (null? set2)) (list))
(else
(let ((x1 (car set1))
(x2 (car set2)))
(cond ((> x1 x2) (cons x2
(union-set set1 (cdr set2))))
((= x1 x2) (cons x1
(union-set (cdr set1) (cdr set2))))
((< x1 x2) (cons x1
(union-set (cdr set1) set2))))))))
(define set1 (list 100 200 300 400 500))
(define set2 (list 10 20 30 40 50))
(define set3 (list 1 2 35 45 55))
(define set4 (list 10 20 50 70 90))
(display (union-set set1 set2)) (newline)
(display (union-set set2 set3)) (newline)
(display (union-set set3 set4)) (newline)
(display (union-set set2 set4)) (newline)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment