3.15 Dictionaries
A dictionary is an instance of a datatype that maps keys to values. The following datatypes are all dictionaries:
vectors (using only exact integers as keys);
lists of pairs (an association list using equal? to compare keys); and
structures whose types implement the gen:dict generic interface.
A dictionary can be used as a two-valued sequence (see Sequences). The associations of the dictionary serve as elements of the sequence. See also in-dict, in-dict-keys, and in-dict-values.
Beware that dict? is not a constant-time test on pairs, since checking that v is an association list may require traversing the list.
Examples: | ||||||||
|
procedure
(dict-mutable? d) → boolean?
d : dict?
Examples: | ||||||||||
|
procedure
d : dict?
Examples: | ||||||
|
procedure
d : dict?
Examples: | ||||||||
|
Examples: | ||||||||||||||||
|
procedure
(dict-set*! dict key v ... ...) → void?
dict : (and/c dict? (not/c immutable?)) key : any/c v : any/c
Examples: | ||||||||||||||||||||||||
|
procedure
(dict-set dict key v) → (and/c dict? immutable?)
dict : (and/c dict? immutable?) key : any/c v : any/c
Examples: | ||||||||
|
procedure
(dict-set* dict key v ... ...) → (and/c dict? immutable?)
dict : (and/c dict? immutable?) key : any/c v : any/c
Examples: | ||||||||||
|
procedure
(dict-has-key? dict key) → boolean?
dict : dict? key : any/c
Examples: | ||||||||||||
|
procedure
dict : dict? key : any/c
failure-result : any/c = (lambda () (raise (make-exn:fail ....)))
If failure-result is a procedure, it is called (through a tail call) with no arguments to produce the result.
Otherwise, failure-result is returned as the result.
Examples: | |||||||||||||||||||||||||
|
Examples: | |||||||||||||||||||||
|
procedure
(dict-update! dict key updater [ failure-result]) → void? dict : (and/c dict? (not/c immutable?)) key : any/c updater : (any/c . -> . any/c)
failure-result : any/c = (lambda () (raise (make-exn:fail ....)))
Examples: | |||||||||||||||||||
|
procedure
(dict-update dict key updater [failure-result])
→ (and/c dict? immutable?) dict : dict? key : any/c updater : (any/c . -> . any/c)
failure-result : any/c = (lambda () (raise (make-exn:fail ....)))
Examples: | |||||||
|
procedure
(dict-remove! dict key) → void?
dict : (and/c dict? (not/c immutable?)) key : any/c
Examples: | |||||||||||||
|
procedure
(dict-remove dict key) → (and/c dict? immutable?)
dict : (and/c dict? immutable?) key : any/c
Examples: | ||||||||||||||||
|
Example: | ||
|
Example: | |||||||||
|
procedure
(dict-count dict) → exact-nonnegative-integer?
dict : dict?
Examples: | ||||
|
procedure
(dict-iterate-first dict) → any/c
dict : dict?
Examples: | ||||||||
|
procedure
(dict-iterate-next dict pos) → any/c
dict : dict? pos : any/c
Examples: | ||||||||||||
|
procedure
(dict-iterate-key dict pos) → any
dict : dict? pos : any/c
Examples: | ||||||||||
|
procedure
(dict-iterate-value dict pos) → any
dict : dict? pos : any/c
Examples: | ||||||||||
|
Examples: | |||||||
|
procedure
(in-dict-keys dict) → sequence?
dict : dict?
Examples: | |||||||
|
procedure
(in-dict-values dict) → sequence?
dict : dict?
Examples: | |||||||
|
procedure
(in-dict-pairs dict) → sequence?
dict : dict?
Examples: | |||||||
|
Examples: | |||||
|
procedure
(dict-values dict) → list?
dict : dict?
Examples: | |||||
|
procedure
(dict->list dict) → list?
dict : dict?
Examples: | |||||
|
dict-ref : accepts either two or three arguments
dict-set! : accepts three arguments, left unimplemented if mutation is not supported
dict-set : accepts three arguments and returns an updated dictionary, left unimplemented if functional update is not supported
dict-remove! : accepts two arguments, left unimplemented if mutation is not supported or if key removal is not supported
dict-remove : accepts two arguments and returns an updated dictionary, left unimplemented if functional update or key removal is not supported
dict-count : accepts one argument
dict-iterate-first : accepts one argument
dict-iterate-next : accepts two arguments; the procedure is responsible for checking that the second argument is a valid position for the first argument
dict-iterate-key : accepts two arguments; the procedure is responsible for checking that the second argument is a valid position for the first argument
dict-iterate-value : accepts two arguments; the procedure is responsible for checking that the second argument is a valid position for the first argument
Examples: | |||||||||||||||||||||||||
|
value
(list dict-vector (vector type-key-contract type-value-contract type-iter-contract instance-key-contract instance-value-contract instance-iter-contract))
The first vector must be a vector of 10 procedures which match the gen:dict generic interface (in addition, it must be an immutable vector). The second vector must contain six elements; each of the first three is a contract for the dictionary type’s keys, values, and positions, respectively. Each of the second three is either #f or a procedure used to extract the contract from a dictionary instance.
procedure
(dict-key-contract d) → contract?
d : dict?
procedure
(dict-value-contract d) → contract?
d : dict?
procedure
(dict-iter-contract d) → contract?
d : dict?
procedure
(make-custom-hash eql? hash-proc [hash2-proc]) → dict?
eql? : (any/c any/c . -> . any/c) hash-proc : (any/c . -> . exact-integer?)
hash2-proc : (any/c . -> . exact-integer?) = (lambda (v) 10001)
procedure
(make-immutable-custom-hash eql? hash-proc [ hash2-proc]) → dict? eql? : (any/c any/c . -> . any/c) hash-proc : (any/c . -> . exact-integer?)
hash2-proc : (any/c . -> . exact-integer?) = (lambda (v) 10001)
procedure
(make-weak-custom-hash eql? hash-proc [ hash2-proc]) → dict? eql? : (any/c any/c . -> . any/c) hash-proc : (any/c . -> . exact-integer?)
hash2-proc : (any/c . -> . exact-integer?) = (lambda (v) 10001)
The make-custom-hash and make-weak-custom-hash functions create a mutable dictionary that does not support functional update, while make-immutable-custom-hash creates an immutable dictionary that supports functional update. The dictionary created by make-weak-custom-hash retains its keys weakly, like the result of make-weak-hash.
Dictionaries created by make-custom-hash and company are equal? when they have the same mutability and key strength, the associated procedures are equal?, and the key–value mappings are the same when keys and values are compared with equal?.
Examples: | ||||||||||||||
|