Exercise 2.59 -- 2.60

Exercise 2.59

Implement the union-set operation for the unordered-list representation of sets.

Solution

(define (union-set set1 set2)
  (cond ((null? set1) set2)
        ((not (element-of-set? (car set1) set2))
         (cons (car set1)
              (union-set (cdr set1) set2)))
        (else (union-set (cdr set1) set2))))

Exercise 2.60

We specified that a set would be represented as a list with no duplicates. For instance, the set {1, 2, 3} could be represented as the list (2 3 2 1 3 2 2). Design procedures element-of set?, adjoin-set, union-set and intersection-set that operate on this representation. How does the efficiency of each compare with the corresponding procedure for the non-duplicate representation? Are there applications for which you would use this representation in preference of the non-duplicate one?

Solution

?? TODO