On this page:
hash?
hash-eqv?
hash-eq?
hash-weak?
make-hash
make-hasheqv
make-hasheq
make-weak-hash
make-weak-hasheqv
make-weak-hasheq
make-immutable-hash
make-immutable-hasheqv
make-immutable-hasheq
hash-set!
hash-set
hash-ref
hash-ref!
hash-has-key?
hash-update!
hash-update
hash-remove!
hash-remove
hash-map
hash-for-each
hash-count
hash-iterate-first
hash-iterate-next
hash-iterate-key
hash-iterate-value
hash-copy
eq-hash-code
eqv-hash-code
equal-hash-code
equal-secondary-hash-code
Version: 4.2.1

3.13 Hash Tables

Hash Tables in Guide: PLT Scheme introduces hash tables.

A hash table (or simply hash) maps each of its keys to a single value. For a given hash table, keys are equivalent via equal?, eqv?, or eq?, and keys are retained either strongly or weakly (see Weak Boxes). A hash table is also either mutable or immutable. Immutable tables support constant-time access and update, just like mutable hash tables; the constant on immutable operations is usually larger, but the functional nature of immutable hash tables can pay off in certain algorithms.

A hash table can be used as a two-valued sequence (see Sequences). The keys and values of the hash table serve as elements of the sequence (i.e., each element is a key and its associated value). If a mapping is added to or removed from the hash table during iteration, then an iteration step may fail with exn:fail:contract, or the iteration may skip or duplicate keys and values. See also in-hash, in-hash-keys, in-hash-values, and in-hash-pairs.

Two hash tables cannot be equal? unless they use the same key-comparison procedure (equal?, eqv?, or eq?), both hold keys strongly or weakly, and have the same mutability.

Caveats concerning concurrent modification: A mutable hash table can be manipulated with hash-ref, hash-set!, and hash-remove! concurrently by multiple threads, and the operations are protected by a table-specific semaphore as needed. Three caveats apply, however:

Caveat concerning mutable keys: If a key in an equal?-based hash table is mutated (e.g., a key string is modified with string-set!), then the hash table’s behavior for insertion and lookup operations becomes unpredictable.

(hash? v)  boolean?
  v : any/c
Returns #t if v is a hash table, #f otherwise.

(hash-eqv? hash)  boolean?
  hash : hash?
Returns #t if hash compares keys with eqv?, #f if it compares with equal? or eq?.

(hash-eq? hash)  boolean?
  hash : hash?
Returns #t if hash compares keys with eq?, #f if it compares with equal? or eqv?.

(hash-weak? hash)  boolean?
  hash : hash?
Returns #t if hash retains its keys weakly, #f if it retains keys strongly.

Creates an empty mutable hash table that holds keys strongly and that uses equal? to compare keys. See also make-custom-hash.

Creates an empty mutable hash table that holds keys strongly and that uses eqv? to compare keys.

Creates an empty mutable hash table that holds keys strongly and that uses eq? to compare keys.

Creates an empty mutable hash table that holds keys weakly and that uses equal? to compare keys. See also make-weak-custom-hash.

Creates an empty mutable hash table that holds keys weakly and that uses eqv? to compare keys.

Creates an empty mutable hash table that holds keys weakly and that uses eq? to compare keys.

(make-immutable-hash assocs)  (and/c hash? immutable?)
  assocs : (listof pair?)
Creates an immutable hash table that compares keys with equal?. In each element of assocs, the car of each pair is a key, and the cdr is the corresponding value. The mappings are added to the table in the order that they appear in assocs, so later mappings can hide earlier mappings.

(make-immutable-hasheqv assocs)
  (and/c hash? hash-eqv? immutable?)
  assocs : (listof pair?)
Like make-immutable-hash, but the resulting hash table compares keys with eqv?.

(make-immutable-hasheq assocs)
  (and/c hash? hash-eq? immutable?)
  assocs : (listof pair?)
Like make-immutable-hash, but the resulting hash table compares keys with eq?.

(hash-set! hash key v)  void?
  hash : (and/c hash? (not/c immutable?))
  key : any/c
  v : any/c
Maps key to v in hash, overwriting any existing mapping for key.

See also the caveats concerning concurrent modification and the caveat concerning mutable keys above.

(hash-set hash key v)  (and/c hash? immutable?)
  hash : (and/c hash? immutable?)
  key : any/c
  v : any/c
Functionally extends hash by mapping key to v, overwriting any existing mapping for key, and returning the extended hash table.

See also the caveat concerning mutable keys above.

(hash-ref hash key [failure-result])  any
  hash : hash?
  key : any/c
  failure-result : any/c
   = 
(lambda ()
  (raise (make-exn:fail:contract ....)))
Returns the value for key in hash. If no value is found for key, then failure-result determines the result:

See also the caveats concerning concurrent modification and the caveat concerning mutable keys above.

(hash-ref! hash key to-set)  any
  hash : hash?
  key : any/c
  to-set : any/c
