SETS

LISP provides functions that allow a List of atoms, that we are going to call lats, be treated as sets and manipulate them with set functions like adjoin, intersection.


(MEMBER item set)

The first question about sets is to test if an element is part of the set, and this is what this function will return, Nil if the item is not in the set argument, otherwise return the tail of its set beginning with the first element that satisfied the test.

Example:

(MEMBER 'hello '(a b c d)  ) = Nil                      
(MEMBER 'b     '(a b c d)  ) = (b c d)                      
(MEMBER 'b     '()         ) = Nil                                               

TRY:

(MEMBER ' ' ) =

Exercise:

Also, you can write your own recursive function to test if an item is member of a set. Try to do it.

=
Answer


(ADJOIN element set)

It is used to add an element to a set, previewed that it is not already a member. The element will be added to the result list if there is no this element in the set.

Example:

(ADJOIN 'a '(a b c d)  ) = (a b c d)                      
(ADJOIN 'e '(a b c d)  ) = (a b c d e)

TRY:

=


(UNION set1 set2)

It takes two sets and returns a new set that contains everything that is an element of either of the sets. If there is a duplication between the two list, only one of the atoms within it.

Example:

(UNION '(e f g h) '(a b c d)  ) = (e f g h a b c d)
(UNION '(a b c)   '(d b e)    ) = (a b c d e) 

TRY:

(UNION ' ' ) =

Exercise:

Now, using the definition and your knowledge about recursive functions, to write your own UNIONSET function.

=
Answer


(INTERSECTION set1 set2)

It takes two sets and returns a new set that contains everything that is an element of both argument sets.

Example:

(INTERSECTION '(e f g h) '(a b f d)  ) = (f)
(INTERSECTION '(a b c)   '(d b e)    ) = (b) 
(INTERSECTION '(a b c)   '(d e f)    ) = () 

TRY:

(INTERSECTION ' ' ) = .

Exercise:

Now, using the definition and your knowledge about recursive functions, to write your own INTERSET function.


Answer