Programming is The How of Functions.
There are lots of examples of functions:
So functions aren't always about numbers. That said, today we'll only use functions on numbers. (We'll start enriching our world starting next time.)
A pizza of diameter 14" has a radius of 7",
of which 1" is crust, so the toppings cover a disc
of radius 6":
pizza-topping-area(14) = dough-area(6) = 36pi.
In general: pizza-topping-area(d) = dough-area( d/2 - 1).
Does this rule make sense?
Let's try some more tests:
pizza-topping-area(8) = dough-area(8/2 - 1) = dough-area(3).
pizza-topping-area(2) = dough-area(2/2 - 1) = dough-area(0).
Finally, a question: we could instead have written
pizza-topping-area(d) = pi (d/2 - 1)^2,
or even
pizza-topping-area(d) = pi/4*d^2 - pi*d + pi.
Do we have a preference? Yes!
The first version reflects how we think about the problem,
while the second makes the reader scratch their head
and wonder if it's really correct.
The First Law of Programming:
Your code should reflect the way you think about a problem.
We'll come back and find another advantage to the first phrasing, as well.
You have been programming and computing for a long time, in elementary school and junior high. In this class, we'll spend ten weeks just exploring implications of pre-algebra.
expression (arithmetic) | expression (scheme) | value |
---|---|---|
2 + 2 | (+ 2 2) | 4 |
7 / 3 | (/ 7 3) | 2 1/3 |
3 * (((4 * 5))) | (* 3 (* 4 5)) | 60 |
3 + (4 * 5) | (+ 3 (* 4 5)) | 23 |
(3 + 4) * 5 | (* (+ 3 4) 5) | 35 |
___ -/16 | (sqrt 16) | 4 |
______________ -/ 7*7 + 24*24 | (sqrt (+ (* 7 7) (* 24 24))) | 25 |
_____________ -5 + -/ 5*5 - 4*1*7 ---------------------- 2*1 | ??? | |
let owes(p) = 12*(p/8) |
(define (owes p) (* 12 (/ p 8))) |
n/a |
let dough-area(r) = pi*r^2 |
(define (dough-area r) (* pi (* r r))) |
n/a |
let pizza-topping-area(d) = dough-area(d/2 - 1) |
(define (pizza-topping-area d) (dough-area (- (/ d 2) 1))) |
n/a |
The pizza industry is a tough biz; it's hard to stand out from the competition. [Not actually true in houston, but we'll ignore that.] What if we want to switch to hexagonal pizzas? What code needs to be changed? Well, certainly we change dough-area, to reflect the formula for area of a hexagon ("radius" now means to dist-from-center-to-middle-of-side). But what about pizza-topping-area? No change needed! (Imagine that we'd used the weird pi/4*d^2... version -- we'd be wading through a long list of various pizza functions, and trying to figure out which were dependent on being a circle.) Thus, by re-using dough-area, we don't have to re-write other functions!
The Second Law of Programming:
Re-use existing code.
We call this a "single point of control".
Other uses of this (very general management) principle:
If you ever find yourself writing the same code to do the same thing (in this case, squaring a radius and multiplying by pi), you should stop and think about how to re-use the common code.
To do before next lecture:
Start up DrScheme, and double-check your
expression for
_____________ -5 + -/ 5*5 - 4*1*7 ---------------------- 2*1