Section 2.5: Designing Programs - PowerPoint PPT Presentation

About This Presentation
Title:

Section 2.5: Designing Programs

Description:

Section 2.5: Designing Programs – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 17
Provided by: Adel90
Learn more at: https://home.adelphi.edu
Category:

less

Transcript and Presenter's Notes

Title: Section 2.5: Designing Programs


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-gtright location of a UFO.
  • Contract
  • X number -gt 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
    thats 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.
  • Cant 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 -gt number
  • / number number -gt number
  • sqrt number -gt number
  • cos number -gt 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.
Write a Comment
User Comments (0)
About PowerShow.com