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:
- <#1335#>Profit<#1335#> is the difference between revenue and costs.
- The <#1336#>revenue<#1336#> is exclusively generated by the sale of
tickets. It is the product of ticket price and number of attendees.
- The <#1337#>costs<#1337#> consists of two parts: a fixed part ($180) and a
variable part that depends on the number of attendees.
- Finally, the problem statement also specifies how the number of
attendees depends on the ticket price.
Let's formulate a function for each of these dependencies; after all,
functions compute how quantities depend on each other.
We start with contracts, headers, and purpose statements. Here is the one
for <#60476#><#1339#>profit<#1339#><#60476#>:
<#70660#>;; <#60477#><#1344#>profit<#1344#> <#1345#>:<#1345#> <#1346#>number<#1346#> <#1347#><#1347#><#1348#>-;SPMgt;<#1348#><#1349#><#1349#> <#1350#>number<#1350#><#60477#><#70660#>
<#1351#>;; to compute the profit as the difference between revenue and costs<#1351#>
<#70661#>;; at some given <#60478#><#1352#>ticket-price<#1352#><#60478#><#70661#>
<#1353#>(define<#1353#> <#1354#>(profit<#1354#> <#1355#>ticket-price)<#1355#> <#1356#>...)<#1356#>
It depends on the ticket price because both revenue and cost depend on
the ticket price. Here are the remaining three:
<#70662#>;; <#60479#><#1364#>revenue<#1364#> <#1365#>:<#1365#> <#1366#>number<#1366#> <#1367#>number<#1367#> <#1368#><#1368#><#1369#>-;SPMgt;<#1369#><#1370#><#1370#> <#1371#>number<#1371#><#60479#><#70662#>
<#70663#>;; to compute the revenue, given <#60480#><#1372#>ticket-price<#1372#><#60480#> <#70663#>
<#1373#>(define<#1373#> <#1374#>(revenue<#1374#> <#1375#>ticket-price)<#1375#> <#1376#>...)<#1376#>
<#70664#>;; <#60481#><#1377#>cost<#1377#> <#1378#>:<#1378#> <#1379#>number<#1379#> <#1380#><#1380#><#1381#>-;SPMgt;<#1381#><#1382#><#1382#> <#1383#>number<#1383#><#60481#><#70664#>
<#70665#>;; to compute the costs, given <#60482#><#1384#>ticket-price<#1384#><#60482#><#70665#>
<#1385#>(define<#1385#> <#1386#>(cost<#1386#> <#1387#>ticket-price)<#1387#> <#1388#>...)<#1388#>
<#70666#>;; <#60483#><#1389#>attendees<#1389#> <#1390#>:<#1390#> <#1391#>number<#1391#> <#1392#><#1392#><#1393#>-;SPMgt;<#1393#><#1394#><#1394#> <#1395#>number<#1395#><#60483#><#70666#>
<#70667#>;; to compute the number of attendees, given <#60484#><#1396#>ticket-price<#1396#><#60484#><#70667#>
<#1397#>(define<#1397#> <#1398#>(attendees<#1398#> <#1399#>ticket-price)<#1399#> <#1400#>...)<#1400#>
Each purpose statement is a rough transliteration of some part of the
problem statement.
<#1406#>Exercise 3.1.1<#1406#>
The next step is to make up examples for each of the function. 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<#60485#><#60485#>
<#1413#>Exercise 3.1.2<#1413#>
Use the results of exercise~#exmovieexamples#1415> to determine how much
it costs to run a show at $3, $4, and $5. 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<#60486#><#60486#>
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~#figprofit#1423> contains
complete definitions of all four functions. The <#60487#><#1424#>profit<#1424#><#60487#> function
computes its result as the difference between the result of
<#60488#><#1425#>revenue<#1425#><#60488#> and <#60489#><#1426#>cost<#1426#><#60489#>, just as the problem analysis and
purpose statement suggest. The computation of both depends on
<#60490#><#1427#>ticket-price<#1427#><#60490#>, which is what the applications say. To compute the
revenue, we first compute the number of attendees for the given
<#60491#><#1428#>ticket-price<#1428#><#60491#> and multiply it with
<#60492#><#1429#>ticket-price<#1429#><#60492#>. 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 <#60493#><#1430#>0.04<#1430#><#60493#> (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 15 cents less than five dollars, 10 more attendees show up.
<#1437#>(d<#1437#><#1438#>efine<#1438#> <#1439#>(profit<#1439#> <#1440#>ticket-price)<#1440#>
<#1441#>(-<#1441#> <#1442#>(revenue<#1442#> <#1443#>ticket-price)<#1443#>
<#1444#>(cost<#1444#> <#1445#>ticket-price)))<#1445#>
<#1446#>(d<#1446#><#1447#>efine<#1447#> <#1448#>(revenue<#1448#> <#1449#>ticket-price)<#1449#>
<#1450#>(*<#1450#> <#1451#>(attendance<#1451#> <#1452#>ticket-price)<#1452#> <#1453#>ticket-price))<#1453#>
<#1454#>(d<#1454#><#1455#>efine<#1455#> <#1456#>(cost<#1456#> <#1457#>ticket-price)<#1457#>
<#1458#>(+<#1458#> <#1459#>180<#1459#>
<#1460#>(*<#1460#> <#1461#>.04<#1461#> <#1462#>(attendance<#1462#> <#1463#>ticket-price))))<#1463#>
<#1464#>(d<#1464#><#1465#>efine<#1465#> <#1466#>(attendance<#1466#> <#1467#>ticket-price)<#1467#>
<#1468#>(+<#1468#> <#1469#>120<#1469#>
<#1470#>(*<#1470#> <#1471#>(/<#1471#> <#1472#>15<#1472#> <#1473#>.10)<#1473#> <#1474#>(-<#1474#> <#1475#>5.00<#1475#> <#1476#>ticket-price))))<#1476#>
~~~~
<#1487#>(d<#1487#><#1488#>efine<#1488#> <#1489#>(profit<#1489#> <#1490#>price)<#1490#>
<#1491#>(-<#1491#> <#1492#>(*<#1492#> <#1493#>(+<#1493#> <#1494#>120<#1494#>
<#1495#>(*<#1495#> <#1496#>(/<#1496#> <#1497#>15<#1497#> <#1498#>.10)<#1498#>
<#1499#>(-<#1499#> <#1500#>5.00<#1500#> <#1501#>price)))<#1501#>
<#1502#>price)<#1502#>
<#1503#>(+<#1503#> <#1504#>180<#1504#>
<#1505#>(*<#1505#> <#1506#>.04<#1506#>
<#1507#>(+<#1507#> <#1508#>120<#1508#>
<#1509#>(*<#1509#> <#1510#>(/<#1510#> <#1511#>15<#1511#> <#1512#>.10)<#1512#>
<#1513#>(-<#1513#> <#1514#>5.00<#1514#> <#1515#>price)))))))<#1515#>
<#60494#>Figure: Two ways to express the <#1520#>profit<#1520#> program<#60494#>
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~#figprofit#1522> shows the most straightforward way of doing so. And
indeed, it is easy to check that the two profit programs in
figure~#figprofit#1523> 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:
rawhtml6 Guideline on Auxiliary Functions rawhtml7 Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example calculations. rawhtml8
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: <#60495#><#1524#>area-of-disk<#1524#><#60495#>. 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 <#60496#><#1525#>attendees<#1525#><#60496#>, are
useful in several other definitions, leading to a network-like
relationship among functions.
<#1528#>Exercise 3.1.3<#1528#>
Determine the profit that the movie owner makes at $3.00, $4.00, and
$5.00 using the program definitions in the both columns column. Make
sure that the results are the same as those predicted in
exercise~#exmovieexamples2#1530>.~ Solution<#60497#><#60497#>
<#1536#>Exercise 3.1.4<#1536#>
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<#60498#><#60498#>