Section 2.5: Designing Programs
E N D
Presentation Transcript
REVIEW: Design Recipe • Figure out precisely what you need to do. • Tell the computer how to do it. • Check that the computer does it right.
Design Recipe – Version 1 • Figure out precisely what you need to do. 1. Understand the problem 2. Function contract 3. Write examples (in Scheme notation) • Tell the computer how to do it. 4. Write a skeleton. 5. Fill in the function body. • Check that the computer does it right. 6. Testing and debugging.
Example: X ;Purpose: ;To determine the left->right location of a UFO. ;Contract: ;X: number -> number ;Examples: (X 3) “should be” 50 (X 1.5) “should be” 35 (X 0) “should be” 0
Example: X ;Skeleton: ;(define (X t) ; …) ;Function (define (X t) (+ (* 10 t) 20)) ;Testing/Debugging ;Check over yourself, then have DrScheme check ;over, then press Run and type in each example.
Why do we need a recipe? • Programming requires creativity, and sometimes that’s enough. • But when programs get big and complex, it helps to have a structure to follow. • Analogous to rules of counterpoint, harmony, etc. in music. Sonata, rondo, virelai, fugue, … • Helps avoid “blank page syndrome”.
What are function contracts? A contract does three things: • Specify function name • Specify how many arguments, and what type(s) • Specify what type is returned !
What are function contracts? A contract does three things: • Specify function name • Specify how many arguments, and what type(s) • Specify what type is returned NOTE: So far we are focusing on numbers. Soon we will see other types of data.
What are function contracts? A contract does three things: • Specify function name • Specify how many arguments, and what type(s) • Specify what type is returned NOTE: So far we are focusing on numbers. Soon we will see other types of data. Can’t use any function correctly without knowing its contract!
Examples of contracts • + takes two or more numbers & returns a number • / takes two numbers & returns a number • sqrt takes one number & returns a number • cos takes one number & returns a number • etc. etc.
Shorter contract notation • + : number number … -> number • / : number number -> number • sqrt : number -> number • cos : number -> number • etc. etc.
“cube”: Figure out what to do. 1. Understand the assignment yourself. Take any number and multiply it by itself. Then multiply that product by the original number. 2. Write a function contract. ; cube : takes in a number and returns a number 3. Write examples (in Scheme notation). ; (cube 0) “should be” 0 ; (cube 5) “should be” 125 ; (cube -6) “should be” -216
“cube”: Tell the computer how to do it. 4. Write the function skeleton. ; (define (cube num) ; … something involving num … ) 5. Write the function body. (define (cube num) (* num num num))
“cube”: Check if its done right. 6.Testing and debugging. Testing Type in (cube 0). Did you get 0? Type in (cube 5). Did you get 125? Type in (cube -6). Did you get -216? Debugging If the answers to any of these questions is no, look back to find your mistake.
In conclusion… • The Design Recipe does not replace the need for thinking. • It does guide the thinking process. • It does have some concrete, automatic steps. • Step 5 – Writing the function body – should hopefully be easier because of the first 4 steps. • However, thinking and content-area knowledge are also essential, even when using a Design Recipe.
Next time… • We have now seen Scheme functions that take in and return numbers. • Next time, we will write Scheme functions that take in and return other data types, like strings and images.