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.
(define-record-type ⟨name⟩
⟨constructor⟩ ⟨pred⟩ ⟨field⟩ …) ¶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:
#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