Download presentation
Presentation is loading. Please wait.
Published byLucy Small Modified over 9 years ago
1
CS 1321
2
CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 6 Sept 6th, 2001 Fall Semester
3
Today’s Menu I.Review of Structures II.Structures and your Design Recipe a. The Data Definition (the beginning) b. The Template III. Working it Through
4
Last Time… We explored the concept of a Structure. Let’s review a little…
5
What is the purpose of a Structure? How do we define a Structure? They group data that belongs together (such as the X & Y coordinates of a point) into a single unit. We use the Scheme function (define-struct …) What’s the format of (define-struct …)? (define-struct ( … ))
6
When I define a structure, have I created a Structure? When I define a structure, what does Scheme create for me? NO! A Constructor Multiple Accessors (or called Selectors) A Predicate
7
So let’s go back to our previous example….
8
BAD PHOTOGRAPHS!
9
What functions are generated for the following Definition?
10
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
11
How this changes your Design Recipe (Part I) Several of you probably have the 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)?”
12
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?
13
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. X y
14
Further… We want a function that consumes two dots and resolves to the distance between the two dots.
16
Most of what we’re doing here is a definition Here, we tell the user what should go inside a dot!
17
This is a constant for all dots!
18
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?
19
Continuing We’re back to normality for a second…
20
Another Difference… We’ve now introduced the idea of a Template. But what’s the idea of a Template?
21
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…
22
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?
23
Finishing up… This is ugly. You should use better abstraction to make this easier to read. (Do as I say, not as I do)
25
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
26
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 re- declare 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
27
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”
28
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.
29
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.
30
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…
31
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!
32
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
33
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
34
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)…)
35
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.