CS1321: Introduction to Programming - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

CS1321: Introduction to Programming

Description:

It is NOT the solution to whatever problem we re solving ... one I m writing... The template will NOT be exactly like the function you re writing. ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 78
Provided by: ccGa
Category:

less

Transcript and Presenter's Notes

Title: CS1321: Introduction to Programming


1
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Module 6
  • Compound Data Structures

2
y
Whats this?
0
X
3
We can describe information about this location,
such as its x y coordinates.
y
X2 Y5
0
X
4
  • Imagine many points in space.
  • Visually we have no problemskeeping track of
    information about individual points.

y
X2 Y5
0
X
5
What happens when we store this information in a
computer?
Do computers have little grids inside them on
which they keep track of points?
Nope, sorry.
6
We could store stuff textually
X 1 Y 13
No problems yet, right?
7
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
Were still ok, right? We can just follow the
X-Y pattern (But its confusing isnt it?)
8
Grouping corresponding xs and ys together makes
sense.
(X 1, Y 13) (X 3,Y 15) (X 4,Y
2) (X 27, Y 1) (X 2, Y 16) (X 9, Y
1)
9
Structures
We just described the need for what are called
structures. A structure is used 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.
10
A First Structure Example The humble posn
WHAT IT ISposn is a predefined structure in the
Beginning Language mode of DrScheme. WHAT IT
CONSISTS OFThere are two fields or locations
within a posn where data is stored. The fields
have names x y. WHY IT IS USEFUL The posn
structure defines a way to store information
conveniently about a position (point)!
11
Creating a posn instance
To store information about a specific point you
create an instance of the posn structure by
calling the built-in function (make-posn
ltX-valuegt ltY-valuegt)
12
And then how do I use it?
The two fields in a posn are called x y, so the
functions to access those two fields from a posn
instance are (posn-x ltposngt)
(posn-y ltposngt)
13
Example using a posn structure
(define (distance-to-0 in-posn) (sqrt ( (sq
(posn-x in-posn)) (sq (posn-y
in-posn)))))
14
Questions?
15
Making Your Own Structures
So posns are pretty useful, but theyre kind of
boring. Lets create our own
structures! There are plenty of things out there
that we could represent with structures.
16
Maybe information like this
People First Name, Last Name, email
Buildings Name, Address, Year Built,
Residential or Business, Number of
occupants CDs Artist, Genre, Record
Company, Number of Songs Or maybe even
17
Heres an embarrassing photograph! Can you
believe it? This is one of the CS1321
instructors!
18
What info do we want to store?
First name of the individual involved Last name
of the individual involved Date the picture was
taken And Embarrassment level of that
particular photograph
19
Here is the format for defining a structure
(define-struct ltname of structuregt
(ltfirst field namegt ltsecond field
namegt ltlast field
namegt ))
20
We now have the ability to store information
about bad photos using our structure, badphoto.
NOTE We have NOT stored any information about
any particular photo yet. We have only created
an empty data structure type that describes how
we can store info about badphotos.
21
define-struct buys us some stuff
Whenever we define a new structure, we also
automatically define several functions to support
the use of that new structure. Consider it a
function bonus!!
22
Some function freebies!
CONSTRUCTOR (make-badphoto ltfirstgt ltlastgt
ltdategt ltlevelgt) Doesnt this look kind of
similar to make-posn?
ACCESSORS (badphoto-first ltbadphotogt)
(badphoto-last ltbadphotogt)
(badphoto-date ltbadphotogt) (badphoto-level
ltbadphotogt)
23
Last but not least
PREDICATES (badphoto? ltthing of unknown
typegt) Would it surprise you to know
there was a (posn? ) function?
24
Lets really see some of those functions in use
Now, lets access some of the fields
dan
dan
25
Design Recipe NoteThe Data Definition for
Structures
Several of you probably have the thought
Well, couldnt I just stick anything in my posn
or badphoto structures? Whats to
prevent me from saying (make-posn
George Burdell) (make-badphoto 27
18 1900 Bubba)?
26
Absolutely nothingsay it again
Its up to the user to insert the correct data in
the correct places. So how do you communicate
with the user about what youre expecting to be
stored in your structures?
COMMENTS
27
So this changes my Design Recipe, right?
YES! As an example, lets change our old
familiar posn structure into a dot structure.
Dots are little filled-in circles with varying
colors but uniform size. Each dot should have
its own location represented by an x y
position.
y
X
28
(No Transcript)
29
Most of what were doing here is a definition
Here, we tell the user must be passed to the
constructor for a dot
30
This is a constant for all dots!
31
One last note
Something to think about for future lectures IS
THERE A BETTER WAY TO DO THIS? CAN I DO THIS
WITHOUT DUPLICATING EFFORT?
32
New Recipe Section
33
New Recipe Section Template
The Template is a skeletal function based solely
on the data definition. (In this case our dot
structure.) It shows the form that MOST
functions that use a particular data definition
will follow. It shows what things are possible
when processing data of a certain data
definition. A couple of things to note
34
It is NOT the solution to whatever problem were
solving
It shows everything that could be done because of
the data definition of the data being processed
35
Our Function
We want a function that consumes a dot, an old
color and a replacement color. If the dots
color is the same as the old color then the
function is to create and return a dot with the
same x and y, but with the new color substituted.
If the dot is not of the old color then the
function just returns the dot unchanged.
36
Continuing
Contract change dot color color -gt dot
where the first color is the color to replace
and the second color is the replacement color
Purpose if the color of the dot matches the
color to replace, create a new dot with the
new color substituted. Otherwise return the
dot unchanged. Examples (change (make-dot
3 4 red) red blue) should produce
(make-dot 3 4 blue) (change (make-dot 5 6
green) blue yellow) should produce
(make-dot 5 6 green)
37
Template from before
Template (define (process-dot in-dot)
(dot-x in-dot) (dot-y in-dot)
(dot-color in-dot) )
38
Finishing up
Definition (define (change my-dot bad-color
new-color) (cond (symbol? (dot-color
my-dot) bad-color) (make-dot
(dot-x my-dot) (dot-y my-dot)
new-color) else my-dot))
39
Finally
Tests (change (make-dot 3 4 red) red
blue) should produce (make-dot 3 4
blue) (change (make-dot 5 6 green) blue
yellow) should produce (make-dot 5 6 green)
40
Using the same Data Definition and Template,
suppose
We want a function that consumes two dots and
resolves to the distance between the two dots.
41
Recall
Wanted Distance between points (dots) P1 and P2
P1 (10,6)
?Y 6 - 2
P2 (4,2)
Y
?X 10 - 4
__________ Distance ?(? X)2
(? Y)2
X
42
Easy
What about the definition??
43
Useful to use a helper function
  • Definition
  • (define (distance-helper dx dy)
  • (sqrt ( ( dx dx) ( dy dy))))
  • (define (distance dot1 dot2)
  • (distance-helper (- (dot-x dot2)
  • (dot-x dot1))
  • (- (dot-y dot2)
  • (dot-y dot1))))

