A STRUCTURE DEFINITION is, as the term says, a new form of definition. Here is DrScheme's definition of posn:
(define-struct posn (x y))When DrScheme evaluates this structure definition, it creates three operations for us, which we can use to create data and to program:
Now consider the following example:
(define-struct entry (name zip phone))The structure represents a simplified entry into an address book. Each entry combines three values. We also say that each entry structure has three fields: name, zip, and phone. Because there are three fields, the constructor make-entry consumes three values. For example,
(make-entry 'PeterLee 15270 '606-7771)creates an entry structure with 'PeterLee in the name-field, 15270 in the zip-field, and '606-7771 in the phone-field.
One way to think of a structure is as a box with as many compartments as there are fields:
The italicized labels name the fields. By putting values in the compartments, we illustrate specific entry structures.
The define-struct definition of entry also introduces new selectors:
entry-name entry-zip entry-phoneHere is how we can use the first one:
(entry-name (make-entry 'PeterLee 15270 '606-7771)) = 'PeterLeeIf we give the structure a name,
(define phonebook (make-entry 'PeterLee 15270 '606-7771))then we can use the selectors in the Interactions window to extract the data from the three fields:
(entry-name phonebook) = 'PeterLeePut more graphically, a constructor creates a box with several compartments and puts values in it. A selector reveals the contents of a particular compartment, but leaves the box alone.(entry-zip phonebook) = 15270
(entry-phone phonebook) = '606-7771
Here is one final example, a structure for representing rock stars:
(define-struct star (last first instrument sales))It defines the class of star structures, each of which has four fields. Accordingly, we get five new primitive operations:
make-star star-last star-first star-instrument star-salesThe first is for constructing star structures; the others are selector operations for extracting values from a star structure.
To create a star structure, we apply make-star to three symbols and a positive integer:
(make-star 'Friedman 'Dan 'ukelele 19004)To select the first name of a star structure called E, we use(make-star 'Talcott 'Carolyn 'banjo 80000)
(make-star 'Harper 'Robert 'bagpipe 27860)
(star-first E)Other fields are extracted with other selectors.
Exercise 6.3.1
Consider the following structure definitions:
Exercise 6.3.2
Consider the following structure definition
(define-struct movie (title producer))and evaluate the following expressions:
Now evaluate the following expressions, assuming x and y stand for arbitrary symbols:
Formulate equations that state general laws concerning the relationships of
movie-title and movie-producer and
make-movie. Solution
Functions both consume and produce structures. Suppose we need to record an increase of sales for one of our stars. This act should be recorded in the star's record. To do so, we should have a function that consumes a star structure and produces a star structure with the same information except for the sales component. Let's assume for now that the function adds 20000 to the star's sales.
First, we write down a basic description of the function, using our contract, header, and purpose format:
;; increment-sales : star -> star ;; to produce a star record like a-star with 20000 more sales (define (increment-sales a-star) ...)Here is an example of how the function should process star structures:
(increment-sales (make-star 'Abba 'John 'vocals 12200)) ;should produce (make-star 'Abba 'John 'vocals 32200))The three sample star structures from above are also good examples of potential inputs.
The increment-sales function must construct a new star structure with make-star, but to do so, it must also extract the data in a-star. After all, almost all of the data in a-star is a part of the star structure produced by increment-sales. This suggests that the definition of increment-sales contains expressions that extract the four fields of a-star:
(define (increment-sales a-star) ... (star-last a-star) ... ... (star-first a-star) ... ... (star-instrument a-star) ... ... (star-sales a-star) ... )As we have seen with the examples, the function adds 20000 to (star-sales a-star) and assembles the four pieces of data into a star structure with make-star. Figure
Exercise 6.3.3
Provide a structure definition that represents an airforce's jet fighters.
Assume that a fighter has four essential properties: designation
('f22, 'tornado, or 'mig22), acceleration,
top-speed, and range. Then develop the function within-range. The
function consumes a fighter record and the distance of a target from the
(fighter's) base. It determines whether the fighter can reach the intended
target. Also develop the function reduce-range. The function
consumes a fighter record and produces one in which the range
field is reduced to 80% of the original value. Solution