This site is a static rendering of the Trac instance that was used by R7RS-WG1 for its work on R7RS-small (PDF), which was ratified in 2013. For more information, see Home. For a version of this page that may be more recent, see TablesCowan in WG2's repo for R7RS-large.

Tables­Cowan

cowan
2010-08-27 20:47:28
3history
source

Tables

This proposal defines an interface to tables, which are widely recognized as a fundamental data structure for a wide variety of applications. A table is a data structure that:

  1. Is disjoint from all other types.
  2. Provides a mapping from some set of keys to some set of values associated to those keys. Keys and values may be any Scheme objects.
  3. Provides an equivalence function which defines when a proposed key is the same as an existing key. No table may contain more than one value for a given key.
  4. Supports mutation as the primary means of setting the contents of a table.
  5. Assumes that keys are immutable; mutating a key leads to unspecified behavior.

No particular implementation such as a-lists, red-black trees, or hash tables is mandated by this proposal. Implementations SHOULD provide the most efficient implementation in time or space or both possible that is consistent with their larger goals; this may mean different implementations for tables of different lengths.

Incorporating this proposal as a standard feature in WG1 Scheme implementations makes it possible to write efficient and portable programs that use tables.

I have added equivalences to SRFI-69 (Basic hash tables) where they exist. Names are based on those in CompleteSequenceCowan.

Constructors

(make-table [equivalence-function] . args)

Creates a new table with no associations. Equivalence-function is a predicate that should accept two keys and return a boolean telling whether they denote the same key value; it defaults to equal?. (SRFI-69 make-hash-table)

Implementations MAY use the args for implementation-specific extensions.

(table [key value] ... )

Creates a new table and populates it with the associations based on the successive key and value arguments. The implementation may take the arguments into account in deciding what kind of table to create, but should not assume that no other types of keys or values will ever exist. (Not in SRFI-69)

(list->table ''alist'' . args)

Creates a new table as if by invoking (make-table . args) which maps the car of every element in alist to the cdr of the same element. If some key occurs multiple times in alist, the value in the first association will take precedence over later ones. (SRFI-69 alist->hash-table)

(table-copy table)

Creates a new table with the same equivalence predicate, associations, and implementation-dependent properties (if any) as table. (SRFI-69 hash-table-copy)

Predicates

(table? obj)

Returns #t if obj is a table. (SRFI-69 hash-table?)

(table-key-exists? table key)

Returns #t if there is any association to key in table. (SRFI-69 hash-table-exists?)

Accessors

(table-ref table key [ thunk ])

Returns the value associated to key in table. If no value is associated to key and thunk is given, it is called with no arguments and its value is returned; if thunk is not given, an error is signalled. (SRFI-69 hash-table-ref)

(table-length table)

Returns the number of associations in table. (SRFI-69 hash-table-size)

(table-keys table)

Returns a list of the keys in table. The order of the keys is unspecified. (SRFI-69 hash-table-keys)

(table-valuestable)

Returns a list of the values in table. The order of the keys is unspecified, and may be inconsistent with the results of table-keys. (SRFI-69 hash-table-values)

(table-equivalence-function ''table)

Returns the equivalence function of table. (SRFI-69 hash-table-equivalence-function)

Mutators

(table-set! table key value)

Sets the value associated to key in table. The previous association (if any) is removed. (SRFI-69 hash-table-set!)

(table-delete! table key)

Removes any association to key in table. It is not an error if no association for that key exists. (SRFI-69 hash-table-delete!)

(table-update! table key procedure [ thunk ])

Semantically equivalent to, but may be implemented more efficiently than, the following code:

(table-set! table key (procedure (table-ref table key thunk)))

(SRFI-69 hash-table-update!)

Iterators

(table-map table procedure . ''args'')`

Returns a new table as if by invoking (make-table . args`). The new table is the result of mapping procedure, which takes two arguments, over table. It is applied to the key and value of each association in table, and returns two values, the key and value to be placed in the new table. (Not in SRFI-69)

(table-for-each table procedure)

The same as table-map, except that no new table is constructed; returns undefined values. (SRFI-69 hash-table-walk)

(table->list table)

Returns an association list such that the car of each element is a key in table and the corresponding cdr of is the value associated to the key. The order of the elements is unspecified. (SRFI-69 hash-table->alist)