Lisp - Set Difference



Set Difference represents the elements of first set that do not appear in the second set. Lisp provides set-difference, a built-in function to get the difference of two sets.

Syntax - set-difference function

(set-difference 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 difference

Example - Set difference of sets of numbers

Following example shows the set differece 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))
; print the result
(print my-set-2)

(terpri)
; get set difference
(setf result-set (set-difference 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) 
(1 2) 

Example - Set difference of sets of characters

Following example shows the set differece 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))
; print the result
(print my-set-2)

(terpri)
; get set difference
(setf result-set (set-difference 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) 
(A E) 

Example - Set difference of sets of list using :test

Following example shows the set differece 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 difference
(setf result-set (set-difference 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)) 
((1 2)) 

Example - Set difference of sets of strings

Following example shows the set differece 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 difference
(setf result-set (set-difference 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") 
("apple" "cherry") 

Key Considerations

  • set-difference is a non-destructive function. Original list is not modified and a new list 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.

set-difference function is generally the most efficient and idiomatic way to perform set difference of Sets.

nset-difference, destructive version of set-difference

Lisp provides another version of set-difference function as nset-difference function. It may destroy the cells of first set.

Syntax - nset-difference function

(nset-difference 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 difference

Example - Set difference of sets of numbers

Following example shows the set differece 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))
; print the result
(print my-set-2)

(terpri)
; get set difference
(setf result-set (nset-difference 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) 
(1 2) 
Advertisements