Lisp - Set Union



Set Union represents the all elements between two sets including common elements only once as set cannot have duplicate elements. Lisp provides union, a built-in function to get the union of two sets.

Syntax - union function

(union 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 union

Example - union of sets of numbers

Following example shows the union 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 union
(setf result-set (union 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 3 4 5 6 7) 

Example - union of sets of characters

Following example shows the union 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 union
(setf result-set (union 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)
(A B C D E F) 

Example - union of sets of list using :test

Following example shows the union 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 union
(setf result-set (union 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) (3 4) (5 6)) 

Example - union of sets of strings

Following example shows the union 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 union
(setf result-set (union 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" "banana" "date") 

Key Considerations

  • union 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.

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

nunion, destructive version of union

Lisp provides another version of union function as nunion function. It may destroy the cells of first set.

Syntax - nunion function

(nunion 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 union

Example - union of sets of numbers

Following example shows the union 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 union
(setf result-set (nunion 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 3 4 5 6 7) 
Advertisements