next up previous contents
Next: Combining two sets: `union' Up: Set Operations Previous: Checking for a member   Contents


Adding an element to a set: `addelt'

This abstraction adds an element to a list if the element is not yet contained in the list thus preserving the essential characteristic of sets.


(define addelt 
   (lambda(x s)
      (if (memberp x s)
         s
         (cons x s))))



(define l1 (cons one (cons two (cons three 
                                        (cons four 
                                               nil)))))
;
(ldisp! l1)
                             --> 1
                                 2
                                 3
                                 4
;
(ldisp! (addelt three l1)) ;; ineffective because already a member
                             --> 1
                                 2
                                 3
                                 4
;
(ldisp! (addelt five l1))  ;; effective because not yet a member
                             --> 5
                                 1
                                 2
                                 3
                                 4
;



Georg P. Loczewski 2004-03-05


Impressum und Datenschutz