Scheme 48 Manual | Contents | In Chapter: Libraries
Previous: Arrays | Next: Finite record types

Records

New types can be constructed using the define-record-type macro from the define-record-types structure The general syntax is:

(define-record-type tag type-name
  (constructor-name field-tag ...)
  predicate-name
  (field-tag accessor-name [modifier-name])
  ...)
This makes the following definitions: 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-names 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-tags 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

(define-record-type pare :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 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:

(define-record-discloser :pare
  (lambda (p) `(pare ,(kar p) ,(kdr p))))
will cause the result of (kons 1 2) to print as #{pare 1 2}.

Previous: Arrays | Next: Finite record types