The structure finite-types
has
two macros for defining `finite' record types.
These are record types for which there are a fixed number of instances,
all of which are created at the same time as the record type itself.
The syntax for the defining a finite type is:
This differs from(define-finite-typetag
type-name
(field-tag
...)predicate-name
vector-of-elements-name
name-accessor
index-accessor
(field-tag
accessor-name
[modifier-name
]) ... ((element-name
field-value
...) ...))
define-record-type
in the following ways:
vector-of-elements-name
is added; it will be bound
to a vector containing all of the elements of the type.
These are constructed by applying the (unnamed) constructor to the
initial field values at the end of the form.
field-tag
s in the constructor's
argument list.
Tag
is bound to a macro that maps element-name
s to the
the corresponding element of the vector.
The name lookup is done at macro-expansion time.
(define-finite-type color :color (red green blue) color? colors color-name color-index (red color-red) (green color-green) (blue color-blue) ((black 0 0 0) (white 255 255 255) (purple 160 32 240) (maroon 176 48 96))) (color-name (color black))->
black (color-name (vector-ref colors 1))->
white (color-index (color purple))->
2 (color-red (color maroon))->
176
Enumerated types are finite types whose only fields are the name and the index. The syntax for defining an enumerated type is:
In the absence of any additional fields, both the constructor argument list and the initial field values are not required.(define-enumerated-typetag
type-name
predicate-name
vector-of-elements-name
name-accessor
index-accessor
(element-name
...))
The above example of a finite type can be pared down to the following enumerated type:
(define-enumerated-type color :color color? colors color-name color-index (black white purple maroon)) (color-name (vector-ref colors 0))->
black (color-name (color white))->
white (color-index (color purple))->
2
Previous: Records | Next: Hash tables