Lisp - Set XOR
Set XOR represents the elements which appears only once in both sets. Lisp provides set-exclusive-or, a built-in function to get the xor of two sets.
Syntax - set-exclusive-or function
(set-exclusive-or 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 xor
Example - Set xor of sets of numbers
Following example shows the set xor 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 set xor (setf result-set (set-exclusive-or 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) (1 2 6 7)
Example - Set xor of sets of characters
Following example shows the set xor 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 g)) ; print the result (print my-set-2) (terpri) ; get set xor (setf result-set (set-exclusive-or 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 G) (A F G)
Example - Set xor of sets of list using :test
Following example shows the set xor 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 xor (setf result-set (set-exclusive-or 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) (5 6))
Example - Set xor of sets of strings
Following example shows the set xor 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 xor
(setf result-set (set-exclusive-or 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" "date")
Key Considerations
set-exclusive-or 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-exclusive-or function is generally the most efficient and idiomatic way to perform set xor of Sets.
nset-exclusive-or, destructive version of set-exclusive-or
Lisp provides another version of set-exclusive-or function as nset-exclusive-or function. It may destroy the cells of first set.
Syntax - set-exclusive-or function
(set-exclusive-or 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 xor
Example - Set xor of sets of numbers
Following example shows the set xor 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 set xor (setf result-set (nset-exclusive-or 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) (1 2 6 7)