For numeric functions, a good strategy is to draw a number line and to identify the intervals that correspond to a specific situation. Consider the contract for the interest-rate function:
;; interest-rate : number -> number ;; to determine the interest rate for the given amount >= 0 (define (interest-rate amount) ...)
It inputs non-negative numbers and produces answers for three distinct situations:
For functions that process booleans, the cond-expression must distinguish between exactly two situations: true and false. We will soon encounter other forms of data that require case-based reasoning.
For our interest-rate function, we should use 0, 1000, and 5000 as borderline cases. In addition, we should pick numbers like 500, 2000, and 7000 to test the interiors of the three intervals.
(define (interest-rate amount)
(cond
[... ...]
[... ...]
[... ...]))
Next we must formulate the conditions that characterize each situation. The conditions are claims about the function's parameters, expressed with Scheme's relational operators or with our own functions.
The number line from our example translates into the following three conditions:
(define (interest-rate amount)
(cond
[(and (<= 0 amount) (<= amount 1000)) ...]
[(and (< 1000 amount) (<= amount 5000)) ...]
[(> amount 5000) ...]))
At this stage, a programmer should check that the chosen conditions distinguish inputs in an appropriate manner. Specifically, if some input belongs to a particular situation and cond-line, the preceding conditions should evaluate to false and the condition of the line should evaluate to true.
In our example, the results are directly specified by the problem statement. They are 4.0, 4.5, and 5.0. In more complicated examples, we may have to determine an expression for each cond-answer following the suggestion of our first design recipe.
Hint: If the answers for each cond-clause are complex, it is good practice to develop one answer at a time. Assume that the condition evaluates to true, and develop an answer using the parameters, primitives, and other functions. Then apply the function to inputs that force the evaluation of this new answer. It is legitimate to leave ``...'' in place of the remaining answers.
When the definition is complete and tested, a programmer might wish to check whether the conditions can be simplified. In our example, we know that amount is always greater than or equal to 0, so the first condition could be formulated as
(<= amount 1000)
Furthermore, we know that cond-expressions are evaluated sequentially. That is, by the time the second condition is evaluated the first one must have produced false. Hence we know that the amount is not less than or equal to 1000, which makes the left component of the second condition superfluous. The appropriately simplified sketch of interest-rate is as follows:
(define (interest-rate amount)
(cond
[(<= amount 1000) ...]
[(<= amount 5000) ...]
[(> amount 5000) ...]))
The figure is not yet translated into HTML.
Exercise 4.4.1
Develop the function interest. Like interest-rate, it consumes a deposit amount. Instead of the rate, it produces the actual amount of interest that the money earns in a year. The bank pays a flat 4% for deposits of up to $1,000, a flat 4.5% per year for deposits of up to $5,000, and a flat 5% for deposits of more than $5,000. Solution
Exercise 4.4.2
Develop the function tax, which consumes the gross pay and produces the amount of tax owed. For a gross pay of $240 or less, the tax is 0%; for over $240 and $480 or less, the tax rate is 15%; and for any pay over $480, the tax rate is 28%.
Also develop netpay. The function determines the net pay of an employee from the number of hours worked. The net pay is the gross pay minus the tax. Assume the hourly pay rate is $12.
Hint: Remember to develop auxiliary functions when a definition becomes too large or too complex to manage. Solution
Exercise 4.4.3
Some credit card companies pay back a small portion of the charges a customer makes over a year. One company returns
Determine by hand the pay-backs for a customer who charged $2000 and one who charged $2600.
Define the function pay-back, which consumes a charge amount and computes the corresponding pay-back amount. Solution
Exercise 4.4.4
An equation is a claim about numbers; a quadratic equation is a special kind of equation. All quadratic equations (in one variable) have the following general shape:
In a specific equation, a, b and c are replaced by numbers, as in
or
The variable x represents the unknown.
Depending on the value of x, the two sides of the equation
evaluate to the same value (see exercise
). If the two sides
are equal, the claim is true; otherwise it is false. A number that makes
the claim true is a solution.
The first equation has one solution,
-1, as we can easily check:
The second equation has two solutions: +1 and -1.
The number of solutions for a quadratic equation depends on the values of a, b, and c. If the coefficient a is 0, we say the equation is degenerate and do not consider how many solutions it has. Assuming a is not 0, the equation has
Develop the function how-many, which consumes the coefficients a, b, and c of a proper quadratic equation and determines how many solutions the equation has:
(how-many 1 0 -1) = 2 (how-many 2 4 2) = 1Make up additional examples. First determine the number of solutions by hand, then with DrScheme.
How would the function change if we didn't assume the equation was
proper? Solution