考虑下列问题:
假设有个电影院的老板,他对电影票价有完全自由的自定权,他定的越高,则愿意消费
的人越来越少。经过一段时间得出的经验,老板决定要精确地知道票价与平均消费者人
数之间的关系。经验数据是:每张票5美元,有120人出席,相应减少0.1美元,则出席人
数增加15人。不幸的是,增加出席人数也带来了成本的增加。为此,每一场要花费180美
元,即每增加一个消费者,随之成本提高0.04美元。为实现利润最大化,他想搞清楚利润
与票价之间的数量关系。
要做的已经很显然,但怎样做却不尽然。我们只能作如下的几个数量分析。
当我们面临这种情况时,最好是每一次写出哪几个变量依赖于某个变量:
1.利润 = 收入 - 成本
2.收入 = 出席人数 * 票价
3.成本 = 180 + (出席人数 - 120) * 0.04
4.出席人数与票价成反比,即:出席人数 = 120 * (5 - P) * 150,其中P是票价。
对于以上的依赖性,我们给出相应的函数,然后,函数计算出它们之间的数量。
我们以Contracts,Headers,Purpose Statements开始,这里利润的一部份:
;;profit : number -> number
;;在给定的票价的情况下,利润是收入减去总成本的差
(define (profit ticket-price)...)
利润依赖于票价,因为收入和成本都同时依赖于票价,以下是余下的三部分:
;;revenue : number -> number
;;给定票价,计算收入
(define (revenue ticket-price) ...)
;;cost : number -> number
;;给定票价,计算总成本
(define (cost ticket-price) ...)
;;attendees : number -> number
;;给定票价,计算出席人数
(define (attendees ticket-price) ...)
一个目的声明是问题声明一些部份的粗略字译。
Exercise 3.1.1 下一步是对每一个函数都给出一个例子。假设票价分别是3.00,4.00,5.00,问有
多少人出席?用这个例子给出一个一般性的公式,能够决定给定票价的情况下,有多少数量的人出席?
试构造更多的例子。
解:
(define (attendees ticket-price)
(+ 120 (* 150 (- 5 ticket-price))))
(attendees 3) => 420
(attendees 4) => 270
(attendees 5) => 120
Exercise 3.1.2 利用3.1.1的结果,问当票价为3.00、4.00、5.00时,总的成本是分别是多少?
同时,它们分别产生多少收入?最后,每一场老板能收到多少利润?那一种定价产生的利润最高?
解:
(define (costs ticket-price)
(+ 180 (* 0.04 (- (attendees ticket-price) 120))))
(define (revenue ticket-price)
(* (attendees ticket-price) ticket-price))
(define (profit ticket-price)
(- (revenue ticket-price) (costs ticket-price)))
(costs 3) (costs 4) (costs 5)
(revenue 3) (revenue 4) (revenue 5)
(profit 3) (profit 4) (profit 5)
192 186 180
1260 1080 600
1068 894 420
Exercise 3.1.4 在研究了每一场的成本结构以后,老板发现几种降低成本的方法。于是他改进方法,
不再有一个固定的成本。他现在的成本是每个出席人1.5。修改被影响的程序,当程序被修改时,再
用3.00、4.00、5.00来试算,并比较结果。
解:
(define (costs ticket-price)
(* (attendees ticket-price) 1.5))
(costs 3) (costs 4) (costs 5)
(revenue 3) (revenue 4) (revenue 5)
(profit 3) (profit 4) (profit 5)
630 405 180
1260 1080 600
630 675 420


