CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 4...

Preview:

DESCRIPTION

Administrivia Homework 3... is now due.

Citation preview

CS1321:Introduction to Programming

Georgia Institute of TechnologyCollege of Computing

Lecture 4Aug. 30, 2001Fall 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

AdministriviaHomework 3 . . . is now due.

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.

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.

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

The following slides show you the basics of this system.

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

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.

Who’s my TA?

Click on “Profile”

This is your (possibly new) recitation room and TA

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…

Are you stalking me?

Everything you ever wanted to Know about your TA

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

Read the guide on the cs1321 webpage.

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.

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?

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

Let’s start with some old familiar faces

>, <, =

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

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

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.

And sometimes we want to negate our conditional:

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

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

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”…

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

Functions that Test Conditions

In our lingo, they’re called:

Predicates

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 ?

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

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!

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!

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

“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.

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

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

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!

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

PurposeThis leads us to the purpose

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

3

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

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.

TemplateOur programs are not yet complicated enough for a template.

;; Template;; < not required >

5

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

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

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

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

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

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”

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.

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

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

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

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.

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

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 ] ))

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 ] ))

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 ] ))

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.

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

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

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

We’ll work morewith graphicslater on.

Try it!

Recommended