Next: , Previous: , Up: srfi sets-and-bags   [Index]


2.37.10 Set theory operations

Function: set-union set0 set
Function: set-intersection set0 set
Function: set-difference set0 set
Function: set-xor set1 set2

Return a newly allocated set that is the union, intersection, asymmetric difference, or symmetric difference of the set arguments.

Asymmetric difference is extended to more than two sets by taking the difference between the first set and the union of the others. Symmetric difference is not extended beyond two sets. Elements in the result set are drawn from the first set in which they appear.

(import (vicare) (srfi :113) (srfi :114))

(let* ((S1 (set fixnum-comparator 1 2))
       (S2 (set fixnum-comparator 2 3))
       (S  (set-difference S1 S2)))
  (set->list S #t))
⇒ (1)
Function: set-union! set0 set
Function: set-intersection! set0 set
Function: set-difference! set0 set
Function: set-xor! set1 set2

Like set-union!, set-intersection!, set-difference!, set-xor! but they are allowed to mutate set0 and return it.

Function: bag-union bag0 bag
Function: bag-intersection bag0 bag
Function: bag-difference bag0 bag
Function: bag-xor bag1 bag2

Return a newly allocated bag that is the union, intersection, asymmetric difference, or symmetric difference of the bag arguments.

Asymmetric difference is extended to more than two bags by taking the difference between the first bag and the union of the others. Symmetric difference is not extended beyond two bags. Elements in the result bag are drawn from the first bag in which they appear.

The procedures behave as follows when both bags contain elements that are equal in the sense of the bags’ comparator:

bag-union

The number of equal elements in the result is the largest number of equal elements in any of the original bags.

bag-intersection

The number of equal elements in the result is the smallest number of equal elements in any of the original bags.

bag-difference

The number of equal elements in the result is the number of equal elements in the first bag, minus the number of elements in the other bags (but not less than zero).

bag-xor

The number of equal elements in the result is the absolute value of the difference between the number of equal elements in the first and second bags.

Function: bag-union! bag0 bag
Function: bag-intersection! bag0 bag
Function: bag-difference! bag0 bag
Function: bag-xor! bag1 bag2

Like bag-union!, bag-intersection!, bag-difference!, bag-xor! but they are allowed to mutate bag0 and return it.


Next: , Previous: , Up: srfi sets-and-bags   [Index]