Title: Conditionals
1Conditionals
2Outline
- Prerequisites
- Basic functions
- Boolean Expressions
- Objectives
- (if )
- (cond )
- Designing conditional functions
- Reference
- HTDP Sections 4.3, 4.4
3Background
- We frequently need to make choices depending on
the value of data - e.g. if we were looking for only real roots of a
quadratic, we might test the discriminant for
being negative - Choices will use boolean expressions to determine
the appropriate result - We will use the (if ) or(cond ) function to
make these choices - Warning There may be some deliberate errors in
the code used in examples today
4The if function
- General form of (if )
- (if ()
- ()
- ())
-
The condition is a boolean expression
Each action is either a literal or a function to
be evaluated
Each action is either a literal or a function to
be evaluated
5Calculating pass - fail
- Consider the task of assigning a pass or fail
- pass-fail number - symbol
- to determine pass or fail for a given
- numerical grade
- examples
- 80 - pass
-
- under 80 - fail
- (define (pass-fail grade)
- (if ( grade 80)
- 'pass
- 'fail ) )
6The cond function
- General form of (cond )
- (cond
-
-
-
- else )
Each condition is a boolean expression
Each result is either a literal or a function to
be evaluated
A catch-all else condition is optional but
highly recommended
may be used instead of ( ) wherever
they improve readability
7Calculating a grade
- Consider the task of assigning a letter grade
- letter-grade number - symbol
- to determine the letter grade for a given
- numerical grade
- examples
- 90 - A
-
- under 60 - F
- (define (letter-grade grade)
- (cond ( grade 90) 'A
- ( grade 80) 'B
- ( grade 70) 'C
- ( grade 60) 'D
- else 'F))
8Designing Conditional Functions
- We will follow an outline similar to that for
designing ordinary functions. - We add a new step Data Analysis and Definition
- We elaborate the process of developing the
function body to allow for the complexity of the
(cond ) function.
9Still the Same Questions to Answer
- Problem statement
- What is relevant?
- What can I safely ignore?
- The Program
- What does it consume? its inputs
- What does it produce? its output
- How are the inputs and outputs related? its
body - Resources
- Does Scheme provide the functions I need?
- If not, must I develop those functions myself?
- Testing
- How will I know this thing is doing what I want?
10Programs Purpose (different problem)
- Begin every program with a CONTRACT which states
its purpose, first logically - interest-rate number - number
- Generate a program header
- (define (interest-rate amount) )
- Write a brief explanatory statement
- to determine the interest rate for a
- given amount of money
- see HTDP Page 39 for the problem statement
11New Step Data Analysis and Definition
- Draw a picture of the situation
- 4 4.5
5
(
(
5,000
10,000
0
to determine the interest rate for a
given amount of money if amount 4 if amount
5,000, use 5
12Program Examples (unchanged)
- Do this before you write the body
- Predict the behavior of the program
- Think about the computations involved
- Illustrate the purpose statement with examples
- to determine the interest rate for a
- given amount of money
- if amount
- if amount
- if amount 5,000, use 5
- test with -1, 700, 1000, 2000, 5000,
- 7000, 9999, 10000, 12000
13Write the Body (expanded)
- Replace the ellipses () in the header with the
code necessary to implement the computation - Add the (cond ) function to the header
- (define (interest-rate amount)
- (cond
-
-
-
- else )
14Write the Body (expanded)
- Define each condition and insert it in the body
- (define (interest-rate amount)
- (cond (
- (and ( amount 1000) (
-
- ( amount 5000)
- else )
- Simplify the expressions
- (define (interest-rate amount)
- (cond (
- (
- ( amount 5000)
- else )
15Write the Body (expanded)
- Insert the proper results
- (define (interest-rate amount)
- (cond (
- (
- ( amount 5000) 5.0
- else 0.0))
- Pop Quiz what could be used instead of a
literal as the result? - Test the behavior
- Is this going to work properly?
- Constant
- Built-in or user-defined function
- Sequence of functions
- (returns the result of the last function)
16Design the Tests (expanded)
- As a minimum, ensure that the examples work
- Cannot show correct behavior for all inputs
- Look for difficult logical situations
- Mathematical (could I divide by 0?)
- Logical (test all paths)
- i.e. make sure each condition is tested
- Check normal operation, and both upper and lower
extremes - Large numbers
- Small numbers
17Demo
18Actual Code
- to determine the interest rate for a
- given amount of money
- if amount
- if amount
- if amount 5,000, use 5
- test with -1, 700, 1000, 2000, 5000,
- 7000, 9999, 10000, 12000
- (define (interest-rate amount)
- (cond (
- (
- (
- else 5.0))
19Questions?
20Summary
- You should now know
- How to use (if )
- How to use (cond )
- Designing conditional functions
21(No Transcript)