Processing simple forms of data
- designing programs
- Contract Purpose and Header
- Examples
- Body
- 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
- 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.
- 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:
- Profit is the difference between revenue and costs.
- The revenue is exclusively generated by the sale of tickets. It is the product of ticket price and number of attendees.
- The costs consist 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.
top-down对问题进行分解并构造函数
;; 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.
- variable definitions
- 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