CS 1321 CS 1321 Introduction to Programming Georgia

  • Slides: 35
Download presentation
CS 1321

CS 1321

CS 1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 6

CS 1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 6 Sept 6 th, 2001 Fall Semester

Today’s Menu I. Review of Structures II. Structures and your Design Recipe III. a.

Today’s Menu I. Review of Structures II. Structures and your Design Recipe III. a. The Data Definition (the beginning) IV. b. The Template V. III. Working it Through

Last Time… We explored the concept of a Structure. Let’s review a little…

Last Time… We explored the concept of a Structure. Let’s review a little…

What is the purpose of a Structure? They group data that belongs together (such

What is the purpose of a Structure? They group data that belongs together (such as the X & Y coordinates of a point) into a single unit. How do we define a Structure? We use the Scheme function (define-struct …) What’s the format of (define-struct …)? (define-struct <name> (<first field name> … <last field name>))

When I define a structure, have I created a Structure? NO! When I define

When I define a structure, have I created a Structure? NO! When I define a structure, what does Scheme create for me? A Constructor Multiple Accessors (or called Selectors) A Predicate

So let’s go back to our previous example….

So let’s go back to our previous example….

BAD PHOTOGRAPHS!

BAD PHOTOGRAPHS!

What functions are generated for the following Definition?

What functions are generated for the following Definition?

What functions are generated for the following Definition? 1. make-badphoto 2. badphoto-first 3. badphoto-last

What functions are generated for the following Definition? 1. make-badphoto 2. badphoto-first 3. badphoto-last 4. badphoto-date 5. badphoto-level 6. badphoto? 1 is the constructor, 2 -5 are all selectors, 6 is a predicate function

How this changes your Design Recipe (Part I) Several of you probably have thought:

How this changes your Design Recipe (Part I) Several of you probably have thought: “Well, couldn’t I just stick anything in my posn or badphoto structures? What’s to prevent me from saying: (make-posn ‘George ‘Burdell) (make-badphoto 27 18 1900 ‘Bubba)? ”

Absolutely nothing… say it again… It’s up to the user to insert the correct

Absolutely nothing… say it again… It’s up to the user to insert the correct data in the correct places. So how do you communicate with the user about what you’re expecting to be stored in your structures?

So this changes my Design Recipe, right? YES! As an example, let’s change our

So this changes my Design Recipe, right? YES! As an example, let’s 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 it’s own location represented by an x & y position. y X

Further… We want a function that consumes two dots and resolves to the distance

Further… We want a function that consumes two dots and resolves to the distance between the two dots.

Most of what we’re doing here is a definition Here, we tell the user

Most of what we’re doing here is a definition Here, we tell the user what should go inside a dot!

This is a constant for all dots!

This is a constant for all dots!

One last note… Something to think about for future lectures: IS THERE A BETTER

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?

Continuing We’re back to normality for a second…

Continuing We’re back to normality for a second…

Another Difference… We’ve now introduced the idea of a Template. But what’s the idea

Another Difference… We’ve now introduced the idea of a Template. But what’s the idea of a Template?

The Template is an example of what can be done with our data definition.

The Template is an example of what can be done with our data definition. It shows the form that MOST functions that use a particular data definition will follow… A couple of things to note…

It is NOT the solution to whatever problem we’re solving We’re writing “distance”, remember?

It is NOT the solution to whatever problem we’re solving We’re writing “distance”, remember? It shows everything that could be done with our data definition These are the functions associated with our data definition, remember?

Finishing up… This is ugly. You should use better abstraction to make this easier

Finishing up… This is ugly. You should use better abstraction to make this easier to read. (Do as I say, not as I do)

Now, let me predict your questions… Starting with Data Definitions: 1) Do Data Definitions

Now, let me predict your questions… Starting with Data Definitions: 1) Do Data Definitions deal with JUST structures? No, but more on that in the next chapter… 2) Do I have to put my structure definition in my data definition section? Yes

3) If I create a data definition for problem X, and I use the

3) If I create a data definition for problem X, and I use the same data definition in problem X+1, do I have to retype the data definition? Do I have to redeclare the struct? No, you do NOT have to re-declare the data definition or re-declare the struct. It is sufficient to say: ; ; I’m using data definition from ; ; Problem X

Template questions… 1) The function in the template isn’t the same as the one

Template questions… 1) The function in the template isn’t the same as the one I’m writing… The function in the template will NOT be exactly like the one you’re writing. The template is an example what is possible, given your Data Definition. It is NOT etched in stone that you will call the functions exactly as shown. In the distance example, we never even called “dot-color”. It wasn’t necessary to work the problem. However, the Template did show that when you take in a value that follows the definition of a structure, you could call “dot-color”

2) If I’m using the same data definition in problem X+1 as I did

2) If I’m using the same data definition in problem X+1 as I did in problem X, do I have to repeat the template? Yes. 3) But you just told me I didn’t have to do that for the Data Definition. What gives? The purpose of the Template is different from the Data Analysis and Definition section. The Template serves as a reminder of your possible choices when using a particular Data Definition. There’s a reason that it appears right above the Definitions section. Believe it or not, it will keep you from making mistakes. As you work with Templates more, you’ll find that the solutions to your problems fall right out from the Template.

How ‘bout one more example? So for those of you who follow (American) football,

How ‘bout one more 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.

I want to be able to store the following information: The points my team

I want to be able to store the following information: The points my team scored during the first quarter The points my team scored during the second quarter The points my team scored during the third quarter The points my team scored during the fourth quarter The total number of points scored Furthermore…

I want a function that will consume one of these structures I just defined

I want a function that will consume one of these structures I just defined and calculate the average number of points scored by my team over the course of a game. Let’s get started!

First, DA&D ; ; Data Analysis & Definition (define-struct game (first second third fourth

First, DA&D ; ; Data Analysis & Definition (define-struct game (first second third fourth total)) ; ; A game is a structure: (make-game a b c d e) ; ; where a, b, c, d, e are numbers

Then, the usual stuff ; ; Contract: game-average : game -> number ; ;

Then, the usual stuff ; ; Contract: game-average : game -> number ; ; Purpose: This function calculates the average ; ; number of points scored by my team ; ; over the course of a game ; ; Example: (game-average (make-game 7 0 14 3 24)) ; ; should produce 6

The Template ; ; Template: ; ; (define (process-game in-game) ; ; … (game-first

The Template ; ; Template: ; ; (define (process-game in-game) ; ; … (game-first in-game)… ; ; … (game-second in-game)… ; ; … (game-third in-game)… ; ; … (game-fourth in-game)… ; ; … (game-total in-game)…)

Definition & Testing ; ; Definition: (define (game-average in-game) (/ (+ (game-first in-game) (game-second

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 24)) 6)