Go to the first, previous, next, last section, table of contents.

Hash Tables

Like the association list functions, the hash table functions come in several varieties: hashq, hashv, and hash. The hashq functions use eq? to determine whether two keys match. The hashv functions use eqv?, and the hash functions use equal?.

In each of the functions that follow, the table argument must be a vector. The key and value arguments may be any Scheme object.

primitive: hashq-ref table key [default]
primitive: hashv-ref table key [default]
primitive: hash-ref table key [default]
Look up key in the hash table table, and return the value (if any) associated with it. If key is not found, return default (or #f if no default argument is supplied).

primitive: hashq-set! table key value
primitive: hashv-set! table key value
primitive: hash-set! table key value
Find the entry in table associated with key, and store value there.

primitive: hashq-remove! table key
primitive: hashv-remove! table key
primitive: hash-remove! table key
Remove key (and any value associated with it) from table.

The standard hash table functions may be too limited for some applications. For example, you may want a hash table to store strings in a case-insensitive manner, so that references to keys named "foobar", "FOOBAR" and "FooBaR" will all yield the same item. Guile provides you with extended hash tables that permit you to specify a hash function and associator function of your choosing. The functions described in the rest of this section can be used to implement such custom hash table structures.

If you are unfamiliar with the inner workings of hash tables, then this facility will probably be a little too abstract for you to use comfortably. If you are interested in learning more, see an introductory textbook on data structures or algorithms for an explanation of how hash tables are implemented.

primitive: hashq key size
primitive: hashv key size
primitive: hash key size
Default hash functions for Guile hash tables. key is the object to be hashed, and size is the size of the target hash table. Each function returns an integer in the range 0 to size-1.

primitive: hashx-ref hasher assoc table key [default]
primitive: hashx-set! hasher assoc table key value
primitive: hashx-remove! hasher assoc table key
These behave the same way as the corresponding ref and set! functions described above, but use hasher as a hash function and assoc to compare keys. hasher must be a function that takes two arguments, a key to be hashed and a table size. assoc must be an associator function, like assoc, assq or assv.

By way of illustration, hashq-ref table key is equivalent to hashx-ref hashq assq table key.

primitive: hashq-get-handle table key
primitive: hashv-get-handle table key
primitive: hash-get-handle table key
primitive: hashx-get-handle hasher assoc table key
These procedures are similar to their -ref cousins, but return a handle from the hash table rather than the value associated with key. By convention, a handle in a hash table is the pair which associates a key with a value. Where hashq-ref table key returns only a value, hashq-get-handle table key returns the pair (key . value).

primitive: hashq-create-handle! table key init
primitive: hashv-create-handle! table key init
primitive: hash-create-handle! table key init
primitive: hashx-create-handle! hasher assoc table key init
These functions look up key in table and return its handle, If key is not already present, a new handle is created which associates key with init.

Go to the first, previous, next, last section, table of contents.