Monday, March 12, 2007

How to Design Programs - 3.2 Variable Definitions

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

(define (attendees ticket-price)
  (+ 120
     (* (/ 15 .10) (- 5.00 ticket-price))))

Exercise 3.2.1.

Provide variable definitions for all constants that appear in the above profit program and replace the constants with their names.

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)
  (+ FIXED-COST
     (* COST-PER-ATTENDEE (attendees ticket-price))))

(define (attendees ticket-price)
  (+ ATTENDEES-AT-BASE-PRICE
     (*
      (/
       MORE-ATTENDEES-PER-PRICE-DECREASE
       PRICE-DECREASE)
      (- BASE-PRICE ticket-price))))

(define FIXED-COST 180)
(define COST-PER-ATTENDEE 0.04)
(define ATTENDEES-AT-BASE-PRICE 120)
(define BASE-PRICE 5.00)
(define MORE-ATTENDEES-PER-PRICE-DECREASE 15)
(define PRICE-DECREASE .10)

No comments: