(define (wage h) (* 5.65 h)) (define (area-of-disk r) (* 3 (* r r)))
Let's define another program. Here is the problem. Suppose you're given a disk with a hole. How do you determine the area? [Draw Picture.] Well, clearly, the area is the difference between the area of the disk and the area of the hole.
Can we translate this into a program?
(define (area-of-disk-w-hole OR IR) (- (area-of-disk OR) (area-of-disk IR)))
Does this work?
(area-of-disk-w-hole 5 3) = (- (area-of-disk 5) (area-of-disk 3)) = (- (* 3 (* 5 5)) (* 3 (* 3 3))) ~ (* 3 16)
Could we have written this in some other way?
(define (area-of-disk-w-hole OR IR) (- (* 3 (* OR OR)) (* 3 (* IR IR))))The two definitions are equivalent for all number inputs.
Say we don't like pi to be 3 and we now want to make it more precise? How many changes does it take in the first version? How many in the second?
Let's look at wages. The sad truth is that your first job offer is always spoiled by taxes. Now if Steve Forbes had won the election, we would all be paying the same 15% tax on our income. That's an easy program to write:
(define (tax wage) (* 15/100 wage))And how can we put everything together? I.e. how can we write a program that computes the net wage when given the number of hours?
(define (book-keeping hours) (net-wage (wage hours))) (define (net-wage wage) (- wage (tax wage)))How many changes does it take to raise the minimum wage?
The first truth: good programming means good organization. Re-use your programs or, conversely, break a program's task into smaller ones.
If WW stands for the weekly wage, then we first determine into which class it falls: low, medium, stratospheric. Then we mulitply with the proper rate.
true or false: (<= 5 10) true or false: (<= x 10) we can only say true or false if we know what x stands for if x stands for 3, true if x stands for 11, falseWe can also combine such claims:
true or false: (and (<= 5 10) (<= 10 20)) true or false: (and (<= 5 x) (<= x 20)) if x stands for .... true or false: (or (<= 5 10) (<= 10 20)) true or false: (or (<= 5 x) (<= x 20)) if x stands for ....We write true as #tt and false as #ff.
|-----------------|-----------------------------------|------------------->>> 0 500 2500 15% 28% 33% Say WW stands for our weekly wage. Let's formulate conditions for each interval: (<= WW 500) (and (< 500 WW) (<= WW 2500)) (< 2500 WW)
(cond (condition answer) ... (condition answer))Here we go:
(define (tax WW) (cond ((<= WW 500) ...) ((and (< 500 WW) (<= WW 2500)) ...) ((< 2500 WW) ...)))Let's fill in the gaps:
(define (tax WW) (cond ((<= WW 500) (* 15/100 WW)) ((and (< 500 WW) (<= WW 2500)) (* 28/100 WW)) ((< 2500 WW) (* 33/100 WW))))
(tax 300) ;; falls into the first part of the interval, so we must pick first answer = (cond ((<= 300 500) (* 15/100 300)) ((and (< 500 300) (<= 300 2500)) (* 28/100 300)) ((< 2500 300) (* 33/100 300))) = (cond (#t (* 15/100 300)) (#f (* 28/100 300)) (#f (* 33/100 300))) = (* 15/100 300) = 45
Another truth: When problems specify different cases for numeric data, draw interval pictures, translate the intervals into conditions, and use cond expressions to formulate the program.
Matthias Felleisen | This page was generated on Fri Apr 9 09:17:38 CDT 1999. |