-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex030.rkt
66 lines (56 loc) · 2.27 KB
/
ex030.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname ex030) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "batch-io.rkt" "teachpack" "2htdp")) #f)))
(define original-price 5.0)
(define attendance-original-price 120)
(define std-price-change 0.1)
(define std-attendance-change 15)
(define attendance-elasticity
(/ std-attendance-change std-price-change))
(define (attendees ticket-price)
(- attendance-original-price
(* (- ticket-price original-price)
attendance-elasticity)))
(define (revenue ticket-price)
(* ticket-price (attendees ticket-price)))
(define fixed-cost 0)
(define variable-cost 1.5)
(define (cost ticket-price)
(+ fixed-cost
(* variable-cost (attendees ticket-price))))
(define (profit ticket-price)
(- (revenue ticket-price) (cost ticket-price)))
; Compact, write-only formula
(define (profit-alt price)
(- (* (+ 120
(* (/ 15 0.1)
(- 5.0 price)))
price)
(+ 0
(* 1.5
(+ 120
(* (/ 15 0.1)
(- 5.0 price)))))))
; Compare profits at $1, $2, $3, $4, $5 using
; both formulas
(define (profit-iter price res)
(if (> price 5)
res
(profit-iter (add1 price)
(string-append res
" "
(number->string (profit price))))))
(define (profit-alt-iter price res)
(if (> price 5)
res
(profit-alt-iter (add1 price)
(string-append res
" "
(number->string (profit-alt price))))))
; Iteratively determine the profit maximizing price
; down to the nearest dime.
(define dime 0.1)
(define (max-profit-price current-price prev-price)
(if (< (profit current-price) (profit prev-price))
prev-price
(max-profit-price (+ current-price dime) current-price)))