Processing simple forms of data

Processing simple forms of data

  1. designing programs
  1. Contract Purpose and Header
  2. Examples
  3. Body
  4. Test
;; Contract: area-of-ring : number number -> number

;; Purpose: to compute the area of a ring whose radius is
;; outer and whose hole has a radius of inner

;; Example: (area-of-ring 5 3) should produce 50.24

;; Definition: [refines the header]
(define (area-of-ring outer inner)
 (- (area-of-disk outer)
 (area-of-disk inner)))

;; Tests:
(area-of-ring 5 3)
;; expected value
50.24
  1. two coding styles
  • with auxiliary functions
(define (area-of-disk r) (* r r ))
(define (area-of-ring outer inner)
 (- (area-of-disk outer)
 (area-of-disk inner))) 
  • without
(define (area-of-ring outer inner)
 (- (* 3.14 (* outer outer))
 (* 3.14 (* inner inner)))) 

The use of auxiliary functions makes the design process manageable and renders programs readable.

  1. Programs are Function Plus Variable Definitions
  • composing functions

problem description:
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 (0.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.
tease out:
When we are confronted with such a situation, it is best to tease out the various dependencies one at a time:

  1. Profit is the difference between revenue and costs.
  2. The revenue is exclusively generated by the sale of tickets. It is the product of ticket price and number of attendees.
  3. The costs consist of two parts: a fixed part ($180) and a variable part that depends on the number of attendees.
  4. Finally, the problem statement also specifies how the number of attendees depends on the ticket price.

top-down對問題進(jìn)行分解并構(gòu)造函數(shù)

;; How to design a program
(define (profit ticket-price)
 (- (revenue ticket-price)
 (cost ticket-price)))
(define (revenue ticket-price)
 (* (attendees ticket-price) ticketprice))
(define (cost ticket-price)
 (+ 180
 (* .04 (attendees ticketprice))))
(define (attendees ticket-price)
 (+ 120
 (* (/ 15 .10) (- 5.00 ticketprice)))) 

Guideline on Auxiliary Functions:

Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example calculations.

  1. variable definitions
  2. Conditional Expressions and Functions
    case
(define (interest-rate amount)
  (cond[(< amount 1000) 0.04]
       [(< amount 5000) 0.045]
       [else 0.5]))
(interest-rate 5000)
  • Designing Conditional Functions


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,862評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,160評論 0 23
  • 昨天下午的理論考試分兩次,先考的病例書寫,40分鐘完成胃痛的首頁病程記錄,對自己最不滿意的地方是關(guān)于中醫(yī)治療的內(nèi)容...
    Dasiy1017閱讀 655評論 0 0
  • 一個時代結(jié)束了,不要悲觀,一個新的時代即將到來! 最近一段時間的經(jīng)歷比我前面二十八年總合還多,雖然付出了慘重的代價...
    浪到天涯海角閱讀 64評論 0 0
  • 愛你在心口難開(剽悍行動營) .一天不見到你來,就好像身邊少了什么 行動營,你真讓人難猜 為何讓這么多優(yōu)秀的人兒迷...
    正齊閱讀 344評論 0 3

友情鏈接更多精彩內(nèi)容