1 / 16

Section 2.5: Designing Programs

Learn how to design programs effectively using the Design Recipe method, which involves understanding the problem, specifying function contracts, writing examples, and testing and debugging. This approach provides structure and helps prevent "blank page syndrome". Gain the tools to create high-quality programs that meet your specific needs.

rjefferies
Download Presentation

Section 2.5: Designing Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 2.5: Designing Programs

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

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

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

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

  6. 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”.

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

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

  9. 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!

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

  11. Shorter contract notation • + : number number … -> number • / : number number -> number • sqrt : number -> number • cos : number -> number • etc. etc.

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

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

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

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

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

More Related