57
CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Embed Size (px)

DESCRIPTION

Administrivia Homework 3... is now due.

Citation preview

Page 1: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

CS1321:Introduction to Programming

Georgia Institute of TechnologyCollege of Computing

Lecture 4Aug. 30, 2001Fall Semester

Page 2: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Today’s Menu

I. Administrivia A. TURN IN YOUR ASSIGNMENT B. Be Nice . . . To Rice C. Webwork…the adventure begins

II. Introducing Conditionals and Cond

III. Symbols

Page 3: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

AdministriviaHomework 3 . . . is now due.

Page 4: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Administrivia

We work closely with the authors of your book. We just came back from a workshop with them.

Do not abuse the textbook website.

When they have a problem, we have a problem; when we have a problem, you have a problem. Do not harass them!!!!!!!

1. Some students have asked for access to the solutions. As the website clearly says, solutions are provided for instructors only. Further attempts to obtain answers will be dealt with harshly.

Page 5: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Administrivia

2. Others have sent infantile gibberish via the website. You have to be kidding me!!!GT’s integrity suffers. Grow up.

3. Lastly, some students have attempted to sign up for the Teach-Scheme program, even though this program is for teachers of Scheme only. Don’t do this.

Page 6: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

WebworkShortly, we will use the WebWork system to hand in homework.

The following slides show you the basics of this system.

Page 7: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

A quick introduction to A quick introduction to doing your homeworkdoing your homework

Page 8: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

WebworkThe cs1321 web page has (or shortly willfeature) a guide to WebWork.

You are required to read this guide.

The following slides show you how to navigate WebWork once you’ve logged in.

Page 9: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Who’s my TA?

Click on “Profile”

Page 10: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

This is your (possibly new) recitation room and TA

Page 11: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Most underlined text in WebWorkLinks to useful information

How can I stalk my TA?

Click here for recitation info

More useful information!If you click on your TA’s link you’ll get info about them…

Page 12: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Are you stalking me?

Everything you ever wanted to Know about your TA

Page 13: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

But what about the homework?How do I do the homework?

Read the guide on the cs1321 webpage.

Page 14: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Making the Right choices

As fun as it is to rewrite mathematical functions intoScheme functions, it would be nice to be able to do something more…interesting.

After all, computer programs aren’t built JUST on mathematical equations.

They’re built on conditions.

Page 15: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Conditions

What kind of conditionals?

•Is the fiery demon-monster in line with the projectile fired from my BFG-3000?

•Does the plane have enough fuel to be put in a holding pattern over the airport?

•Does my car have to increase the amount of gas going to the engine to maintain my cruising speed of 80 mph?

Page 16: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Booleans, the building blocks of conditions

Booleans are a new class of values (along with the numbers and symbols we’ve already seen). They represent truth values, and have two settings:

True & False

Page 17: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Let’s start with some old familiar faces

>, <, =

Page 18: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

X = Y

X < Y

X > Y

