Saturday, March 10, 2007

How to Design Programs - 3.1 Composing Functions

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.

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

  • 120 people can afford 5 dollar per ticket
  • decreasing the ticket price by 0.1 dollar increases attendance by 15
    or:
    decreasing the ticket price by 1 dollar increases attendance by 150

    or:
    for every dollar less, 150 more people will attend
  • if the ticket price is decreased by 5 dollar, 870 people will attend
    so:
    number of people = 870 - (price in dollars) * (150 per dollar)
priceattendees
5120
4270
3420
2570
1720
0870

Exercise 3.1.2.

Use the results of exercise 3.1.1 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

  • the costs are 180 dollars, plus 0.04 dollars timers the number of attendees
  • the revenue is the number of attendees times the ticket price
  • profit is revenue minus cost
priceattendeescostrevenueprofit
5120184.8600415.2
4270190.81080889.2
3420196.812601063.2
2570202.81140937.2
1720208.8720511.2
0870214.80-214.8

$3.00 is the best price of the three.

;; 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)))))))

Exercise 3.1.3.

Determine the profit that the movie owner makes at $3.00, $4.00, and $5.00 using both program definitions. Make sure that the results are the same as those predicted in exercise 3.1.2.

Solution

;; How to design a program
> (profit 5)
415.2
> (profit 4)
889.2
> (profit 3)
1063.2
;; How not to design a program
> (profit 5)
415.2
> (profit 4)
889.2
> (profit 3)
1063.2

The results are the same as before.

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

;; 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)
  (* 1.50 (attendees ticket-price)))

(define (attendees ticket-price)
  (+ 120
    (* (/ 15 .10) (- 5.00 ticket-price))))
> (profit 5)
420
> (profit 4)
675
> (profit 3)
630
;; how not to design a program
(define (profit price)
   (- (* (+ 120
            (* (/ 15 .10)
               (- 5.00 price)))
         price)
      (* 1.50
        (+ 120
          (* (/ 15 .10)
             (- 5.00 price))))))
> (profit 5)
420
> (profit 4)
675
> (profit 3)
630

The results for both methods are the same. Compared to the results in exercise 3.1.3, a ticket price of $4.00 is more favorable, instead of the earlier $3.00.

No comments: