VOOZH about

URL: https://www.geeksforgeeks.org/lisp/sets-in-lisp/

⇱ Sets in LISP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sets in LISP

Last Updated : 31 Jan, 2022

A set is an unordered collection of items. A set is just like other data structures. In C++ we have a similar data structure called a hash map. Common lisp does not provide a built-in set data type, but it provides a number of functions that allow set operations to be performed onto a list. Using them, we can add and remove items from the list and also check if an item is present in the list. We can also perform union, intersection, and difference operations on the list.

Implementing  Sets in the Lisp:

Sets are implemented using the adjoin function. Adjoin function allows us to build up a set.  Adjoin function can take a list as an argument and returns a set. The adjoin function is defined as follows:

Syntax:

adjoin item list &key key test => new-list

Here, the item is an object, the list is a proper list, the test is a designator for a function of two-argument and it returns a boolean value, the key is a designator function for a function of one argument.  

These key and test argument is used for checking whether an item is present in the list or not. If the item is present in the list, then the item is not added to the set. If the item is not present in the list, then the item is added to the set. We can use the macro pushnew to add an item to the set, if the item is not present in the set, and DELETE or REMOVE to remove or delete the item.

Example:

Create a new lisp file and save it as set.lisp

Output:

👁 Image
Set implementation

Checking Membership in a Set:

The Membership is a function that checks whether an item is present in the set or not. The membership function is defined as follows:

member item list &key :test :test-not :key  

member-if predicate list &key :key  

member-if-not predicate list &key :key

These functions probe the list for the item. If the item is present in the list, then the membership function returns t. If the item is not present in the list, then the membership function returns nil. The search is done at the top level only. The search is done in the order of the list.

Syntax:

member item list test &key key &key => tail or nil

Here, the item is an item that is to be found, the list is the list to be searched, the test is the item to be compared, and &key is the function key. Key is a function for extracting the value before the test. Member function searches the list for the item. If the item is present in the list, then the membership function returns t. If the item is not present in the list, then the membership function returns nil. The search is done at the top level only. The search is done in the order of the list.

Example:

Create a new lisp file and save it as membership.lisp.

Output:

👁 Image
Membership in Set

Set union in Lisp:

The union means the set of all the items in the list. Using the union group function, we can find the union of two sets. The union function is defined as follows:

Syntax:

union list1 list2 &key :test 
nunion list1 list2 &key :test 

Here, the union function takes two lists, list1 and list2, and returns a new list that contains all the items from both the lists. Duplicate items are not allowed in the new list.

Example:

Create a new lisp file and save it as union.lisp.

Output:

👁 Image
Union in Set

Set intersection in Lisp:

The intersection can think of as the set of items that are common to both sets. To do intersection, we can use the intersection group of functions. It takes two lists and returns a new list that contains all the items that are common to both lists. The intersection function is defined as follows:

Syntax:

intersection list1 list2 &key :test
nintersection list1 list2 &key :test

Here the intersection function takes two lists, list1 and list2, and returns a new list that contains all the items that are common to both the lists. The intersection is the same as the intersection function except that it destroys the list1 using its cells to construct the result list2 and returns the result list2.  

Example:

Create a new lisp file and save it as intersection.lisp.

Output:

👁 Image
A set intersection in Lisp

Set Difference in Lisp:

A set difference is the set of items that are in the first set but not in the second set. Using the set difference function, we can find the set difference of two sets. The set difference function is defined as follows:

Syntax:

set-difference list1 list2 &key :test 
nset-difference list1 list2 &key :test

Here, the set difference function takes two lists, list1 and list2, and returns a new list that contains all the items that are in the first list but not in the second list. The nset-difference is the same as the set difference function except that it destroys the list1 using its cells to construct the result list2 and returns the result list2.

Set-difference returns a list of items that are in the first list but not in the second list.

Nset is the destructive version of the set-difference, it destroys the list1 and returns the result list2.

Example:

Create a new lisp file and save it as set-difference.lisp.

Output:

👁 Image
Set difference in lisp
Comment