


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Structures and Aggregates
On the Board
Homework 2 due 9/15/ Read Chapter 2 — as of today, you have the background
Structures — A First Example
Computer displays are (now) filled with windows, menus, popup menus, and so on. Each of these forms is (roughly) a rectangle. To represent the myriad rectangles on the screen, we can use the notion of points on a Cartesian plane. A window (or a menu pane) is simply two opposite corners; by convention, the upper left and lower right corners. We must establish an origin (by convention, the lower left corner.
To represent these points in programs that manipulate screen images, we can use a structure. The structure needs an x coordinate and a y coordinate. In our model, it needs no other data, although a real program might associate more information with the point (color, intensity, kind of rectangle, other objects that it overlays or underlies on the screen, etc.)
If I asked you to draw a point, it might look like:
In Scheme, we create such a structure as follows:
; a point is a structure ; (make-point x y) ; where x and y are numbers (define-struct point (x y))
When this command executes (type it into the Definitions pane of Dr. Scheme and click the Run button), it creates a set of functions that a Scheme program (or expression) can use to manipulate points. These functions include:
COMP 200: Elements of Computer Science Fall 2004 Lecture 7: September 8, 2004
Comments for the methodology and later readers …
Actual Scheme command
point x y
make-point : number number point point-x : point number point-y : point number
make-point creates a new point and sets its values for x and y to the values of its two arguments, in the order used in the define-struct construct. The new point is an object in Scheme’s workspace that persists until it can no longer be seen (end of the session, or loss of the last reference to it). Once you can no longer name it, Dr. Scheme is free to reclaim any space it occupies. Since you cannot name it, you cannot tell if Dr. Scheme maintains it or recycles it.
point-x and point-y take as their sole argument a point. They return the value of the appropriate field — x or y — inside the point.
define-struct creates other functions for manipulating points, but we won’t get to discuss those for a while.
With these functions, we can write programs that create and manipulate points. For example, to add two points,
; PointAdd: point point point ; Purpose: Add two points to produce a new point (define (PointAdd p1 p2) (make-point (+ (point-x p1) (point-x p2)) (+ (point-y p1) (point-y p2)) ))
To move a point on the screen, we “translate” it by an offset in the x direction and an offset in the y direction, precisely what PointAdd does.
Writing Programs with Structures
When we start using structures, the methodology for writing programs becomes more complex in two ways. First, we need to think (actively) about the kind of data that the program will encounter (will need?) and how to represent that data. This requirement adds a step before “contract, header, and purpose” called “ data analysis .” Second, to remind ourselves of the possible selectors that we may need, we also write a template for each structure — a skeleton of a function that accesses that structure. It includes all the selector functions that we may need in manipulating instances of that structure. Now, the methodology is as follows:
Constructor
Selectors
Step 3: Test Data
Inputs Results (make-point 0 0) (make-point 2 4) 8 (make-point 0 0) (make-point –2 –4) 8 (make-point 1 1) (make-point 1 1) 0
Step 4: Work out the code body
Need to compute the sides and multiply them. Subtract x coordinates, then y coordinates. Placement of the origin affects the order in which we should subtract them. Can use absolute value to eliminate the entire order issue. The function works with points, so we can refer back to the template to remind us of what selectors are available inside the point structure. Something along the lines of (abs (- (point-x ul) (point-x lr))) yields the size of the x dimension. Similar expression yields the size in the y dimension. Thus, the area should be (* (abs (- (point-x ul) (point-x lr)) ) (abs (- (point-y ul) (point-y lr)) ) )
Step 5: Code up the program
; RectangleArea: point point number ; Purpose: given points that define the upper left and lower right ; corners of a rectangle, compute the rectangle’s area. (define (RectangleArea ul lr) (* (abs (- (point-x ul) (point-x lr) ) ) (abs (- (point-y ul) (point-y lr) ) ) ))
Step 6: Test the program on the data from Step 3
Try it yourself in Dr. Scheme or hand evaluate the expressions using the rewriting semantics. (Expect to have a hand evaluation on the first test. The rewriting semantics for a selector function are simple — replace the selector and its argument, such as (point-x ul), with the value of that field in the structure.