Prev Up Next

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:

(defstruct tree height girth age leaf-shape leaf-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:

(define coconut 
  (make-tree 'height 30
             'leaf-shape 'frond
             'age 5))

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.

The accessor procedures are invoked as follows:

(tree.height coconut) => 30
(tree.leaf-shape coconut) => frond
(tree.girth coconut) => <undefined>

The tree.girth accessor returns an undefined value, because we did not specify girth for the coconut tree.

The modifier procedures are invoked as follows:

(set!tree.height coconut 40)
(set!tree.girth coconut 10)

If we now access these fields using the corresponding accessors, we will get the new values:

(tree.height coconut) => 40
(tree.girth coconut) => 10

Prev Up Next

Log in or registerto write something here or to contact authors.