400 likes | 571 Views
1321. CS. CS1321: Introduction to Programming. Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester. Today’s Menu. I. Administrivia A. TURN IN YOUR ASSIGNMENT B. Note: WebWork is used for Hw5
E N D
1321 CS
CS1321:Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester
Today’s Menu I. Administrivia A. TURN IN YOUR ASSIGNMENT B. Note: WebWork is used for Hw5 II. Introduction to Compound Data (Section 6) A. Introduction B. Demonstration using Graphics! III. Creating your own Structures
Administrivia Homework 4 . . . is now due.
Administrivia Homework 5 will be handed in using the WebWork system. Note the due date and time, on the assignment.
Administrivia Teachpacks. A few students had trouble with teachpacks. Please note: 1) You must install DrScheme completely. 2) You must read directions carefully.
Everyone knows what this is…. y 0 X
y So what’s this? 0 X
We can describe information about this location, such as its x & y coordinates. y X=2 Y=5 0 X
So now we start to have many of these points in space. Visually we have no problems keeping track of information about individual points. y X=2 Y=5 0 X
But what happens when we store this information in a computer? Do computers have little grids inside them on which they keep track of points? NO
So let’s say we store stuff textually X = 1 Y = 13 No problems yet, right?
But as our problem grows more complex… X = 1 Y = 13 X = 3 Y = 15 X = 4 Y = 2 X = 27 Y = 1 X = 2 Y = 16 X = 9 Y = 1 We’re still ok, right? We can just follow the X-Y pattern…
But what happens when… X-Values Y-Values X = 1 X = 2 X = 3 X = 4 X = 9 X = 27 Y = 1 Y = 1 Y = 2 Y = 13 Y = 15 Y = 16
Wouldn’t it be easier to group things together? (X = 1, Y = 13) (X = 3,Y = 15) (X = 4,Y = 2) (X = 27, Y = 1) (X = 2, Y = 16) (X = 9, Y = 1)
Structures The whole idea of structures is to group together data that belongs together (such as the x & y coordinates of a point in space). This gives us a single unit that we can shuttle back and forth between functions without fear of losing the meaning of our data.
Example: The Posn A posn is a scheme structure built into Beginning DrScheme. It contains two fields or locations within a posn where data is stored. These fields have names: x & y. So a posn just stores information about a position!
How do I make one? You call on a built-in function: (make-posn <X-value> <Y-value>)
So How Do I Use it? Well, the two fields in a posn are called x & y, so oddly enough…. (posn-x <posn>) & (posn-y <posn>)
So let’s write an Example that uses a posn structure * I’m not going to put in all the Design Recipe comments here. Structures do change what your Design Recipes look like, but we’ll get into that on Thursday.
distance-to-0 function (define (distance-to-0 in-posn) (sqrt (+ (square (posn-x in-posn)) (square (posn-y in-posn)))))
How about the distance between any two points? Recall: Wanted: Distance between points P1 and P2 P1: (10,6) Y = 6 - 2 P2: (4,2) Y X = 10 - 4 __________ Distance = ( X)2 + ( Y)2 X
Using posn Structures(incomplete use of template…) • (define (distance dx dy) • (sqrt (+ (* dx dx) (* dy dy)))) • ;; Contract: between: posn posn -> number • ;; Purpose: find the distance between two points • ;; use sqrt( dx*dx + dy*dy) • ;; Examples: ( 0 0 ) to ( 0 100 ) -> 100 • ;; ( 0 0 ) to ( 100 100 ) -> 141 • ;; Definition: • (define (between a b) • (distance (- (posn-x b) • (posn-x a)) • (- (posn-y b) • (posn-y a))))
Built-in Drawing Tools • (start <x> <y>) ; open a canvas • (draw-solid-line <a> <b> color) • <a> and <b> are posn’s • Color is a symbol • 'white 'yellow 'red 'blue 'green 'black • (draw-solid-rect <a> <w> <h> color) • <w> and <h> are numbers giving the height and width • (draw-circle <a> <r> color) • <r> is a number giving the radius • (draw-solid-disk <a> <r> color) • (stop); close the current canvas
Draw a Simple Tree 100 150 • (define (drawTree x) • (and • (draw-solid-disk • (make-posn 100 100) 50 'green) • (draw-solid-rect • (make-posn 90 150) 20 100 'black))) 100 50 90 20 100
100 120 100 200 Add Another Tree • (define (drawTree x) • (and • (draw-solid-disk • (make-posn 100 100) 50 'green) • (draw-solid-rect • (make-posn 90 150) 20 100 'black) • (draw-solid-disk • (make-posn 200 120) 50 'green) • (draw-solid-rect • (make-posn 190 170) 20 100 'black)))
Programming Principle • Whenever you are tempted to copy and paste some code: • (draw-solid-rect • (make-posn 200 120) 50 'green) • (draw-solid-rect • (make-posn 190 170) 20 100 'black) • Stop and think: "Is there a better way?" • Let's abstract out the drawing of the tree from its location…
y (y + 50) x 50 (x – 10) 20 100 Draw a Re-Usable Tree ;; tree : posn -> drawing (define (tree p) (and (draw-solid-disk p 50 'green) (draw-solid-rect (offset p (make-posn -10 50)) 20 100 'black))) • ;; offset : posn posn -> posn • ;; take an initial position p and offset delta; return a • ;; new posn whose x and y components are the sums of the • ;; x and y components of p and delta. • (define (offset p delta) • (make-posn (+ (posn-x p) • (posn-x delta)) • (+ (posn-y p) • (posn-y delta))))
Making Your Own Structures So posns are pretty useful and all, but they’re kind of boring. Why not create our own structures? There are plenty of things out there that we could represent with structures….
People – First Name, Last Name, Age, Weight Buildings – Name, Address, Height, Residential or Business, Number of occupants CDs – Artist, Genre, Record Company, Number of Songs Or maybe even…
Embarrassing Photographs! Can you believe it? This is one of the other instructors!
Our Structure Should Store… The First Name of the Individual involved The Last Name of the Individual involved The Date the picture was taken And… The Embarrassment Level of that particular photograph
define-struct (define-struct <name of structure> (<first field name> <second field name> … <last field name> ))
We now have the ability to store information about badphotos. NOTE: We have NOT stored any information about any particular photo. All that we’ve done is create a kind of TEMPLATE for how we WILL store info about badphotos. (Hint: that’s foreshadowing for next lecture!)
define-struct buys us some stuff Whenever we define a new struct, we also automatically define a whole slew of functions that we can use to manipulate that new struct. Consider it a function bonus!!
Some function freebies! CONSTRUCTOR: (make-badphoto <first> <last> <date> <level>) * Doesn’t this look kind of similar to make-posn? ACCESSORS: (badphoto-first <badphoto>) (badphoto-last <badphoto>) (badphoto-date <badphoto>) (badphoto-level <badphoto>)
Last but not least: PREDICATES: (badphoto? <thing of unknown type>) * Would it surprise you to know there was a (posn? …) function?
dan Let me give one of these badphotos a name Now, let’s access some data dan dan