{ 4 = 55 = 55 = 6

4 < 55 < 55 < 6

4 > 55 > 55 > 6

{{

FalseTrueFalse

TrueFalseFalse

FalseTrueFalse

resolves to

resolves to

resolves to

Page 19: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Now in Scheme…

(= X Y)

(< X Y)

(> X Y)

{ (= 4 5)(= 5 5)(= 5 6)

(< 4 5)(< 5 5)(< 5 6)

(> 4 5) (> 5 5)(> 5 6)

{{

falsetruefalse

truefalsefalse

falsetruefalse

resolves to

resolves to

resolves to

Page 20: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Linking Conditionals and Saying what we mean

Sometimes we want to combine two or three conditions together.

If it’s raining AND I have some money, I’ll drive down to the Regal and catch a flick

If I have $30 OR someone buys it off my wishlist, I’m going to get the Clerks – Collector’s Edition DVD.

Page 21: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

And sometimes we want to negate our conditional:

If I do NOT get a loan this semester, I’ll beeating lots of ramen noodles.

Page 22: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Scheme Functions that do just that

(and (> x 5) (< x 10))

(or (> x 6) (< y 30))

(not (= x 3))

x needs to be between 5 and 10

x needs to be greater than 6 or y needs to be less than 30

x can’t be 3

Page 23: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Short-circuit evaluationJust because we have a whole bunch of conditions doesn’t mean we have to use them all.

We know some basic things about “and” and “or”…

Page 24: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Run these through the Stepper:

(and (> 5 3) (< 3 5) (= 3 5)) (and (= 3 5) (> 5 3) (< 3 5)) (and (> 5 3) (< 3 5) (= 3 3))

(or (> 3 5) (< 5 3) (= 3 3)) (or (> 5 3) (< 3 5) (= 3 3))

Page 25: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Functions that Test Conditions

In our lingo, they’re called:

Predicates

Page 26: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Predicates

True (as in Real) Predicates have the following properties:

1) They Always Resolve To true Or false. (no numbers, symbols, structures, strings, hash tables, vectors,

function bodies, etc) 2) The Function Name Always Ends With

A ?

Page 27: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Examples:(number? <value>) is a built in predicate that tests

whether or not <value> is a number

(define (is-between-5-6? n) is a defined predicate (and (< 5 n) (< n 6))) that tests to see if n

is bigger than 5 but less than 6.

(define (is-bigger-than-z? x y z) another defined (or (> x z) (> y z))) function that tests if either x or y are

bigger than z

Page 28: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Using Your Predicates

So how do you go about putting these conditionalfunctions to good use? How do we put these things into bigger functions?

We need some sort of conditional statement!

Page 29: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

CondThe built-in function “cond” allows us to create our conditional statement. It’s format is as follows:

(cond (question answer) (question answer) … (question answer))

(cond (question answer) (question answer) … (else answer))

OR

The version on the left is more explicit!

Page 30: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Cond…continued

“What does (question answer) mean?”

“question” is where you insert a predicate, a function that evaluates to true or false.

“answer” is where you state what you’d like to happen should your

question resolve to TRUE

Page 31: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

“How many questions and answer pairs can I have?”

As many as you need. “cond” recognizes anywhere from one pair to many pairs.

“What’s the deal with ‘else’?”

“else” is a built-in catch-all clause for your cond statement.

“Will this affect my Design Recipe?” You betcha.

Page 32: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

ExampleLet’s design a simple example that uses cond.

Suppose we have a function that determines the unit price of a item discounted in quantity. Given one or more DVDs, the price is:

– buy 1 DVD for $25.00– buy 3 DVDs for $20.00 each– buy 10 or more DVDs for $12.00 each

Page 33: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Analysis

We begin with the Data Analysis section. We know from the cs1321 template that the section is called:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <1> ;; Data Analysis & Definitions: ;; <not yet required>

For now, we’ll do HALF of this--the data analysis. A data definition will come later.

1

Page 34: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

AnalysisSo what do we know about the data our function will use?

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <1> ;; Data Analysis & Definitions: ;; The unit price of 1 DVD is 25;; In sets of 3 or more, the unit price is 20;; In sets of 10 or more, the unit price is 12

We give a short description of the data we’re working with. Here unit price varies with the quantity of the order.

A number!

Page 35: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

ContractThis gives us enough to build a contract

;; Contract: unit-price : Number --> Number

We have to give the function a quantity, and it returns the unit price.

2

Page 36: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

PurposeThis leads us to the purpose

;; Purpose: to return the unit price of a ;; quantity of DVDs

3

Page 37: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

ExamplesNow things get interesting...

;; Examples:;; (unit-price 1) should return 25;; (unit-price 3) should return 20;; (unit-price 99) should return 12;; (unit-price 8) should return 20

Note that we’ve designed a test case for each condition:

3 10

4

Page 38: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

ExamplesNow things get interesting...

;; Examples:;; (unit-price 1) should return 25;; (unit-price 3) should return 20;; (unit-price 99) should return 12;; (unit-price 8) should return 20

Note that we’ve designed a test case for each condition:

3 10

What aboutzero units?

It does not fitthe problemstatement.

Page 39: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

TemplateOur programs are not yet complicated enough for a template.

;; Template;; < not required >

5

Page 40: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

DefinitionOur definition falls out of the Data Analysis;; Definition(define (unit-price quantity) (cond [ . . . ? . . . ]yipes! How many different conditions do I need?? [ . . . ? . . . ] ))

Note that the ‘else’ could be rewritten with a specific case.

This might make it easier to later modify the function for new prices/quantity discounts, etc.

6

Page 41: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

DefinitionOur definition falls out of the Data Analysis

;; Definition(define (unit-price quantity) (cond [ . . . ? . . . ] look at your data analysis – it tells you! [ . . . ? . . . ] ))

Note that the ‘else’ could be rewritten with a specific case.

This might make it easier to later modify the function for new prices/quantity discounts, etc.

6

Page 42: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

DefinitionOur definition falls out of the Data Analysis

;; Definition(define (unit-price quantity) (cond [(< quantity 3) 25 ] [(and(>= quantity 3)(< quantity 10)) 20 ] [(>= quantity 10) 12 ] ))

Note that we use three very specific exact cases. We’ve been careful to precisely cover the range of possibilities.

This might make it easier to later modify the function for new prices/quantity discounts, etc.

6

Page 43: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

DefinitionOur definition falls out of the Data Analysis

;; Definition(define (unit-price quantity) (cond [(< quantity 3) 25 ] [(and(>= quantity 3)(< quantity 10)) 20 ] [else 12 ] ))

Note the ‘else’ being used this time.

This form is not quite as explicit as the previous.

6

Page 44: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Tests ;; Tests(unit-price 1) ;; should return25(unit-price 3);; should return20(unit-price 99);; should return12(unit-price 8);; should return20

We include test for each of our examples, and perhaps a few more...

7

Page 45: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

New Improved Tests ;; Tests(unit-price 1) =25(unit-price 3)=20(unit-price 99)=12(unit-price 8)=20

We can also opt for the more cosmopolitan ‘=‘ approach instead of commented “should return”

Page 46: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Tests

;; Tests(= (unit-price 1) 25)(= (unit-price 3) 20)(= (unit-price 99) 12)(= (unit-price 8) 20)

But we’ve also learned about booleans today, and can also insert a few tests instead of numbers.

Page 47: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

TestsWe can then see if our tests all return “true”.

This lets the computer do the tedious chore checking if numbers are equal.

Page 48: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

SymbolsThus far, we’ve done only numeric processing. Let’s revisit the various elements of Scheme to learn more about symbolic information.

Recall:Scheme

Elements

IntegersIntegers, e.g. 4, -6, 0

Real NumbersReal Numbers, e.g., 3.14159

Ratios (Rational Numbers)Ratios (Rational Numbers), e.g., 1/2, 4/3

SymbolsSymbols, e.g., x, y, foo, bar-- often used for the name of a parameter,or a function name, or a variable

Page 49: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

SymbolsA symbol is a sequence of characters preceded by a single forward quotation mark:

‘this ‘is ‘a ‘bunch ‘of ‘symbols

By themselves, symbols have no meaning. It’s up to programs and users to give them meaning.

Page 50: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

SymbolsLike numbers, symbols are atomic pieces of data.

Even if they are made up of many letters, the symbol, as a whole is one thing.

You can check to see if symbols are equal by using the symbol=? Predicate:

(symbol=? ‘one ‘one) ==> true

(symbol=? ‘one ‘two) ==> false

Page 51: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

SymbolsAnd just like numbers, we can return symbols, or pass them into a function.

(define (day-of-week day-num) (cond [ (= day-num 0) ‘Saturday ] [ (= day-num 1) ‘Sunday ] [ (= day-num 2) ‘Monday ] [ (= day-num 3) ‘Tuesday ] [ (= day-num 4) ‘Wednesday ] [ (= day-num 5) ‘Thursday ] [ (= day-num 6) ‘Friday ] ))

Page 52: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

SymbolsWhat’s the contract for this one?

(define (day-of-week day-num) (cond [ (= day-num 0) ‘Saturday ] [ (= day-num 1) ‘Sunday ] [ (= day-num 2) ‘Monday ] [ (= day-num 3) ‘Tuesday ] [ (= day-num 4) ‘Wednesday ] [ (= day-num 5) ‘Thursday ] [ (= day-num 6) ‘Friday ] ))

Page 53: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Symbols;; Contract: day-of-week: number -> symbol

(define (day-of-week day-num) (cond [ (= day-num 0) ‘Saturday ] [ (= day-num 1) ‘Sunday ] [ (= day-num 2) ‘Monday ] [ (= day-num 3) ‘Tuesday ] [ (= day-num 4) ‘Wednesday ] [ (= day-num 5) ‘Thursday ] [ (= day-num 6) ‘Friday ] ))

Page 54: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Strings – more later…In DrScheme, you can also define compound pieces of data called strings. Strings are not atoms of data like symbols--they are made of many, many parts.

“this is a string”

(define name “John Doe”)

Like symbols, strings are symbolic data. But that’s where the similarity stops. We’ll use string later, once we come to understand compound data.

Page 55: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Wow! ImagesIn DrScheme, you can also define images--a third type of symbolic data.

Use the “Insert Image…” option to load an image.

Page 56: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Images – can you believe it?The imagesthen becomebound to thevariable nameswe give them.

We’ll work morewith graphicslater on.

Page 57: CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4 Aug. 30, 2001 Fall Semester

Try it!