44
(No Transcript)
45
Now, let me predict your questions
Starting with Data Definitions
  • Are Data Definitions just for structures?

No, well see more uses soon
  • Does the structure definition go in the
  • data definition section?

Yes
46
  • If I create a data definition for problem X, and
    I use the same data definition in problem X1, do
    I have to retype the data definition? Do I have
    to show the declaration of the structure again?

No, you do NOT have to re-declare the data
definition or re-declare the structure. It is
sufficient to say Im using data definition
from Problem X (where Problem X
appears previously in the same
homework set.
47
Template questions
  • The function in the template isnt the same as
    the
  • one Im writing

The template will NOT be exactly like the
function youre writing. The template shows what
is possible, given your Data Definition. Its
almost like a menu of typical things you can do.
Your function might not call the functions
exactly as shown in the template. In the dot
distance example, we didnt call dot-color
because we didnt need it. However, the template
did show that when you consume a dot, a function
you can call is dot-color if you want to
retrieve the color of the dot.
48
  • If Im using the same data definition in problem
    X1 as
  • I did in problem X, do I have to repeat the
    template?

Yes. For now.
  • But you just told me I didnt have to do that for
    the Data Definition. What gives?

The template serves as a reminder of your
possible choices when using a particular Data
Definition. It should appear right above the
Definitions section of your function. Believe
it or not, it will keep you from making mistakes.
As you work with templates more, youll find
that the solutions to your problems fall right
out from the template.
49
Football example
So for those of you who follow (American)
football, you might recall that each game is
divided into four quarters. During the course of
each quarter, both teams involved do their
darnedest to score points on the opposing team,
with the goal of having more points at the end of
the game than the opposing team. I happen to
just be mad about statistics, and I want to be
able to group together information about how my
team did over the course of the game.
50
I want to be able to store the following
information The points my team
scored during the first quarter Those
scored during the second quarter Those
scored during the third quarter and Those
scored during the fourth quarter
Furthermore
51
I want a function that will consume one of these
structures and calculate the average number of
points per quarter scored by my team over the
course of a game.
Lets get started!
52
First, From the Top, Data AD
Data Analysis Definition (define-struct game
(first second third fourth)) A game is a
structure (make-game a b c d) where a, b,
c, d are numbers representing the points
during the 1st, 2nd, 3rd, and 4th quarters of a
football game
53
Then, the usual stuff
Contract game-average game -gt number
Purpose calculate the average number of
points per quarter scored by my team
over the course of a
football game Example (game-average
(make-game 7 0 14 3)) should
produce 6 Example (game-average (make-game 7
7 0 14)) should produce 7
Example (game-average (make-game 0 0 0 0))
should produce 0
54
The Template
Template (define (process-game in-game)
(game-first in-game)
(game-second in-game) (game-third
in-game) (game-fourth in-game))
55
Definition Testing
Definition (define (game-average in-game)
(/ ( (game-first in-game)
(game-second in-game) (game-third
in-game) (game-fourth in-game))
4)) Tests ( (game-average (make-game 7
0 14 3)) 6) ( (game-average (make-game 7 7 0
14)) 7) ( (game-average (make-game 0 0 0
0)) 0)
56
Variety in Data...
57
Problem Statement Dealing with Shapes
Create a function find-area that takes in a 2-D
shape and calculates the area of that shape.
Specifically you should deal with Circles PI
Radius Radius (where PI 3.14) Rectangles
Base Height Triangles 1/2 length of the
base height (you may assume that only right
triangles are given, though any data definitions
you use should store any type of triangle)
58
Creating Complex Data Definitions
So far weve seen very simple data definitions.
For example (define-struct posn (x y))
For our Data Definition, the actual
declaration of the structure doesnt tell the
complete story. The comment below it specifies
EXACTLY what a posn is.
a posn is a structure (make-posn a b)
where a and b are numbers representing the x
and y coordinates of a point
59
  • For instance
  • A pet is either
  • a cat, or
  • a dog, or
  • a snake

It is feasible to create data definitions that
allow more variety
In this case, lets look at this classic example
Shape
60
Shapes
Our problem statement allows us to keep it
simple. A shape can only be
triangle it has three points
rectangle it has an upper left-
hand corner, a base, and a height.
circle it has a center position and
a radius
61
Shapes
Our problem statement allows us to keep it
simple. A shape can only be
triangle it has three points
Lets assume this
rectangle it has an upper left-
hand corner, a base, and a height.
circle it has a center position and
a radius
62
How do we capture this in scheme?
  • We know we can define structures for each single
    type (and we need to do this.)
  • One for triangle
  • One for rectangle
  • One for circle
  • But how do we capture the notion that these are
    all subcategories of shape?

63
First the Trivial Data Definitions
(define-struct rectangle (lcorner width
height)) a rectangle is a structure
(make-rectangle lc w h) where lc is a posn, and
w h are numbers (define-struct triangle
(corner1 corner2 corner3)) a triangle is a
structure (make-triangle c1 c2 c3) where c1,
c2, c3 are posns (define-struct circle (center
radius)) a circle is a structure (make-circle
c r) where c is a posn and r is a number
64
Using these simple statements
  • A shape can be either a circle, a rectangle or a
    triangle. So why dont we just say

A shape is either 1. A circle 2.
A rectangle 3. A triangle
65
Shape Defined
A shape is either 1. A circle 2.
A rectangle 3. A triangle
Wait, where is (define-struct shape ) or
somethinglike that? We dont need it. A shape
is not a structure.
Because of the flexibility of shape any function
that takes in a shape must be prepared to deal
with any of the three possibilities.
66
Skipping ahead a little, Template for processing
a shape
(define (process-shape my-shape) (cond
(circle? my-shape)
(circle-center my-shape)
(circle-radius my-shape)
(rectangle? my-shape)
(rectangle-lcorner my-shape)
(rectangle-width my-shape)
(rectangle-height my-shape)
(triangle? my-shape)
(triangle-corner1 my-shape)
(triangle-corner2 my-shape)
(triangle-corner3 my-shape)))
67
Skipping ahead a little, Template for processing
a shape
(define (process-shape my-shape) (cond
(circle? my-shape)
(circle-center my-shape)
(circle-radius my-shape)
(rectangle? my-shape)
(rectangle-lcorner my-shape)
(rectangle-width my-shape)
(rectangle-height my-shape)
(triangle? my-shape)
(triangle-corner1 my-shape)
(triangle-corner2 my-shape)
(triangle-corner3 my-shape)))
Were testing for each type of shape!
68
Back to the problem
Contract find-area shape-gtnumber Purpose
Given a shape, calculate the specific area of
the shape using its specific properties Examples
(find-area (make-rectangle
(make-posn 0 0) 3 4))
-gt 12 more
examples Template
69
(define (process-shape my-shape) (cond
(circle? my-shape)
(circle-center my-shape)
(circle-radius my-shape)
(rectangle? my-shape)
(rectangle-lcorner my-shape)
(rectangle-width my-shape)
(rectangle-height my-shape)
(triangle? my-shape)
(triangle-corner1 my-shape)
(triangle-corner2 my-shape)
(triangle-corner3 my-shape)))
70
(define (process-shape my-shape) (cond
(circle? my-shape)
(circle-center my-shape)
(circle-radius my-shape)
(rectangle? my-shape)
(rectangle-lcorner my-shape)
(rectangle-width my-shape)
(rectangle-height my-shape)
(triangle? my-shape)
(triangle-corner1 my-shape)
(triangle-corner2 my-shape)
(triangle-corner3 my-shape)))
why dont we just utilize our template to find
our solution?
71
(define (find-area my-shape) (cond (circle?
my-shape) (circle-center
my-shape) (circle-radius
my-shape) (rectangle? my-shape)
(rectangle-lcorner my-shape)
(rectangle-width my-shape)
(rectangle-height my-shape)
(triangle? my-shape)
(triangle-corner1 my-shape)
(triangle-corner2 my-shape)
(triangle-corner3 my-shape)))
If we ponder for a bit, we probably dont really
need the posn data for the case of a circle or
rectangle. Any calculation based on our triangle
certainly needs it!
72
(define (find-area my-shape) (cond (circle?
my-shape) (circle-radius
my-shape) (rectangle? my-shape)
(rectangle-width
my-shape) (rectangle-height
my-shape) (triangle? my-shape)
(triangle-corner1 my-shape)
(triangle-corner2 my-shape)
(triangle-corner3 my-shape)))
Now we fill in some glue!
73
(define (find-area my-shape) (cond (circle?
my-shape) ( 3.14 (
(circle-radius my-shape) (circle-radius
my-shape) (rectangle? my-shape)
( (rectangle-width
my-shape) (rectangle-height
my-shape)) (triangle? my-shape)
(triangle-corner1 my-shape)
(triangle-corner2 my-shape)
(triangle-corner3 my-shape)))
You might note that I made another call to
circle-radius as we needed that value twice to
square it. When we deal with triangle, would we
have to do something similar?
74
(define (find-area my-shape) (cond (circle?
my-shape) ( 3.14 (
(circle-radius my-shape) (circle-radius
my-shape) (rectangle? my-shape)
( (rectangle-width
my-shape) (rectangle-height
my-shape)) (triangle? my-shape)
( 1/2 (distance
(triangle-corner1 my-shape)
(triangle-corner2 my-shape))) (distance
(triangle-corner2
my-shape) (triangle-corner3
my-shape)))))
(We cheated a little by assuming we had a
distance function written for us. But otherwise
this wouldnt fit on the page)
75
(define (find-area my-shape) (cond (circle?
my-shape) ( 3.14 (
(circle-radius my-shape) (circle-radius
my-shape) (rectangle? my-shape)
( (rectangle-width
my-shape) (rectangle-height
my-shape)) (triangle? my-shape)
( 1/2 (distance
(triangle-corner1 my-shape)
(triangle-corner2 my-shape))) (distance
(triangle-corner2
my-shape) (triangle-corner3
my-shape)))))
Which brings up an excellent point. Couldnt we
reduce a bunch of this by abstracting out the
math from our solution? Maybe something like
this...
76
(define (find-area my-shape) (cond (circle?
my-shape) (find-area-circle my-shape)
(rectangle? my-shape)
(find-area-rectangle my-shape)
(triangle? my-shape)
(find-area-triangle my-shape)))
77
Questions?
Write a Comment
User Comments (0)
About PowerShow.com