Section 6

Compound Data, Part 1: Structures

6.1  Structures

The posn structure is built into DrScheme's teaching languages, including make-posn, posn-x, and posn-y.

When you type

(make-posn 1 2)

in the interaction window, DrScheme replies with
(make-posn 1 2)

DrScheme's reply is the same as your input expression because there is no shorter name for the posn containing 1 and 2.

In contrast, enter

(make-posn (+ 1 2) 4)

and DrScheme replies with
(make-posn 3 4)

In this case, (+ 1 2) can be reduced to the simpler 3. But, as before, there is no simpler name for a posn containing 3 and 4 other than (make-posn 3 4).

6.2  Extended Exercise: Drawing Simple Pictures

Wehn you evaluate (start 300 300) with the draw.ss library, DrScheme create a new, empty drawing canvas, like this one:

 [emptycanvas.gif] 

Evaluating a drawing expressions, such as

(draw-solid-line (make-posn 100 100) (make-posn 200 200) 'red)

changes the image in the canvas:

 [linecanvas.gif] 

If you try to evaluate drawing expressions before using start, DrScheme reports an error message that reminds you to use start.

When the canvas window appears with your images, do not close it, because there is no way to re-open a closed canvas window. Instead, you have to re-execute your program, or create a new canvas window with start.

If you use start multiple times, drawing commands affect the most recently created canvas.

To close a canvas (when you are done with it), you can click the canvas window's close box, or evaluate (stop). Clicking DrScheme's Execute button also closes canvas windows.

Exercise Notes

Exercise 6.2.1 To open Help Desk, select the Help|Help Desk menu item. On Help Desk's home page, the third major bullet has a sub-link Teachpacks that provides a menu of Teachpack documentation links.

6.3  Structure Definitions

The posn structure is built into DrScheme, so you cannot define it yourself. (If you try, DrScheme will report such as ``cannot redefine name: make-posn'''.)

Structure definitions with define-struct are analogous to function definitions, in that the definition can appear either before or after functions using the structure, but must always precede examples using the structure.

6.4  Data Definitions

To include a data definition in your program file, use a comment (just as for contracts):


;; A star is a structure
;;    (make-star last first instrucment sales)
;; where last, first, and instrucment are symbols
;;   and sales is a number.

6.5  Designing Functions for Compound Data - no notes

No DrScheme notes for this section.

6.6  Extended Exercise: Moving Circles and Rectangles

Use the draw.ss teachpack for this exercise. For information about selecting a teachpack, see the Exercise Notes for Exercise 2.2.1.

6.7  Extended Exercise: Hangman

Initially, use the draw.ss teachpack for this exercise, then switch to hangman.ss as directed. For information about selecting a teachpack, see the Exercise Notes for Exercise 2.2.1.