• No results found

Structure Definitions

In document How to Design Programs (Page 76-80)

In the preceding section we explored one particular class of structures: the posn

structures. A posn structure combines two numbers, and it is useful to represent pixels.

If we wish to represent employee records or points in three-dimensional space, however,

posns are useless. DrScheme therefore permits programmers to define their own structures so that they can represent all kinds of objects with a fixed number of properties.

Using and Defining Structures

A STRUCTUREDEFINITION 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:

1. make-posn, the CONSTRUCTOR, which creates posn structures;

2. posn-x, a SELECTOR, which extracts the x coordinate;

3. posn-y, also a selector, which extracts the y coordinate.

In general, the names of these new operations are created by prefixing the name of the structure with ``make-'' and by postfixing the name with all the field names. This naming convention appears to be complicated but, with some practice, it is easy to remember.

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:

name zip phone

'PeterLee 15270 '606-7771 'PeterLee15270'606-7771

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-phone

Here is how we can use the first one:

(entry-name (make-entry 'PeterLee 15270 '606-7771))

= 'PeterLee

If 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)

= 'PeterLee

(entry-zip phonebook)

= 15270

(entry-phone phonebook)

= '606-7771

Put 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.

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 last first instrument star-sales

The 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) (make-star 'Talcott 'Carolyn 'banjo 80000) (make-star 'Harper 'Robert 'bagpipe 27860)

To select the first name of a star structure called E, we use

(star-first E)

Other fields are extracted with other selectors.

Exercise 6.3.1. Consider the following structure definitions:

1. (define-struct movie (title producer))

2. (define-struct boyfriend (name hair eyes phone))

3. (define-struct cheerleader (name number))

4. (define-struct CD (artist title price))

5. (define-struct sweater (material size producer))

What are the names of the constructors and the selectors that each of them adds to Scheme? Draw box representations for each of these structures. Solution

Exercise 6.3.2. Consider the following structure definition

(define-struct movie (title producer))

and evaluate the following expressions:

1. (movie-title (make-movie 'ThePhantomMenace 'Lucas))

2. (movie-producer (make-movie 'TheEmpireStrikesBack 'Lucas))

Now evaluate the following expressions, assuming x and y stand for arbitrary symbols:

1. (movie-title (make-movie x y))

2. (movie-producer (make-movie x y))

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.

;; increment-sales : star -> star

;; to produce a star record like a-star with 20000 more sales (define (increment-sales a-star)

(make-star (star-last a-star) (star-first a-star)

(star-instrument a-star)

(+ (star-sales a-star) 20000)))

Figure 9: The complete definition of increment-sales

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 9 contains the complete definition.

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

6.4

In document How to Design Programs (Page 76-80)