Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 4.4: Designing Conditional Functions. REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function.

Similar presentations


Presentation on theme: "Section 4.4: Designing Conditional Functions. REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function."— Presentation transcript:

1 Section 4.4: Designing Conditional Functions

2 REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function contract 3. NEW STEP – Data Analysis 4. Write examples (in Scheme notation)  Tell the computer how to do it. 5. Write a skeleton 6. NEW STEP – Choose a template 7. Fill in the function body  Check that the computer does it right. 8. Testing and debugging

3 REVIEW: Question 2 - “interest-rate” (define (interest-rate amount) (cond [(<= amount 1000).040] [(<= amount 5000).045] [(<= amount 10000).055] [( > amount 10000).060]))

4 Question 3: Testing a Program Suppose one of your classmates wrote these examples for interes-rate. Test the program with each of these examples: (interest-rate 4000) ->.045 (interest-rate 20000) ->.060 (interest-rate 6000) ->.040

5 Question 3: Testing a Program Testing and Debugging -- As always, type each of your examples into the interactions window. --Normally, if you get an error message or unexpected answer, debug the program to find your mistake.

6 Question 3: Testing a Program Which example did not return the expected answer? Is there a mistake in the program? What step in the design recipe did he / she make the mistake?

7 Question 3: Testing a Program Which example did not return the expected answer? the third one Is there a mistake in the program? What step in the design recipe did he / she make the mistake?

8 Question 3: Testing a Program Which example did not return the expected answer? the third one Is there a mistake in the program? no What step in the design recipe did he / she make the mistake?

9 Question 3: Testing a Program Which example did not return the expected answer? the third one Is there a mistake in the program? no What step in the design recipe did he / she make the mistake? writing the example So we see that it is possible that the mistake is in the example rather than the actual Scheme function.

10 Exercise 5.1.2: check-guess Develop the function check-guess. It consumes two numbers, guess and target. Depending on how guess relates to target, the function produces one of the following three answers: “Too Small”, “Perfect”, or “Too Large”.

11 ;Purpose: To check the guess of a random number. ;Contract: number number -> string Exercise 5.1.2: check-guess

12 ;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Data Analysis: ;We take in 2 numbers and determine which of 3 intervals it ;is in. ;We print out a different string for each of the intervals. Exercise 5.1.2: check-guess

13 ;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

14 ;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Skeleton: ;(define (check-guess guess target) ; (… guess … target …)) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

15 ;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Template (define (check-guess guess target) (cond [question … answer …] [question … answer …])) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

16 ;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Fill in the Questions (define (check-guess guess target) (cond [(= guess target) …answer…] [(> guess target) …answer…] [(< guess target) …answer…])) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

17 ;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Function body (define (check-guess guess target) (cond [(= guess target) “Perfect”] [(> guess target) “Too Large”] [(< guess target) “Too Small”])) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

18 In summary… Both the interest-rate and check-guess functions need to determine which of several conditions holds for the input. We call them conditional functions. On to compound data… --Posns: A First Structure --Creating Your Own Structures Next time…


Download ppt "Section 4.4: Designing Conditional Functions. REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function."

Similar presentations


Ads by Google