Top Banner
Section 4.3: Conditions Section 4.3: Conditions and Conditional Functions and Conditional Functions
27

Section 4.3: Conditions and Conditional Functions.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Section 4.3: Conditions and Conditional Functions.

Section 4.3: Conditions and Section 4.3: Conditions and Conditional FunctionsConditional Functions

Page 2: Section 4.3: Conditions and Conditional Functions.

Conditions

Conditions are most useful in the context of a program.

• We call the programs that use them conditional programs.

• We formulate them using conditional expressions.

Page 3: Section 4.3: Conditions and Conditional Functions.

Question 1: Type into Definitions Window

(define age 10)(cond

[(< age 3) “toddler”][(and (>= age 3) (< age 13)) “pre-

teen”][(and (>= age 13) (< age 20)) “teen”][(>= age 20) “adult”])

; returns “pre-teen”Experiment by changing age & re-executing

Page 4: Section 4.3: Conditions and Conditional Functions.

Syntax Rule #4: ConditionalsSyntax Rule #4: Conditionals

((cond cond [question … answer][question … answer]……[question … answer]) [question … answer])

Often, the last part is what answer do you want Often, the last part is what answer do you want for everything else, in which case, the last for everything else, in which case, the last question is question is elseelse..

((cond cond [question … answer][question … answer]……[[elseelse answer]) answer])

Page 5: Section 4.3: Conditions and Conditional Functions.

Exercise 4.3.1Exercise 4.3.1

Decide which of these expressions is Decide which of these expressions is legal:legal:

((condcond ((condcond

[(< n 10) 20][(< n 10) 20] [(< n 10) 20] [(< n 10) 20]

[(> n 20) 0][(> n 20) 0] [(and (> n 20) (<= n [(and (> n 20) (<= n 30))]30))]

[[elseelse 1]) 1]) [ [elseelse 1]) 1])

Page 6: Section 4.3: Conditions and Conditional Functions.

Exercise 4.3.1 solution

• The expressions are identical except for the third line so look at those.

• The second expression does not have an answer on the third line:

[(and (> n 20) (<= n 30)]

so it is an illegal expression.

Page 7: Section 4.3: Conditions and Conditional Functions.

Why is the following illegal?

(cond

[(< n 10) 20]

[ * 10 n]

[else 555])

Page 8: Section 4.3: Conditions and Conditional Functions.

Because…

There is no question on the third line.

Page 9: Section 4.3: Conditions and Conditional Functions.

Question 2: interest-rate

A bank pays higher interest rates to

depositors with larger balances:

• over $10,000, 6%

• over $5000 and up to $10,000, 5.5%

• over $1000 and up to $5000, 4.5%

• up to $1000, 4%

Page 10: Section 4.3: Conditions and Conditional Functions.

How do we write this in Scheme?Use a conditional

(cond

[… …]

[… …]

[… …]

[… …])

Page 11: Section 4.3: Conditions and Conditional Functions.

How do we write this in Scheme?Filling in the questions

(cond

[(<= n 1000) …]

[(<= n 5000) …]

[(<= n 10000) …]

[( > n 10000) …])

Page 12: Section 4.3: Conditions and Conditional Functions.

How do we write this in Scheme?Filling in the answers

(cond

[(<= n 1000) .040]

[(<= n 5000) .045]

[(<= n 10000) .055]

[( > n 10000) .060])

Page 13: Section 4.3: Conditions and Conditional Functions.

Write the program

(define (interest-rate amount)

(cond

[(<= amount 1000) .040]

[(<= amount 5000) .045]

[(<= amount 10000) .055]

[( > amount 10000) .060]))

Page 14: Section 4.3: Conditions and Conditional Functions.

Section 4.4: Designing Conditional Section 4.4: Designing Conditional FunctionsFunctions

Page 15: Section 4.3: Conditions and Conditional Functions.

Strategy for conditionals

• Identify number of cases; write cond with

that many clauses

• If answers are simple, write all the answers

& then go back to fill in questions

• If questions are simple, write all the

questions & then fill in answers

Page 16: Section 4.3: Conditions and Conditional Functions.

Design Recipe – Version 2Design Recipe – Version 2

Figure out precisely what you need to do.Figure out precisely what you need to do.

1. Understand the problem1. Understand the problem2. Function contract2. Function contract3. NEW STEP – Data Analysis3. NEW STEP – Data Analysis4. Write examples (in Scheme notation)4. Write examples (in Scheme notation)

Tell the computer how to do it.Tell the computer how to do it.5. Write a skeleton5. Write a skeleton6. NEW STEP – Choose a template6. NEW STEP – Choose a template

7. Fill in the function body7. Fill in the function body Check that the computer does it right. Check that the computer does it right.

8. Testing and debugging8. Testing and debugging

Page 17: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• Understand

This problem is about determining the interest rate given an amount of money in the bank.

• Contract;; interest-rate: number -> number

Page 18: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• NEW STEP: Data Analysis--We take in a number and determine

which of four intervals it is in.

--We print out a different number for each of the intervals.

Page 19: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• ExamplesBorderline Cases

;; (interest-rate 0) -> ??;; (interest-rate 1000) -> ??;; (interest-rate 5000) -> ??;; (interest-rate 10000) -> ??

Interval Cases;; (interest-rate 500) -> ??;; (interest-rate 3000) -> ??;; (interest-rate 7000) -> ??;; (interest-rate 12000) -> ??

Page 20: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• ExamplesBorderline Cases

;; (interest-rate 0) -> .040;; (interest-rate 1000) -> .040;; (interest-rate 5000) -> .045;; (interest-rate 10000) -> .055

Interval Cases;; (interest-rate 500) -> .040;; (interest-rate 3000) -> .045;; (interest-rate 7000) -> .055;; (interest-rate 12000) -> .060

Page 21: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• Skeleton

(define (interest-rate amount)

( … amount … ) )

Page 22: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• NEW STEP: TemplateSo far we only have one template, for conditionals,so we will use that one.

(define (interest-rate amount) (cond

[ question answer ] … [ question answer ])

Page 23: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• NEW STEP: TemplateSince we have four cases, we will have four lines ofquestions and answers, so the template is refined asfollows:

(define (interest-rate amount) (cond

[ question answer ] [ question answer ]

[ question answer ] [ question answer ]))

Page 24: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• Fill in the Function Body --First fill in the questions.

(define (interest-rate amount) (cond

[(<= n 1000) answer] [(<= n 5000) answer] [(<= n 10000) answer]

[( > n 10000) answer]))

Page 25: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• Fill in the Function Body --Now fill in the answers.

(define (interest-rate amount)(cond

[(<= n 1000) .040] [(<= n 5000) .045] [(<= n 10000) .055] [( > n 10000) .060]))

Page 26: Section 4.3: Conditions and Conditional Functions.

“interest-rate” again

• Testing and Debugging

--As always, type each of your examples into the interactions window.

--If you get an error message or unexpected answer, debug the program to find your mistake.

Page 27: Section 4.3: Conditions and Conditional Functions.

Next time…

• More on Conditional Functions

• Conditional Functions with other data types – string and images