Implement the union-set operation for the unordered-list representation of sets.
Extracted from Structure and Interpretation of Computer Programs, SICP
Attempts.
Define a function element-of-set?
by
1 2 3 4 |
(define (element-of-set? x set) (cond ((null? set) false) ((equal? x (car set)) true) (else (element-of-set? x (cdr set))))) |
to determine whether an object x
is an element of a set set
.
Define a function intersection-set
by
1 2 3 4 5 6 |
(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)))) |
to draw the intersection intersection-set
of two sets set1
and set2
.
Define a function union-set
by
1 2 3 4 5 6 7 8 |
(define (union-set set1 set2) (cond ((and (null? set1) (null? set2)) ’()) ((null? set1) set2) ((null? set2) set1) ((element-of-set? (car set1) set2) (cons (car set1) (union-set (cdr set1) set2))) (else (union-set (cdr set1) set2)))) |
to draw the union union-set
of two sets set1
and set2
.