10 Sets
(require (planet krhari/pfds:1:2/set)) |
An simple implementation of sets based on binary trees.
(set comp a ...) → (Set A) |
comp : (A A -> Boolean) |
a : A |
Example: |
> (set < 1 2 3 4 5) |
- : (Set Positive-Fixnum) |
#<Set> |
In the above example, the set obtained will have 2 as its root and < as the comparison function.
(empty? set) → Boolean |
set : (Set A) |
(insert a set) → (Set A) |
a : A |
set : (Set A) |
Examples: |
> (insert 10 (set < 1 2 3 4 5 6)) |
- : (Set Positive-Fixnum) |
#<Set> |
> (insert 1 (set < 1 2 3 4 5 6)) |
- : (Set Positive-Fixnum) |
#<Set> |
In the first example, insert adds the 10 to (set < 1 2 3 4 5 6). In the second example, it just returns (set < 1 2 3 4 5 6).
(member? set) → Boolean |
set : (Set A) |
Examples: |
> (member? 5 (set < 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (member? 10 (set < 1 2 3 4 5 6)) |
- : Boolean |
#f |
(subset? set1 set2) → Boolean |
set1 : (Set A) |
set2 : (Set A) |
Examples: |
> (subset? (set < 1 2 3 4 5 6) (set < 7 8 9)) |
- : Boolean |
#f |
> (subset? (set < 1 2 3 4 5 6) (set < 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (subset? (set < 1 2 3 4 5 6) (set < 2 3)) |
- : Boolean |
#t |
> (subset? (set < 1 2 3 4 5 6) ((inst set Positive-Fixnum) <)) |
- : Boolean |
#t |
(union set1 set2) → (Set A) |
set1 : (Set A) |
set2 : (Set A) |
Examples: |
> (union (set < 1 2 3 4 5 6) (set < 7 8 9)) |
- : (Set Positive-Fixnum) |
#<Set> |
> (union (set < 1 2 3 4 5 6) (set < 1 2 3 4 5 6)) |
- : (Set Positive-Fixnum) |
#<Set> |
In the first example, union returns (set < 1 2 3 4 5 6 7 8 9), and in the second example it returns (set < 1 2 3 4 5 6)
(intersection set1 set2) → (Set A) |
set1 : (Set A) |
set2 : (Set A) |
Examples: |
> (intersection (set < 1 2 3 4 5 6) (set < 7 8 9)) |
- : (Set Positive-Fixnum) |
#<Set> |
> (intersection (set < 1 2 3 4 5 6) (set < 1 2 3 4 5 6)) |
- : (Set Positive-Fixnum) |
#<Set> |
> (intersection (set < 1 2 3 4 5 6) (set < 2 3 10 15)) |
- : (Set Positive-Fixnum) |
#<Set> |
In the first example, intersection returns a empty set, and in the second example it returns (set < 1 2 3 4 5 6) and in the third, it returns (set < 2 3)
(difference set1 set2) → (Set A) |
set1 : (Set A) |
set2 : (Set A) |
Examples: |
> (difference (set < 1 2 3 4 5 6) (set < 7 8 9)) |
- : (Set Positive-Fixnum) |
#<Set> |
> (difference (set < 1 2 3 4 5 6) (set < 1 2 3 4 5 6)) |
- : (Set Positive-Fixnum) |
#<Set> |
> (difference (set < 1 2 3 4 5 6) (set < 2 3 10 15)) |
- : (Set Positive-Fixnum) |
#<Set> |
In the first example, difference returns (set < 1 2 3 4 5 6), and in the second example it returns a empty set and in the third, it returns (set < 1 4 5 6)
(set->list set) → (Listof A) |
set : (Set A) |
Example: |
> (set->list (set > 1 2 3 4 5)) |
- : (Listof Positive-Fixnum) |
'(5 4 3 2 1) |
(map comparer func set1 set2 ...) → (Set A) |
comparer : (C C -> Boolean) |
func : (A B ... B -> C) |
set1 : (Set A) |
set2 : (Set B) |
Examples: | |||
> (set->list (map < add1 (set < 1 2 3 4 5 6))) | |||
- : (Listof Exact-Positive-Integer) | |||
'(2 3 4 5 6 7) | |||
| |||
- : (Listof Exact-Positive-Integer) | |||
'(1 4 9 16 25 36) |
(filter func set) → (Set A) |
func : (A -> Boolean) |
set : (Set A) |
Examples: | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(1 2 3 4) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(1 2 3 4 5) |
(remove func set) → (Set A) |
func : (A -> Boolean) |
set : (Set A) |
Examples: | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(1 2 3 4 5) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(5 6) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6) |