6.8 Tuples

For a general description, see Dynamic Tuples. Dynamic tuples are currently the only implementation of tuples.

To use tuples, you first need to declare some tuple keys. They can also be created dynamically, but this is less common.

Data type: tuple-key

A key in a tuple. All tuple keys must be of this type. These print as:

#<Key name>
Macro: define-tuple-key name &key (type ’t) (default nil) no-default? documentation

Defines a tuple key named name as a global lexical variable. If type is supplied, with operations using this key will check that the supplied value is of that type; default is also checked. If default is supplied, it will be returned from lookup, instead of nil, when the tuple has no pair with the given key. Alternatively, if no-default? is true, lookup will signal an error in that case.

Function: get-tuple-key name &key (type ’t) (default nil) no-default?

Creates a tuple key or updates an existing one. If name has not previously been used in the current Lisp session to name a tuple key, creates a new one; otherwise, finds and updates the existing one. In either case, returns the key. name should normally be a symbol, but can be of any type known to FSet. type, default, and no-default? are treated as by define-tuple-key, above.

Function: tuple-key-name key

The name given when the key was created.

Function: tuple-key-type key

The type of values associated with the key.

Data type: tuple

The abstract class for FSet functional tuples. Subclass of collection.

Function: tuple? x

Returns true iff x is an FSet functional tuple.

Data type: dyn-tuple

Functional tuples implemented as dynamic tuples. Subclass of tuple. These print as:

#~< (k0 v0) (k1 v1) ... >

For each key, only its name is printed, without the #<Key …>.

Function: dyn-tuple? x

Returns true iff x is an FSet functional dynamic tuple.

Function: empty-tuple
Function: empty-dyn-tuple

Returns an empty dyn-tuple.

Macro: tuple &rest subforms

Constructor macro. Constructs a tuple of the default implementation, currently dyn-tuple, according to the supplied argument subforms. Each argument subform can be a list of the form (key-expr value-expr), denoting a mapping from the value of key-expr to the value of value-expr; or a list of the form ($ expression), in which case the expression must evaluate to a tuple, denoting all its mappings. The result is constructed from the denoted mappings in left-to-right order; so if a given key is supplied by more than one argument subform, its associated value will be given by the rightmost such subform.

Macro: dyn-tuple &rest subforms

Constructor macro. Constructs a dyn-tuple according to the supplied argument subforms. Each argument subform can be a list of the form (key-expr value-expr), denoting a mapping from the value of key-expr to the value of value-expr; or a list of the form ($ expression), in which case the expression must evaluate to a tuple, denoting all its mappings. The result is constructed from the denoted mappings in left-to-right order; so if a given key is supplied by more than one argument subform, its associated value will be given by the rightmost such subform.

Method: size (tuple)  tup

Returns the number of key/value pairs in tup. O(1).

Method: domain (tuple)  tup

The set of keys in tup. O(1).

Method: with (tuple)  tup k v

Returns a new dynamic tuple that contains all pairs of tup whose key is not k, and also maps k to v. If tup already maps k to a value equal? to v, returns tup itself. If k was created with a type, checks that v is of that type, signalling a tuple-value-type-error if not. O(n) in the worst case, but under reasonable assumptions, typically O(1) in practice.

Method: lookup (tuple)  tup k

If tup contains key k, returns the associated value. Otherwise, if k has a default, returns that default. Otherwise, signals a tuple-key-unbound-error. O(n) in the worst case, but under reasonable assumptions, typically O(1) in practice..

Method: tuple-merge (tuple tuple)  tup1 tup2

Returns a new tuple containing all the keys of tuple1 and tuple2, where the value for each key contained in only one tuple is the value from that tuple, and the value for each key contained in both tuples is the result of calling val-fn on the value from tuple1 and the value from tuple2. val-fn defaults to simply returning its second argument, so the entries in tuple2 simply shadow those in tuple1. O(n).