-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework 0
157 lines (123 loc) · 4.42 KB
/
homework 0
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
;***********************************************
; Please do not copy the entire code
; the auto check will find out
; only providing the thoughts
;************************************************
;Question 1
;a)
(* (- (* 7 3) (- 8 (string-length (string-append "CS" "1101")))) (/ 32 16))
;; (* (- 21 (- 8 (string-length (string-append "CS" "1101")))) (/ 32 16))
;; (* (- 21 (- 8 (string-length "CS1101"))) (/ 32 16))
;; (* (- 21 (- 8 6)) (/ 32 16))
;; (* (- 21 2) (/ 32 16))
;; (* 19 (/ 32 16))
;; (* 19 2)
;; 38
;; Total Steps: 7
;b)
(define (triple n)
(* n 3))
(if (> (triple 2) (triple (/ 12 4)))
(sqrt (+ 4 (* 15 4)))
107)
;;(if (> (* 2 3) (triple (/ 12 4)))
;; (sqrt (+ 4 (* 15 4)))
;; 107)
;;(if (> 6 (triple (/ 12 4)))
;; (sqrt (+ 4 (* 15 4)))
;; 107)
;;(if (> 6 (triple 3))
;; (sqrt (+ 4 (* 15 4)))
;; 107)
;;(if (> 6 (* 3 3))
;; (sqrt (+ 4 (* 15 4)))
;; 107)
;;(if (> 6 9)
;; (sqrt (+ 4 (* 15 4)))
;; 107)
;;(if #false
;; (sqrt (+ 4 (* 15 4)))
;; 107)
;;107
;;Total Steps: 7
;=======================
;Composing Function
;Question 2
(require 2htdp/image)
;; four-square: Image -> Image
;; parameter name: pic
;; given a picture to convert to flipped-picture
(define (four-square pic)
(above (beside (flip-horizontal pic) pic)
(flip-vertical (beside (flip-horizontal pic)pic)
)))
(define pix .)
; For test purpose
(four-square pix)
;=======================
;Question 3
;define constants
(define event 1500)
(define attFee 2)
;; cost: Natural -> Natural
;; parameter name: att
;; consumes the number of attendees at an event and produces the cost to hold events
(define (cost att)
(+ event (* attFee att) ))
(check-expect (cost 1) 1502) ;check with one attendee
(check-expect (cost 0) 1500) ;check boundary - none attendee
(check-expect (cost 100) 1700) ;check normal number
;; cost: Natural Number -> Number
;; parameter name: att price
;; consumes the number of attendees and ticket price to produce the income
(define (income att price)
(* att price))
(check-expect (income 0 0) 0) ;check both boundary
(check-expect (income 1 0) 0) ;check one boundary - free ticket
(check-expect (income 0 20) 0) ;check one boundary - no attendee
(check-expect (income 1 20) 20) ;check one attendee with ticket price
(check-expect (income 1 1.99) 1.99) ;check decimal number
(check-expect (income 10 20) 200) ;check normal attendee with ticket price
;; profit: Natural Number -> Number
;; parameter names: att price
;; consumes the number of attendees and the ticket price to produce the profit for the event
(define (profit att price)
(- (income att price) (cost att)))
(check-expect (profit 0 0) -1500) ;check both boundary
(check-expect (profit 1 0) -1502) ;check one boundary - free ticket
(check-expect (profit 0 20) -1500) ;check one boundary - no attendee
(check-expect (profit 10 20) -1320) ;check negative integer profit
(check-expect (profit 10 15.99) -1360.1) ;check negative decimal profit
(check-expect (profit 20 150) 1460) ;check positive integer profit
(check-expect (profit 20 150.99) 1479.8) ;check positive decimal profit
;=======================
; Question 4
; define constant
(define bankFee 1000)
(define rate 0.04)
(define time 30)
;; total-house-cost: Natural Natural -> Natural
;; parameter name: prcie dp
;; consumes the purchase price of the house and amount of down-payment
;; to produce the grand total payment
(define (total-house-cost price dp)
(+ price (interest (principal price dp)) bankFee))
(check-expect (total-house-cost 0 0) 1000) ;check boundary
(check-expect (total-house-cost 6 9) 1002.4);check purchase price smaller than down-payment
(check-expect (total-house-cost 9 6) 1012.6) ;check purchase price larger than down-payment and decimal total cost
(check-expect (total-house-cost 1000 1000) 2000) ;check purchase price equal to down-payment
(check-expect (total-house-cost 2000 1000) 4200) ;check integer total cost
; Helper function
;#1
(define (interest amount)
(* rate amount time))
(check-expect (interest 0) 0) ;check boundary
(check-expect (interest 1) 1.2) ;check integer input and decimal interest
(check-expect (interest 2.5) 3) ;check decimal input and integer interest
;#2
(define (principal pur down)
(- pur down))
(check-expect (principal 0 0) 0) ;check boundary
(check-expect (principal 1 2) -1) ;check negative principal
(check-expect (principal 1.5 1) 0.5) ;check decimal
(check-expect (principal 2 1) 1) ;check positive principal