next up previous contents
Next: Associative Lists in A++ Up: Set Operations Previous: Adding an element to   Contents


Combining two sets: `union'

This abstraction takes two lists as arguments and creates a new list having the property of a set by ensuring that elements may not occur more than once in the resulting list.


(define union (lambda(s1 s2)
                (if (nullp s1) 
                  s2
                  (if (memberp (car s1) s2) 
                    (union (cdr s1) s2)
                    (cons (car s1) (union (cdr s1) s2))))))


(define l1 (cons one 
                  (cons two 
                         (cons three 
                                (cons four 
                                       nil)))))
;
(define l2 (cons four (cons five (cons six nil))))
;
(ldisp! l1)
                             --> 1
                                 2
                                 3
                                 4
;
(ldisp! l2)
                             --> 4
                                 5
                                 6
;
(ldisp! (union l1 l2))           
                             --> 1
                                 2
                                 3
                                 4 ;; kommt nur einmal vor
                                 5
                                 6



Georg P. Loczewski 2004-03-05


Impressum und Datenschutz