10 Sets
| (require (planet krhari/pfds:1:4/set)) |
An simple implementation of sets based on binary trees.
| (Set 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.
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).
Examples: |
| > (member? 5 (set < 1 2 3 4 5 6)) |
- : Boolean |
#t |
| > (member? 10 (set < 1 2 3 4 5 6)) |
- : Boolean |
#f |
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 |
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)
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) |
Examples: | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(1 2 3 4) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(1 2 3 4 5) |
Examples: | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(1 2 3 4 5) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(5 6) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6) |