R7RS-large defines hashtable (
see R7RS hash tables) but its API is not completely consistent
with Gauche’s original hashtables and other native APIs.
Rather than mixing different flavor of APIs, we keep Gauche’s native
API consistent, and provide R7RS procedures that are inconsistent
with aliases—specifically, those procedures are suffixed with
gauche module. For portable programs, you can import
scheme.hash-table to get R7RS names.
Hash table class. Inherits
Gauche doesn’t provide immutable hash tables for now. (If you need immutable maps, see Immutable map).
#t iff obj is a hash table.
#t iff a hash table ht is mutable.
Gauche doesn’t have immutable hash tables, so this procedure always
#t for any hash tables.
Returns a comparator used in the hashtable ht.
This is an old API, superseded by
Returns one of symbols
general, indicating the type of the hash table ht.
Return the number of entries in the hash table ht.
R7RS name is
Creates a hash table. The comparator argument
specifies key equality and hash function using a comparator
(see Basic comparators). If omitted,
is used. Note that in R7RS, comparator
argument can’t be omitted.
As Gauche’s extension, the comparator argument can also
be one of the symbols
If it is one of those symbols,
string-comparator will be used, respectively.
The comparator must have hash function, of course. See Hashing, for the built-in hash functions. In general, comparators derived from other comparators having hash functions also have appropriate hash functions.
Constructs and returns a hash table from given
list of arguments.
The comparator argument is the same as of
Each key&value must be a pair, and its car is used as a key
and its cdr is used as a value.
Note: This is called
hash-table by 0.9.5. R7RS introduced
a procedure with the same name, but different interface.
We see R7RS version makes more sense, so we’ll eventually switch to it,
but the transition will take long time.
The R7RS interface is available as
hash-table-r7, and we urge you to use it in the new code,
and replace existing
(hash-table-from-pairs 'eq? '(a . 1) '(b . 2)) ≡ (rlet1 h (make-hash-table 'eq?) (hash-table-put! h 'a 1) (hash-table-put! h 'b 2))
An alias of
hash-table-from-pairs above. R7RS introduced the same name
procedure with different interface (see
hash-table-r7 below), and
we’d like to switch to it in future. For now, use either
scheme.hash-table and write in R7RS.
Create and returns a hash table using comparator. The args … are the contents, alternating keys and values.
This is defined as
hash-table in R7RS
(see R7RS hash tables).
(hash-table-r7 'eq? 'a 1 'b 2) ≡ (rlet1 h (make-hash-table 'eq?) (hash-table-put! h 'a 1) (hash-table-put! h 'b 2))
Note: An R7RS compliant implementation of
may return an immutable hash table. Since Gauche doesn’t have
immutable hash tables (we have immutable maps instead; see Immutable map),
we return mutable hash tables. However, the portable program should
refrain from mutating the returned hash tables.
[R7RS hash-table] Constructs and returns a new hash table with those repetitive steps. Each iteration keeps the current seed value, whose initial value is seed.
[R7RS hash-table] Returns a new copy of a hash table ht.
R7RS defines this procedure to return an immutable hash table if the implementation supports one, unless the optional mutable? argument is provided and not false. Gauche doesn’t have immutable hash tables so it ignores the optional argument and always returns a mutable hash table. But when you write a portable programs, keep it in mind.
[R7RS hash-table] Returns a new mutable empty hash table that has the same properties as the given hash table ht.
Creates and returns a hash table that has entries of
each element in alist, using its car as the key and
its cdr as the value. The comparator argument
is the same as in
make-hash-table. The default value
of comparator is
R7RS doesn’t allow to omit comparator.
(hash-table-map h cons)
Search key from a hash table ht, and returns its value if found. If the key is not found in the table and default is given, it is returned. Otherwise an error is signaled.
Puts a key key with a value value to the hash table ht.
Method versions of
[R7RS hash-table] This is R7RS way to look up a hash table.
Look up a value associated to the key in the table ht, then pass it to a procedure success, and returns its value. If success is omitted, an identity function is used.
If there’s no association for key in ht, a thunk failure is called and its result is returned. The default value of failure throws an error.
It is more general than Gauche’s
hash-table-get, but if you
need to simply return a fallback value in case of failure, you need
to wrap it with a clojure, which is annoying. In R7RS, you can
[R7RS hash-table] Looks up key in a hash table ht and returns the associated value. If there’s no key in the table, returns default.
This is same as Gauche’s
hash-table-get, except that
default is not optional. We provide both, for
hash-table-get is short and handy.
This is R7RS version to put associations into a hash table.
The args … is a list of alternating keys and values; so,
hash-table-put!, you can insert more than
one associations at once. It is an error if args … have
odd number of arguments.
(hash-table-set! ht 'a 1 'b 2) ≡ (begin (hash-table-put! ht 'a 1) (hash-table-put! ht 'b 2))
This is defined in R7RS as
hash-table-intern!. We add
suffix to remind that it takes a failure thunk,
which is consistent with R7RS hash-table interface but not Gauche’s way.
Lookup key in ht. If there’s already an entry, it just returns the value. Otherwise, it calls a thunk failure, and insert the association of key and the return value of failure into ht, and returns the value.
#t if a hash table ht has a key key.
R7RS name is
Deletes an entry that has a key key from the hash table ht.
#t if the entry has exist, or
#f if the entry
hasn’t exist. The same function is called
hash-table-remove! in STk
(except that it returns an undefined value); I use ‘delete’ for consistency
to SRFI-1, SRFI-13 and other parts of the libraries.
Note: This is different from R7RS
hash-table-delete!, so we provide
R7RS interface with an alias
Delets entries that have key … from the hash table ht. The key which isn’t in ht has no effect. Returns the number of entries actually deleted.
This is called
hash-table-delete! in R7RS, and so as in
scheme.hash-table. We provide this under different name,
hash-table-delete! returns a boolean value.
[R7RS hash-table] Removes all entries in the hash table ht.
Conses value to the existing value for the key key in the
hash table ht and makes it the new value for key.
If there’s no entry for key, an entry
is created with the value
Works the same as the following code, except that this function only looks up the key once, thus it’s more efficient.
(hash-table-put! ht key (cons value (hash-table-get ht key '())))
Looks for the value for the key key in the hash table ht. If found and it is a pair, replaces the value for its cdr and returns car of the original value. If no entry for key is in the table, or the value is not a pair, the table is not modified and the procedure returns default if given, or signals an error otherwise.
During the operation the key is looked for only once, thus runs efficiently.
Note: R7RS has
hash-table-pop! but its totally different.
We provide R7RS version as an alias
Removes one arbitrary entry from ht, and returns the removed entry’s key and value as two values. If ht is empty, an error is thrown.
This is called
hash-table-pop! in R7RS, and so as in
A more general version of
It works basically as the following code piece,
except that the lookup of key is only done once.
(let ((tmp (proc (hash-table-get ht key default)))) (hash-table-put! ht key tmp) tmp)
For example, when you use a hash table to count the occurrences of items, the following line is suffice to increment the counter of the item, regardless of whether item has already appeared or not.
(hash-table-update! ht item (cut + 1 <>) 0))
hash-table-update! with different interface,
so we provide R7RS version as an alias
This is R7RS version of
hash-table-update!. With no optional
arguments, it works like Gauche’s
hash-table-update!. But in
practice you often needs to specify the behavior when key hasn’t
been in ht, in which case R7RS differs from Gauche.
The R7RS version works like this but potentially more efficiently:
(hash-table-put! ht key (updater (hash-table-ref-r7 ht key failure success)))
This is the same as Gauche’s
hash-table-default!, except that
the default value can’t be omitted.
A procedure proc is called with two arguments, a key and its associated value, over all the entries in the hash table ht.
For all entries in the hash table ht,
a procedure kons is called with three arguments;
a key, its associated value, and the previous return value of kons.
The first call of kons receives knil as the third argument.
The return value of the last call of kons is returned
Apply pred with each key and value in the hash table ht.
Once pred returns a true value, that return value is immediately returned
If no key-value satisfies pred, a thunk failure is
invoked and its result is returned. If failure is
(lambda () #f) is assumed.
Note: The convention starting from srfi-1 is that
*-find returns an item in the collection that
satisfy the predicate, while
*-any returns a non-false
value the predicate returns. SRFI-125 broke the convention.
The justification given in SRFI-125 discussion
was that the “any” semantics is strictly upper-compatible to the
“find” semantics so we can combine two. So far, though, SRFI-125
is the only exception of this convention.
;; Find if hash tables ha and hb has a common key. (hash-table-find ha (^[k v] (hash-table-exists? hb k)))
Returns all the keys or values of hash table ht in a list, respectively.
A hash table can be viewed as a set of pairs of key and value. This procedure compares two hash tables ht1 and ht2 as such sets.
The key comparators of two tables must match (in terms of
of the comparators). Otherwise, an error is signaled.
Two elements of the set are equal to each other iff their keys match
with the equality predicate of the key comparator, and their values
match with value=? procedure. If omitted,
used for value=?
There can be four cases.
[R7RS hash-table] This also compares two hash tables ht1 and ht2 as sets, and returns true iff two are the same. That is, every element in ht1 is also in ht2 and vice versa.
Two element are the same iff their keys are the same in terms of the equality predicate of the tables’ key comparator, and their values are the same in terms of the equality predicate of a comparator value-cmpr.
It is an error if ht1 and ht2 has different key comparators.
[R7RS hash-table] Perform set operations on two hashtables ht1 and ht2, and modify ht1 to store the result. Note that these procedures only look at the keys for operation; if the values of the same key differ between ht1 and ht2, the value in ht1 is taken.