4 Collection Datatypes
4.1 Dictionaries
| (require (planet cce/scheme:6:1/dict)) |
This module provides tools for manipulating dictionary values.
4.1.1 Dictionary Lookup
Examples: | ||
| ||
| > (dict-set! d 1 'one) | ||
| > (dict-set! d 2 'two) | ||
| > d | ||
#hash((1. one) (2. two)) | ||
| > (dict-ref! d 2 'dos) | ||
two | ||
| > d | ||
#hash((1. one) (2. two)) | ||
| > (dict-ref! d 3 'tres) | ||
tres | ||
| > d | ||
#hash((1. one) (2. two) (3. tres)) | ||
| > (dict-ref! d 4 gensym) | ||
g1266 | ||
| > d | ||
#hash((1. one) (2. two) (3. tres) (4. g1266)) |
| (dict-ref/check d k) → any/c |
| d : dict? |
| k : (lambda (k) (dict-has-key? d k)) |
Example: |
| > (dict-ref/check '([1 . one] [2 . two] [3 . three]) 2) |
two |
| (dict-ref/identity d k) → any/c |
| d : dict? |
| k : any/c |
Examples: |
| > (dict-ref/identity '([1 . one] [2 . two] [3 . three]) 2) |
two |
| > (dict-ref/identity '([1 . one] [2 . two] [3 . three]) 4) |
4 |
| (dict-ref/default d k v) → any/c |
| d : dict? |
| k : any/c |
| v : any/c |
Examples: |
| > (dict-ref/default '([1 . one] [2 . two] [3 . three]) 2 'other) |
two |
| > (dict-ref/default '([1 . one] [2 . two] [3 . three]) 4 'other) |
other |
Examples: |
| > (dict-ref/failure '([1 . one] [2 . two] [3 . three]) 2 gensym) |
two |
| > (dict-ref/failure '([1 . one] [2 . two] [3 . three]) 4 gensym) |
g1376 |
4.1.2 Dictionary Accessors
| (dict-has-key? d k) → boolean? |
| d : dict? |
| k : any/c |
Examples: |
| > (dict-has-key? '([1 . one] [2 . two] [3 . three]) 2) |
#t |
| > (dict-has-key? '([1 . one] [2 . two] [3 . three]) 4) |
#f |
| (dict-domain d) → list? |
| d : dict? |
Example: |
| > (dict-domain '([1 . one] [2 . two] [3 . three])) |
(1 2 3) |
| (dict-range d) → list? |
| d : dict? |
Example: |
| > (dict-range '([1 . one] [2 . two] [3 . three])) |
(one two three) |
4.1.3 Dictionary Combinations
| (dict-union d0 d ... [#:combine combine]) | ||||||||||||
| → (and/c dict? dict-can-functional-set?) | ||||||||||||
| d0 : (and/c dict? dict-can-functional-set?) | ||||||||||||
| d : dict? | ||||||||||||
|
Examples: | |||
| > (dict-union '([1 . one]) '([2 . two]) '([3 . three])) | |||
((1 . one) (2 . two) (3 . three)) | |||
| |||
((1 one uno ein une) (2 two dos zwei deux)) |
| (dict-union! d0 d ... [#:combine combine]) → void? | ||||||||||||
| d0 : (and/c dict? dict-mutable?) | ||||||||||||
| d : dict? | ||||||||||||
|
Examples: | |||
| |||
| > d | |||
#hash() | |||
| > (dict-union! d '([1 one uno] [2 two dos])) | |||
| > d | |||
#hash((1oneuno) (2twodos)) | |||
| |||
| > d | |||
#hash((1oneunoeinune) (2twodoszweideux)) |
4.1.4 Dictionary Contract
The wrapped dictionary will support the same set of dictionary operations as the base dictionary, and respond identically to dict-mutable?, dict-can-remove-keys?, and dict-can-functional-set?.
Warning: Bear in mind that these checks are perfomed on every dictionary operation, and dictionaries wrapped in dict/c multiple times will perform the checks as many times for each operation. Especially for immutable dictionaries (which may be passed through a constructor that involves dict/c on each update), contract-wrapped dictionaries may be much less efficient than the original dictionaries.
4.2 Hash Tables
| (require (planet cce/scheme:6:1/hash)) |
This module provides tools for manipulating hash tables.
4.2.1 Hash Table Lookup
| (hash-ref/check h k) → any/c |
| h : hash? |
| k : (lambda (k) (hash-has-key? h k)) |
Example: |
| > (hash-ref/check (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2) |
two |
| (hash-ref/identity h k) → any/c |
| h : hash? |
| k : any/c |
Examples: |
| > (hash-ref/identity (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2) |
two |
| > (hash-ref/identity (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4) |
4 |
| (hash-ref/default h k v) → any/c |
| h : hash? |
| k : any/c |
| v : any/c |
Examples: |
| > (hash-ref/default (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 'other) |
two |
| > (hash-ref/default (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 'other) |
other |
Examples: |
| > (hash-ref/failure (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2 gensym) |
two |
| > (hash-ref/failure (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4 gensym) |
g1103 |
4.2.2 Hash Table Accessors
| (hash-has-key? h k) → boolean? |
| h : hash? |
| k : any/c |
Examples: |
| > (hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 2) |
#t |
| > (hash-has-key? (make-immutable-hash '([1 . one] [2 . two] [3 . three])) 4) |
#f |
| (hash-domain h) → list? |
| h : hash? |
Example: |
| > (hash-domain (make-immutable-hash '([1 . one] [2 . two] [3 . three]))) |
(1 2 3) |
| (hash-range h) → list? |
| h : hash? |
Example: |
| > (hash-range (make-immutable-hash '([1 . one] [2 . two] [3 . three]))) |
(one two three) |
4.2.3 Hash Table Combinations
| (hash-union h0 h ... [#:combine combine]) | ||||||||||||
| → (and/c hash? hash-can-functional-set?) | ||||||||||||
| h0 : (and/c hash? hash-can-functional-set?) | ||||||||||||
| h : hash? | ||||||||||||
|
Examples: | |||
| > (hash-union (make-immutable-hash '([1 . one])) (make-immutable-hash '([2 . two])) (make-immutable-hash '([3 . three]))) | |||
#hash((1. one) (2. two) (3. three)) | |||
| |||
#hash((1oneunoeinune) (2twodoszweideux)) |
| (hash-union! h0 h ... [#:combine combine]) → void? | ||||||||||||
| h0 : (and/c hash? hash-mutable?) | ||||||||||||
| h : hash? | ||||||||||||
|
Examples: | |||
| |||
| > h | |||
#hash() | |||
| > (hash-union! h (make-immutable-hash '([1 one uno] [2 two dos]))) | |||
| > h | |||
#hash((1oneuno) (2twodos)) | |||
| |||
| > h | |||
#hash((1oneunoeinune) (2twodoszweideux)) |
4.3 Imperative Queues
| (require (planet cce/scheme:6:1/queue)) |
This module provides a mutable queue representation.
| (make-queue) → queue/c |
| (dequeue! q) → any/c |
| q : nonempty-queue/c |
Examples: | ||
| ||
| > (enqueue! q 1) | ||
| > (dequeue! q) | ||
1 | ||
| > (enqueue! q 2) | ||
| > (enqueue! q 3) | ||
| > (dequeue! q) | ||
2 | ||
| > (dequeue! q) | ||
3 |
| (queue-empty? q) → boolean? |
| q : queue/c |
Examples: | ||
| ||
| > (queue-empty? q) | ||
#t | ||
| > (enqueue! q 1) | ||
| > (queue-empty? q) | ||
#f | ||
| > (dequeue! q) | ||
1 | ||
| > (queue-empty? q) | ||
#t |
Examples: |
| > (queue? (make-queue)) |
#t |
| > (queue? 'not-a-queue) |
#f |