Std\Set
Set — persistent set backed by a Hash Array Mapped Trie (HAMT).
Provides immutable sets with O(log32 n) insert, membership test, and standard set operations (union, intersection, difference). Iterators use stack-based trie traversal with O(1) memory per element.
Functions
Section titled “Functions”insert : Set a -> a -> Set a
Section titled “insert : Set a -> a -> Set a”Add an element to the set. Returns a new set containing elem.
The original set is unchanged. Inserting a duplicate is a no-op.
let s = insert (insert #{} 1) 2 insize s # => 2contains : Set a -> a -> Bool
Section titled “contains : Set a -> a -> Bool”Check whether elem is a member of the set.
let s = insert #{} 42 incontains s 42 # => truecontains s 99 # => falsesize : Set a -> Int
Section titled “size : Set a -> Int”Returns the number of elements in the set.
let s = insert (insert #{} 1) 2 insize s # => 2elements : Set a -> [a]
Section titled “elements : Set a -> [a]”Eagerly collects all elements into a sequence.
let s = insert (insert #{} 3) 1 inelements s # => [3, 1] (order may vary)union : Set a -> Set a -> Set a
Section titled “union : Set a -> Set a -> Set a”Returns a new set containing all elements from both a and b.
let a = insert (insert #{} 1) 2 inlet b = insert (insert #{} 2) 3 inelements (union a b) # => [1, 2, 3] (order may vary)intersection : Set a -> Set a -> Set a
Section titled “intersection : Set a -> Set a -> Set a”Returns a new set containing only elements present in both a and b.
let a = insert (insert #{} 1) 2 inlet b = insert (insert #{} 2) 3 inelements (intersection a b) # => [2]difference : Set a -> Set a -> Set a
Section titled “difference : Set a -> Set a -> Set a”Returns a new set containing elements in a that are not in b.
let a = insert (insert (insert #{} 1) 2) 3 inlet b = insert #{} 2 inelements (difference a b) # => [1, 3] (order may vary)iterator : Set a -> Iterator a
Section titled “iterator : Set a -> Iterator a”Returns a streaming Iterator Int over set elements.
Uses stack-based trie traversal — O(1) memory per element.
import iterator from Std\Set inlet s = insert (insert #{} 1) 2 inlet iter = iterator s in# consume with iterator protocolforEach : (a -> b) -> Set a -> ()
Section titled “forEach : (a -> b) -> Set a -> ()”Apply callback to each element for side effects.
let s = insert (insert #{} 1) 2 inforEach (\x -> println (show x)) s