Returns the value for key in hash. If no value is found for key, then to-set determines the result as in hash-ref (i.e., it is either a thunk that computes a value or a plain value), and this result is stored in hash for the key. (Note that if to-set is a thunk, it is not invoked in tail position.)

See also the caveats concerning concurrent modification and the caveat concerning mutable keys above.

(hash-has-key? hash key)  boolean?
  hash : hash?
  key : any/c
Returns #t if hash contains a value for the given key, #f otherwise.

(hash-update! hash    
  key    
  updater    
  [failure-result])  void?
  hash : (and/c hash? (not/c immutable?))
  key : any/c
  updater : (any/c . -> . any/c)
  failure-result : any/c
   = 
(lambda ()
  (raise (make-exn:fail:contract ....)))
Composes hash-ref and hash-set! to update an existing mapping in hash, where the optional failure-result argument is used as in hash-ref when no mapping exists for key already. See the caveat above about concurrent updates.

See also the caveats concerning concurrent modification and the caveat concerning mutable keys above.

(hash-update hash key updater [failure-result])
  (and/c hash? immutable?)
  hash : (and/c hash? immutable?)
  key : any/c
  updater : (any/c . -> . any/c)
  failure-result : any/c
   = 
(lambda ()
  (raise (make-exn:fail:contract ....)))
Composes hash-ref and hash-set to functionally update an existing mapping in hash, where the optional failure-result argument is used as in hash-ref when no mapping exists for key already.

See also the caveat concerning mutable keys above.

(hash-remove! hash key)  void?
  hash : (and/c hash? (not/c immutable?))
  key : any/c
Removes any existing mapping for key in hash.

See also the caveats concerning concurrent modification and the caveat concerning mutable keys above.

(hash-remove hash key)  (and/c hash? immutable?)
  hash : (and/c hash? immutable?)
  key : any/c
Functionally removes any existing mapping for key in hash, returning the fresh hash table.

See also the caveat concerning mutable keys above.

(hash-map hash proc)  (listof any/c)
  hash : hash?
  proc : (any/c any/c . -> . any/c)
Applies the procedure proc to each element in hash in an unspecified order, accumulating the results into a list. The procedure proc is called each time with a key and its value. See the caveat above about concurrent modification.

See also the caveats concerning concurrent modification above.

(hash-for-each hash proc)  void?
  hash : hash?
  proc : (any/c any/c . -> . any)
Applies proc to each element in hash (for the side-effects of proc) in an unspecified order. The procedure proc is called each time with a key and its value. See the caveat above about concurrent modification.

See also the caveats concerning concurrent modification above.

(hash-count hash)  exact-nonnegative-integer?
  hash : hash?
Returns the number of keys mapped by hash. If hash is not created with 'weak, then the result is computed in constant time and atomically. If hash is created with 'weak, see the caveats concerning concurrent modification above.

(hash-iterate-first hash)
  (or/c #f exact-nonnegative-integer?)
  hash : hash?
Returns #f if hash contains no elements, otherwise it returns an integer that is a index to the first element in the hash table; “first” refers to an unspecified ordering of the table elements, and the index values are not necessarily consecutive integers. For a mutable hash, this index is guaranteed to refer to the first item only as long as no items are added to or removed from hash.

(hash-iterate-next hash pos)
  (or/c #f exact-nonnegative-integer?)
  hash : hash?
  pos : exact-nonnegative-integer?
Returns either an integer that is an index to the element in hash after the element indexed by pos (which is not necessarily one more than pos) or #f if pos refers to the last element in hash. If pos is not a valid index, then the exn:fail:contract exception is raised. For a mutable hash, the result index is guaranteed to refer to its item only as long as no items are added to or removed from hash.

(hash-iterate-key hash pos)  any
  hash : hash?
  pos : exact-nonnegative-integer?
Returns the key for the element in hash at index pos. If pos is not a valid index for hash, the exn:fail:contract exception is raised.

(hash-iterate-value hash pos)  any
  hash : hash?
  pos : exact-nonnegative-integer?
Returns the value for the element in hash at index pos. If pos is not a valid index for hash, the exn:fail:contract exception is raised.

(hash-copy hash)  (and/c hash? (not/c immutable?))
  hash : hash?
Returns a mutable hash table with the same mappings, same key-comparison mode, and same key-holding strength as hash.

(eq-hash-code v)  exact-integer?
  v : any/c
Returns an exact integer; for any two eq? values, the returned integer is the same. Furthermore, for the result integer k and any other exact integer j, (= k j) implies (eq? k j).

(eqv-hash-code v)  exact-integer?
  v : any/c
Returns an exact integer; for any two eqv? values, the returned integer is the same. Furthermore, for the result integer k and any other exact integer j, (= k j) implies (eq? k j).

Returns an exact integer; for any two equal? values, the returned integer is the same. Furthermore, for the result integer k and any other exact integer j, (= k j) implies (eq? k j). A has code is computed even when v contains a cycle through pairs, vectors, boxes, and/or inspectable structure fields. See also prop:equal+hash.

Like equal-hash-code, but computes a secondary value suitable for use in double hashing.