Consider the following problem:
Imagine the owner of a movie theater who has complete freedom in setting ticket prices. The more he charges, the fewer the people who can afford tickets. In a recent experiment the owner determined a precise relationship between the price of a ticket and average attendance. At a price of $5.00 per ticket, 120 people attend a performance. Decreasing the price by a dime ($.10) increases attendance by 15. Unfortunately, the increased attendance also comes at an increased cost. Every performance costs the owner $180. Each attendee costs another four cents ($0.04). The owner would like to know the exact relationship between profit and ticket price so that he can determine the price at which he can make the highest profit.While the task is clear, how to go about it is not. All we can say at this point is that several quantities depend on each other.
When we are confronted with such a situation, it is best to tease out the various dependencies one at a time:
We start with contracts, headers, and purpose statements. Here is the one for profit:
;; profit : number -> number ;; to compute the profit as the difference between revenue and costs ;; at some given ticket-price (define (profit ticket-price) ...)It depends on the ticket price because both revenue and cost depend on the ticket price. Here are the remaining three:
;; revenue : number -> number ;; to compute the revenue, given ticket-price (define (revenue ticket-price) ...)Each purpose statement is a rough transliteration of some part of the problem statement.;; cost : number -> number ;; to compute the costs, given ticket-price (define (cost ticket-price) ...)
;; attendees : number -> number ;; to compute the number of attendees, given ticket-price (define (attendees ticket-price) ...)
Exercise 3.1.1
The next step is to make up examples for each of the functions. Determine how many attendees can afford a show at a ticket price of $3.00, $4.00, and $5.00. Use the examples to formulate a general rule that shows how to compute the number of attendees from the ticket price. Make up more examples if needed. Solution
Exercise 3.1.2
Use the results of exercise
to determine how much
it costs to run a show at $3.00, $4.00, and $5.00. Also determine how
much revenue each show produces at those prices. Finally, figure out how
much profit the monopolistic movie owner can make with each show. Which is
the best price (of these three) for maximizing the
profit? Solution
Once we have written down the basic material about our functions and
calculated out several examples, we can replace the ``...'' with
Scheme expressions. The left column of figure
contains
complete definitions of all four functions. The profit function
computes its result as the difference between the result of
revenue and cost, just as the problem analysis and
purpose statement suggest. The computation of both depends on
ticket-price, which is what the applications say. To compute the
revenue, we first compute the number of attendees for the given
ticket-price and multiply it with
ticket-price. Similarly, to compute the cost we add the fixed
portion of the cost to the variable part, which is the product of the
number of attendees and 0.04 (four cents). Finally, the
computation of the number of attendees also follows the problem
statement. The base attendance at a price of five dollars is 120, and for
each 10 cents less than five dollars, 15 more attendees show up.
;; How to design a program
(define (profit ticket-price)
(- (revenue ticket-price)
(cost ticket-price)))
(define (revenue ticket-price)
(* (attendees ticket-price) ticket-price))
(define (cost ticket-price)
(+ 180
(* .04 (attendees ticket-price))))
(define (attendees ticket-price)
(+ 120
(* (/ 15 .10) (- 5.00 ticket-price))))
;; How not to design a program
(define (profit price)
(- (* (+ 120
(* (/ 15 .10)
(- 5.00 price)))
price)
(+ 180
(* .04
(+ 120
(* (/ 15 .10)
(- 5.00 price)))))))
Instead of developing a function per dependency in the problem statement,
we could have tried to express the relationship between the ticket price
and the owner's profit in a single function. The right column in
figure
shows the most straightforward way of doing so. And
indeed, it is easy to check that the two profit programs in
figure
produce the same profit when given the same ticket
price. Still, it is also obvious that while the arrangement on the left
conveys the intention behind the program directly, the program on the right
is nearly impossible to understand. Worse, if we are asked to modify some
aspect of the program, say, the relationship between the number of
attendees and the price of the ticket, we can do this for the left column
in a small amount of time, but we need to spend a much longer time for the
right one.
Based on our experience, we thus formulate the first and most important guideline of programming:
| Guideline on Auxiliary Functions |
Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example calculations. |
Sometimes we will find that some of the required functions are already available as programs for other problems. Indeed, we have already encountered such an example: area-of-disk. At other times, we will make a list of functions and develop each one separately. We may then find that some of the functions, such as attendees, are useful in several other definitions, leading to a network-like relationship among functions.
Exercise 3.1.3
Determine the profit that the movie owner makes at $3.00, $4.00, and
$5.00 using the program definitions in both columns. Make
sure that the results are the same as those predicted in
exercise
. Solution
Exercise 3.1.4
After studying the cost structure of a show, the owner discovered several ways of lowering the cost. As a result of his improvements, he no longer has a fixed cost. He now simply pays $1.50 per attendee.
Modify both programs to reflect this change. When the programs are
modified, test them again with ticket prices of $3.00, $4.00, and
$5.00 and compare the results. Solution