Data that are naturally grouped are called structures. One can use Scheme's compound data types,
eg, vectors or lists, to represent structures. Eg,
let's say we are dealing with grouped data relevant to
a (botanical) tree. The individual elements of
the data, or fields, could be: height, girth, age, leaf-shape, and leaf-color, making a total of 5 fields. Such data
could be represented as a 5-element vector. The fields
could be accessed using
vector-ref and modified using vector-set!.
Nevertheless, we wouldn't want to be saddled with the
burden of remembering which vector index corresponds to
which field. That would be a thankless and error-prone
activity, especially if fields get excluded or included
over the course of time.
We will therefore use a Scheme macro defstruct to
define a structure data type, which is basically a
vector, but which comes with an appropriate suite of
procedures for creating instances of the structure, and
for accessing and modifying its fields. Thus, our tree
structure could be defined as:
(defstructtreeheightgirthageleaf-shapeleaf-color)
This gives us a constructor procedure named
make-tree; accessor procedures for each field,
named
tree.height, tree.girth, etc; and modifier
procedures for each field, named set!tree.height,
set!tree.girth, etc. The constructor is used as
follows:
The constructor's arguments are in the form of
twosomes, a field name followed by its initialization.
The fields can occur in any order, and may even be
missing, in which case their value is undefined.
We can have some initializations done during the
definition of the structure itself, instead of per
instance. Thus, we could postulate that leaf-shape
and leaf-color are by default frond and
green respectively. We can always override these
defaults by providing explicit initialization in the
make-tree call, or by
using a field modifier after the structure instance has
been created: