A structure definition is a third form of definition. The keyword define-struct distinguishes this form of definition from function and variable definitions. The keyword is followed by a name and a sequence of names enclosed in parentheses:
=
(define-struct
(
)) .
The names in a define-struct definition must be chosen as if they were function names, though none of them is used as a function (or variable) name.
(define-struct point (x y z)) .Since point, x, y, and z are variables and the parentheses are placed according to the grammatical pattern, it is a proper definition of a structure. In contrast, these two parenthesized sentences
(define-struct (point x y z))are improper definitions, because define-struct is not followed by a single variable name and a sequence of variables in parentheses.(define-struct point x y z)
A define-struct definition introduces new primitive operations. The names of these operations are formed from those that occur in the definition. Suppose a data structure definition has the following shape:
(define-struct c (s-1 ... s-n))Then Scheme introduces the following primitive operations: These primitives have the same status as +, -, or *. Before we can understand the rules that govern these new primitives, however, we must return to the definition of values. After all, the purpose of define-struct is to introduce a new class of values: structures.
Simply put, the set of values no longer consists of just constants, but also of structures, which compound several values into one. In terms of our grammar of values, we must add one clause per define-struct:
Let us return to the points structures. Since the list of fields contains three names, (make-point u v w) is a value if u, v, and w are values.
Now we are in a position to understand the evaluation rules of the new primitives. If c-s-1 is applied to a c structure, it returns the first component of the value. Similarly, the second selector extracts the second component, the third selector the third component, and so on. The relationship between the new data constructor and the selectors is best characterized with n equations:
(c-s-1 (make-c V-1 ... V-n)) = V-1
(c-s-n (make-c V-1 ... V-n)) = V-n
where V-1 ... V-n is a sequence of values that is as long
as s-1 ... s-n.
For our running example, we get the equations
(point-x (make-point V U W)) = V (point-y (make-point V U W)) = U (point-z (make-point V U W)) = WIn particular, (point-y (make-point 3 4 5)) is equal to 4, and (point-x (make-point (make-point 1 2 3) 4 5)) evaluates to (make-point 1 2 3) because the latter is also a value.
The predicate c? can be applied to any value. It returns true if the value is of kind c and false otherwise. We can translate both parts into equations. The first one,
(c? (make-c V-1 ... V-n)) = true ,relates c? and values constructed with make-c; the second one,
(c? V) = false; if V is a value not constructed with make-c ,relates c? to all other values.
Again, the equations are best understood in terms of our example. Here are the general equations:
(point? (make-point V U W)) = true (point? U) = false ; if U is value, but not a point structure.Thus, (point? (make-point 3 4 5)) is true and (point? 3) is false.
Exercise 8.7.1
Distinguish legal from illegal sentences:
Explain why the sentences are legal define-struct definitions or how they fail to be legal define-struct definitions. Solution
Exercise 8.7.2
Which of the following are values?
Exercise 8.7.3
Suppose the Definitions window contains
(define-struct ball (x y speed-x speed-y))Determine the results of the following expressions:
Also check how DrScheme deals with the following expressions: