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 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
REVIEW: Question 2 - “interest-rate” (define (interest-rate amount) (cond [(<= amount 1000).040] [(<= amount 5000).045] [(<= amount 10000).055] [( > amount 10000).060]))
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
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.
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?
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?
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?
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.
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”.
;Purpose: To check the guess of a random number. ;Contract: number number -> string Exercise 5.1.2: check-guess
;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
;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
;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
;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
;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
;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
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…