New types can be constructed using the define-record-type
macro
from the define-record-types
structure
The general syntax is:
This makes the following definitions:(define-record-typetag
type-name
(constructor-name
field-tag
...)predicate-name
(field-tag
accessor-name
[modifier-name
]) ...)
| type |
(constructor-name
field-init ...) -> type-name
(predicate-name
value) -> boolean
(accessor-name
type-name) -> value
(modifier-name
type-name value)
Type-name
is the record type itself, and can be used to
specify a print method (see below).
Constructor-name
is a constructor that accepts values
for the fields whose tags are specified.
Predicate-name
is a predicate that returns #t
for
elements of the type and #f
for everything else.
The accessor-name
s retrieve the values of fields,
and the modifier-name
's update them.
Tag
is used in printing instances of the record type and
the field-tag
s are used in the inspector and to match
constructor arguments with fields.
Define-record-discloser
determines how
records of type type
are printed.
Discloser
should be procedure which takes a single
record of type type
and returns a list whose car is
a symbol.
The record will be printed as the value returned by discloser
with curly braces used instead of the usual parenthesis.
For example
defines(define-record-type pare :pare (kons x y) pare? (x kar set-kar!) (y kdr))
kons
to be a constructor, kar
and kdr
to be
accessors, set-kar!
to be a modifier, and pare?
to be a predicate
for a new type of object.
The type itself is named :pare
.
Pare
is a tag used in printing the new objects.
By default, the new objects print as #{Pare}
.
The print method can be modified using define-record-discloser
:
will cause the result of(define-record-discloser :pare (lambda (p) `(pare ,(kar p) ,(kdr p))))
(kons 1 2)
to print as
#{pare 1 2}
.
Previous: Arrays | Next: Finite record types