Lisp - Set Intersection



Set Intersection represents the common elements between two sets. Lisp provides intersection, a built-in function to get the intersection of two sets.

Syntax - intersection function

(intersection list1 list2 &key :test :test-not :key)

Arguments

  • list1− first set

  • list2− second set

  • :test− Function of two arguments to compare elements of Set. By default, eql is the function used.

  • :test-not− Function of two arguments returning true if elements are not equal.

  • :key− Function of one argument to be applied on each item of the set to be used for intersection

Example - Intersection of sets of numbers

Following example shows the Intersection of set of numbers.

main.lisp

; define a set of values
(defvar my-set-1 '(1 2 3 4 5 ))
; print the result
(print my-set-1)

(terpri)
; define another set of values
(defvar my-set-2 '(3 4 5 6 7))
; print the result
(print my-set-2)

(terpri)
; get intersection
(setf result-set (intersection my-set-1 my-set-2))
; print the result
(print result-set)

Output

When you execute the code, it returns the following result −

(1 2 3 4 5) 
(3 4 5 6 7) 
(3 4 5)

Example - Intersection of sets of characters

Following example shows the intersection of set of characters.

main.lisp

; define a set of values
(defvar my-set-1 '(a b c d e))
; print the result
(print my-set-1)

(terpri)
; define another set of values
(defvar my-set-2 '(b c d e f))
; print the result
(print my-set-2)

(terpri)
; get set intersection
(setf result-set (intersection my-set-1 my-set-2))
; print the result
(print result-set)

Output

When you execute the code, it returns the following result −

(A B C D E) 
(B C D E F) 
(B C D E)

Example - Intersection of sets of list using :test

Following example shows the Intersection of set of list.

main.lisp

; define a set of lists
(defvar my-set-1 '((1 2)(3 4)))
; print the result
(print my-set-1)

(terpri)
; define another set of list
(defvar my-set-2 '((3 4)(5 6)))
; print the result
(print my-set-2)

(terpri)
; get set intersection
(setf result-set (intersection my-set-1 my-set-2 :test #'equal))
; print the result
(print result-set)

Output

When you execute the code, it returns the following result −

((1 2) (3 4)) 
((3 4) (5 6)) 
((3 4)) 

Example - Intersection of sets of strings

Following example shows the intersection of set of strings.

main.lisp

; define a set of lists
(defvar my-set-1 '("apple" "banana" "cherry"))
; print the result
(print my-set-1)

(terpri)
; define another set of list
(defvar my-set-2 '("banana" "date"))
; print the result
(print my-set-2)

(terpri)
; get set intersection
(setf result-set (intersection my-set-1 my-set-2 :test #'string=))
; print the result
(print result-set)

Output

When you execute the code, it returns the following result −

("apple" "banana" "cherry") 
("banana" "date") 
("banana") 

Key Considerations

  • intersection is a non-destructive function. Original set is not modified and a new set is returned.

  • :test argument can be used to specify custom equality function.

  • :test-not argument is inverse of :test.

  • :key keyword allows to compare part of an element.

intersection function is generally the most efficient and idiomatic way to perform intersection of Sets.

nintersection, destructive version of intersection

Lisp provides another version of intersection function as nintersection function. It may destroy the cells of first set.

Syntax - nintersection function

(nintersection list1 list2 &key :test :test-not :key)

Arguments

  • list1− first set

  • list2− second set

  • :test− Function of two arguments to compare elements of Set. By default, eql is the function used.

  • :test-not− Function of two arguments returning true if elements are not equal.

  • :key− Function of one argument to be applied on each item of the set to be used for intersection

Example - Intersection of sets of numbers

Following example shows the Intersection of set of numbers.

main.lisp

; define a set of values
(defvar my-set-1 '(1 2 3 4 5 ))
; print the result
(print my-set-1)

(terpri)
; define another set of values
(defvar my-set-2 '(3 4 5 6 7))
; print the result
(print my-set-2)

(terpri)
; get intersection
(setf result-set (nintersection my-set-1 my-set-2))
; print the result
(print result-set)

Output

When you execute the code, it returns the following result −

(1 2 3 4 5) 
(3 4 5 6 7) 
(3 4 5)
Advertisements