Next: Libraries, Previous: Syntax definitions, Up: Program structure [Index]
Record-type definitions are used to introduce new data types, called record types. Like other definitions, they can appear either at the outermost level or in a body. The values of a record type are called records and are aggregations of zero or more fields, each of which holds a single location. A predicate, a constructor, and field accessors and mutators are defined for each record type.
Syntax: ⟨name⟩ and ⟨pred⟩ are identifiers. The ⟨constructor⟩ is of the form
(
⟨constructor name⟩ ⟨field name⟩ …)
and each ⟨field⟩ is either of the form
(
⟨field name⟩ ⟨accessor name⟩)
or of the form
(
⟨field name⟩ ⟨accessor name⟩ ⟨modifier name⟩)
It is an error for the same identifier to occur more than once as a field name. It is also an error for the same identifier to occur more than once as an accessor or mutator name.
The define-record-type
construct is generative: each use creates
a new record type that is distinct from all existing types, including
Scheme’s predefined types and other record types—even record types of
the same name or structure.
An instance of define-record-type
is equivalent to the following
definitions:
(
⟨constructor
name⟩ …)
subexpression and returns a new record of type
⟨name⟩. Fields whose names are listed with ⟨constructor name⟩
have the corresponding argument as their initial value. The initial values
of all other fields are unspecified. It is an error for a field name to
appear in ⟨constructor⟩ but not as a <field name>.
#t
when given a
value returned by the procedure bound to ⟨constructor name⟩ and
#f
for everything else.
For instance, the following record-type definition
(define-record-type ⟨pare⟩ (kons x y) pare? (x kar set-kar!) (y kdr))
defines kons
to be a constructor, kar
and kdr
to be accessors, set-kar!
to be a modifier, and pare?
to be a predicate for instances of <pare>
.
(pare? (kons 1 2)) ⇒ #t (pare? (cons 1 2)) ⇒ #f (kar (kons 1 2)) ⇒ 1 (kdr (kons 1 2)) ⇒ 2 (let ((k (kons 1 2))) (set-kar! k 3) (kar k)) ⇒ 3
Next: Libraries, Previous: Syntax definitions, Up: Program structure [Index]