Sets

common functions and operations for sets (union, intersection, difference, etc)


Functions

Predicates


The Sets module provides functions for performing set theory operations on blocks. It includes all standard set operations and set relationship tests.

Key Concepts

  • Sets are represented using blocks (duplicate values are handled automatically)
  • Standard set operations (union, intersection, difference)
  • Set relationship tests (subset, superset)
  • Support for powerset generation

Basic Usage

Basic Set Operations

A: [1 2 3 4]
B: [3 4 5 6]

union A B               ; => [1 2 3 4 5 6]
intersection A B        ; => [3 4]
difference A B          ; => [1 2]

Set Relationships

X: [1 2 3]
Y: [1 2 3 4]

subset? X Y             ; true
subset?.proper X Y      ; true
superset? Y X           ; true

; check if sets have common elements
intersect? X Y          ; true
disjoint? X Y           ; false

Note
A proper subset/superset means that one set must be strictly smaller/larger than the other - they cannot be equal.

Powerset Generation

print powerset [1 2 3]       
; [] [1] [2] [1 2] [3] [1 3] [2 3] [1 2 3] 

Note
The powerset contains all possible subsets of a set, including the empty set and the set itself.

Common Patterns

Symmetric Difference

A: [1 2 3 4]
B: [3 4 5 6]

; elements that are in either set but not in both
print difference.symmetric A B  ; 1 2 5 6

Set Operations with Multiple Sets

sets: [[1 2] [2 3] [3 4]]

result: fold sets => intersection
; []

unionAll: fold sets => union  
; [1 2 3 4]