Scheme 48 Manual | Contents | In Chapter: Libraries
Previous: Records | Next: Hash tables

Finite record types

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:

(define-finite-type tag type-name
  (field-tag ...)
  predicate-name
  vector-of-elements-name
  name-accessor
  index-accessor
  (field-tag accessor-name [modifier-name])
  ...
  ((element-name field-value ...)
   ...))
This differs from define-record-type in the following ways:
(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:

(define-enumerated-type tag type-name
  predicate-name
  vector-of-elements-name
  name-accessor
  index-accessor
  (element-name ...))
In the absence of any additional fields, both the constructor argument list and the initial field values are not required.

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