-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchapter3.rkt
181 lines (138 loc) · 3.54 KB
/
chapter3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#lang racket
;; Exercise 3.1.2
;; maximize profit of movie theater
(define (price n)
(- 5 (* 0.1 n)))
(define (num-of-people n)
(+ 120 (* 15 n)))
(define (cost n)
(+ 180 (* 0.04 (num-of-people n))))
(define (revenue n)
(* (price n) (num-of-people n)))
(define (profit n)
(- (revenue n) (cost n)))
(profit 0)
(profit 10)
(profit 20)
(profit 30)
(profit 40)
(profit 50)
;; Exercise 3.1.2 version 2
(define (profit ticket-price)
(- (revenue ticket-price) (cost ticket-price)))
(define (revenue ticket-price)
(* ticket-price (attendees ticket-price)))
(define (cost ticket-price)
(+ 180 (* 0.04 (attendees ticket-price))))
(define (attendees ticket-price)
(+ 120
(* 15 (/ (- 5 ticket-price) 0.1))))
(profit 0)
(profit 1)
(profit 2)
(profit 3)
(profit 4)
(profit 5)
;; Exercise 3.1.4
(define (cost ticket-price)
(* 1.5 (attendees ticket-price)))
;; Exercise 3.2.1
(define BASE-PRICE 5)
(define PRICE-DROP-UNIT 0.1)
(define BASE-ATTENDEES 120)
(define ATTENDEES-BY-PRICE-DROP 15)
(define BASE-COST 180)
(define COST-PER-ATTENDEE 0.04)
(define (price n)
(- BASE-PRICE (* PRICE-DROP-UNIT n)))
(define (num-of-people n)
(+ BASE-ATTENDEES (* ATTENDEES-BY-PRICE-DROP n)))
(define (cost n)
(+ BASE-COST (* COST-PER-ATTENDEE (num-of-people n))))
(define (revenue n)
(* (price n) (num-of-people n)))
(define (profit n)
(- (revenue n) (cost n)))
(profit 0)
(profit 10)
(profit 20)
(profit 30)
(profit 40)
(profit 50)
;; Exercise 3.3.1
(define CM-PER-INCH 2.54)
(define INCH-PER-FOOT 12)
(define FEET-PER-YARD 3)
(define YARD-PER-ROD 5.5)
(define ROD-PER-FURLONG 40)
(define FURLONG-PER-MILE 8)
(define (inches->cm n)
(* CM-PER-INCH n))
(define (feet->inches n)
(* INCH-PER-FOOT n))
(define (yards->feet n)
(* FEET-PER-YARD n))
(define (rods->yards n)
(* YARD-PER-ROD n))
(define (furlongs->rods n)
(* ROD-PER-FURLONG n))
(define (miles->furlongs n)
(* FURLONG-PER-MILE n))
(define (feet->cm n)
(* (feet->inches n) (inches->cm n)))
(define (yards->cm n)
(* (yards->feet n) (feet->cm n)))
(define (rods->inches n)
(* (rods->yards n) (yards->feet n) (feet->inches n)))
(define (miles->feet n)
(* (miles->furlongs n)
(furlongs->rods n)
(rods->yards n)
(yards->feet n)))
;; should be 5280
(miles->feet 1)
;; should be 91.44
(yards->cm 1)
;; Exercise 3.3.2
(define (area-of-disk radius)
(* pi (sqr radius)))
(define (volume-cylinder radius height)
(* (area-of-disk radius) height))
(volume-cylinder 5 10)
;; Exercise 3.3.3
(define (circumfence radius)
(* 2 pi radius))
(define (area-surrounding radius height)
(* (circumfence radius) height))
(define (area-cylinder radius height)
(+ (area-surrounding radius height)
(* 2 (area-of-disk radius))))
(area-cylinder 5 10)
;; Exercise 3.3.4
(define (outter-radius inner-radius thickness)
(+ inner-radius thickness))
(define (area-of-ring outter-radius inner-radius)
(- (area-of-disk outter-radius)
(area-of-disk inner-radius)))
(define (area-pipe inner-radius length thickness)
(+ (* 2 (area-of-ring (outter-radius inner-radius thickness) inner-radius))
(area-surrounding (outter-radius inner-radius thickness) length)
(area-surrounding inner-radius length)))
(area-pipe 5 10 2)
;; Exercise 3.3.5
(define (v g t)
(* g t))
(define (height g t)
(* 0.5 (v g t) t))
(height 5 10)
;; Exercise 3.3.6
(define (Celsius->Fahrenheit n)
(+ 32 (/ (* 9 n) 5)))
(Celsius->Fahrenheit 0)
(Celsius->Fahrenheit 30)
(Celsius->Fahrenheit 38)
(define (Fahrenheit->Celsius n)
(* (/ 5 9) (- n 32)))
(define (I f)
(Celsius->Fahrenheit (Fahrenheit->Celsius f)))
(I 32)