Set: Purely Functional Sets
by Dave Herman (dherman at ccs dot neu dot edu)
This library provides two implementations of functional sets backed by hash tables: one comparing elements for equality using equal?, the other using eq?.
The data structures of this library are immutable. They are implemented on top of Racket’s immutable hash tables, and therefore should have O(1) running time for extending and O(log n) running time for lookup.
This library was inspired by SRFI-1 and GHC’s Data.Set library.
1 Getting started
This module provides two libraries, one for equal?-based sets, and one for eq?-based sets.
> (define heroes (list->seteq '(rocky bullwinkle)))
> (define villains (list->seteq '(boris natasha)))
> (for ([x (seteq-union heroes villains)]) (printf "~a~n" x))
rocky
bullwinkle
boris
natasha
2 Sets using equal?
(require data/set) | package: set |
procedure
(set-empty? s) → boolean?
s : set?
procedure
(set-intersection sets ...+) → set?
sets : set?
procedure
(set-intersections sets) → set?
sets : (nelistof set?)
procedure
(set-difference sets ...+) → set?
sets : set?
procedure
(set-differences sets) → set?
sets : (nelistof set?)
procedure
sets : set?
procedure
(set-unions sets?) → set?
sets? : (listof set?)
procedure
(set-partition sets ...+) →
set? set? sets : set?
procedure
(set-partitions sets) →
set? set? sets : (nelistof set?)
(values (set-differences sets) (set-intersection (car sets) (unions (cdr sets))))
(values (set-differences sets) (set-intersections sets)) ; not the same thing!
procedure
(set-adjoin s elts ...) → set?
s : set? elts : any
procedure
(set-contains? s x) → boolean?
s : set? x : any
procedure
s : set?
Sets themselves have the prop:sequence property and can therefore be used as sequences.
3 Sets using eq?
(require data/seteq) | package: set |
procedure
(list->seteq ls) → seteq?
ls : list?
procedure
(seteq->list s) → list?
s : seteq?
value
procedure
(seteq-empty? s) → boolean?
s : seteq?
procedure
(seteq-intersection sets ...+) → seteq?
sets : seteq?
procedure
(seteq-intersections sets) → seteq?
sets : (nelistof seteq?)
procedure
(seteq-difference sets ...+) → seteq?
sets : seteq?
procedure
(seteq-differences sets) → seteq?
sets : (nelistof seteq?)
procedure
(seteq-union sets ...) → seteq?
sets : seteq?
procedure
(seteq-unions sets?) → seteq?
sets? : (listof seteq?)
procedure
sets : seteq?
procedure
(seteq-xors sets) → seteq?
sets : (nelistof seteq?)
procedure
(seteq-partition sets ...+) →
seteq? seteq? sets : seteq?
procedure
(seteq-partitions sets) →
seteq? seteq? sets : (nelistof seteq?)
(values (seteq-differences sets) (seteq-intersection (car sets) (unions (cdr sets))))
(values (seteq-differences sets) (seteq-intersections sets)) ; not the same thing!
procedure
(seteq-adjoin s elts ...) → seteq?
s : seteq? elts : any
procedure
(seteq-contains? s x) → boolean?
s : seteq? x : any
procedure
s : seteq?
syntax
(for/seteq (for-clause ...) body ...+)
syntax
(for*/seteq (for-clause ...) body ...+)
Sets themselves have the prop:sequence property and can therefore be used as sequences.