-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.scm
257 lines (218 loc) · 7.86 KB
/
math.scm
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
;;Requred - 2 Library procedures: =, modulo, zero?, equal?;
; Please refer to list.scm for list, and append.;
;More built-in functions with regards to arithmetic.
;using only special forms and primitives
;that we've implemented (e.g., +, -, *, /, and <=)
;;6.more arithmetic procedures
;;= zero? max modulo
;;>= positive? min floor
;;< negative? abs ceiling
;;> even? gcd truncate
;; odd? lcm round
;We consulted R5RS(link in readme) for specifications of procedures
(define =
(lambda (x y)
(if (and (number? x) (number? y))
(and (<= x y ) (<= y x))
(evaluationError "= expects numbers as input"))))
;to be used later
(define not
(lambda (x)
(if x
#f
#t)))
(define >=
(lambda (x y)
(if (and (number? x) (number? y))
(or (= x y) (not (<= x y)))
(evaluationError ">= expects numbers as input"))))
(define <
(lambda (x y)
(if (and (number? x) (number? y))
(and (not (= x y)) (<= x y))
(evaluationError "< expects numbers as input"))))
(define >
(lambda (x y)
(if (and (number? x) (number? y))
(and (not (= x y)) (>= x y))
(evaluationError "> expects numbers as input"))))
(define zero?
(lambda (x)
(if (number? x)
(= x 0)
(evaluationError "zero? expects a number as input"))))
;Equal? recursively compares the contents of pairs, vectors, and strings
;applying eqv? on other objects such as numbers and symbols.
;since we have eq? already, we will use eq? instead as -
;"Eq? and eqv? are guaranteed to have the same behavior
;on symbols, booleans, the empty list, pairs,
;procedures, and non-empty strings and vectors."
(define equal?
(lambda (x y)
(if (and (pair? x) (pair? y))
(letrec ((helper
(lambda (x y)
(if (not (equal? (car x) (car y)))
#f
(equal? (cdr x) (cdr y))))))
(helper x y))
(eq? x y))))
(define positive?
(lambda (x)
(if (number? x)
(> x 0)
(evaluationError "positive? expects a real number as input"))))
(define negative?
(lambda (x)
(if (number? x)
(< x 0)
(evaluationError "negative? expects a real number as input"))))
;Floor returns the largest integer not larger than x.
;display a bit different from DrRacket in terms of float format/int format
(define floor
(lambda (x)
(if (number? x)
(if (integer? x)
x
(letrec ((helper(lambda (x)
(cond ((positive? x)
(if (< x 1)
0
(+ 1 (floor (- x 1)))))
((negative? x)
(if (> x -1)
-1
(- (floor (+ x 1)) 1)))
(else 0)))))
(helper x)))
(evaluationError "floor expects a real number as input"))))
;Ceiling returns the smallest integer not smaller than x
;display a bit different from DrRacket in terms of float format/int format
(define ceiling
(lambda (x)
(if (number? x)
(if (integer? x)
x
(letrec ((helper
(lambda (x)
(if (positive? x)
(if (< x 1)
1
(+ 1 (ceiling (- x 1))))
;;case of negative
(if (> x -1)
;Dr.Racket prints -0.0
;but for the sake of clarity
;we decide to print 0.0
0
(- (ceiling (+ x 1)) 1))))))
(helper x)))
(evaluationError "ceiling expects a real number as input"))))
;remainder
(define modulo
(lambda (x y)
(if (and (integer? x) (integer? y))
(- x (* y (floor (/ x y))))
(evaluationError "modulo expects integers as input"))))
;Truncate returns the integer closest to x whose absolute value
;is not larger than the absolute value of x
;print in format of float number as the behavior of Dr.Racket
(define truncate
(lambda (x)
(if (number? x)
(if (integer? x)
x
(letrec ((helper
(lambda (x)
(if (positive? x)
(if (< x 1)
0
(+ 1 (truncate (- x 1))))
;;case of negative
(if (> x -1)
;Dr.Racket prints -0.0
;but for the sake of clarity
;we decide to print 0.0
0
(- (truncate (+ x 1)) 1))))))
(helper x)))
(evaluationError "truncate expects a real number as input"))))
;Round returns the closest integer to x,
;rounding to even when x is halfway between two integers.
;print in format of float number as the behavior of Dr.Racket
(define round
(lambda (x)
(if (number? x)
(cond ((integer? x) x)
((>= x (+ (floor x) 0.5)) (ceiling x))
(else (floor x)))
(evaluationError "round expects a real number as input"))))
;take in integer
(define even?
(lambda (x)
(if (integer? x)
(zero? (modulo x 2))
(evaluationError "even? expects a integer as input"))))
(define odd?
(lambda (x)
(if (integer? x)
(not (even? x))
(evaluationError "odd? expects a integer as input"))))
;These procedures return the maximum or minimum of their arguments.
;currently take in two arguments as default
;will try to make them variadic.
(define max
(lambda (x y)
(if (and (number? x) (number? y))
(if (>= x y)
x
y)
(evaluationError "max expects real numbers as input"))))
(define min
(lambda (x y)
(if (and (number? x) (number? y))
(if (<= x y)
x
y)
(evaluationError "min expects real numbers as input"))))
(define abs
(lambda (x)
(if (number? x)
(if (positive? x)
x
(* -1 x))
(evaluationError "abs a expects real numbers as input"))))
;using idea of Euclid Algorithm
;because modulo is adopted and modulo assumed integer
;will only deal with integers in this function
;will try to make this variadic,
;but currently take in two arguments as default
;greatest common divisor of their arguments.
;The result is always non-negative.
;if no inputs or inputs are 0, return 0 as of DrRacket's behavior
(define gcd
(lambda (x y)
(if (and (integer? x) (integer? y))
(letrec ((helper (lambda (x y)
(cond ((zero? x) (abs y))
((zero? y) (abs x))
((zero? (modulo x y)) (abs y))
(else (gcd y (modulo x y)))))))
(helper (if (>= (abs x) (abs y)) x y)
(if (>= (abs x) (abs y)) y x)))
(evalError "gcd expects integers as input"))))
(define lcm
(lambda (x y)
(if (and (integer? x) (integer? y))
(cond ((or (zero? x) (zero? y)) 0)
((zero? (modulo x y)) x)
((zero? (modulo y x)) y)
(else (/ (abs (* x y)) (gcd x y))))
(evaluationError "lcm expects integers as input"))))
(= 3 5)
(zero? -3)
(max 7 0)
(min 3 4)
;(eq? (quote s) 4)
(eq? 3 4)
(= 3 3.0)