-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations.rkt
219 lines (191 loc) · 8.13 KB
/
operations.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
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
#lang racket
(require (except-in eopl #%module-begin))
(define (is-all-num list)
(cond
[(null? list) #t]
[(number? (car list)) (is-all-num (cdr list))]
[else #f]
))
(define (is-all-boolean list)
(cond
[(null? list) #t]
[(boolean? (car list)) (is-all-boolean (cdr list))]
[else #f]
))
(define (is-all-str list)
(cond
[(null? list) #t]
[(string? (car list)) (is-all-str (cdr list))]
[else #f]
))
(define (greater-list-num? list num)
(cond
[(boolean=? (is-all-num list) #f) (eopl:error "The list elements must be numbers.")]
[(null? list) #t]
[(<= (car list) num) #f]
[else (greater-list-num? (cdr list) num)]
))
(define (less-list-num? list num)
(cond
[(boolean=? (is-all-num list) #f) (eopl:error "The list elements must be numbers.")]
[(null? list) #t]
[(>= (car list) num) #f]
[else (less-list-num? (cdr list) num)]
))
(define (greater-num-list? num list)
(cond
[(boolean=? (is-all-num list) #f) (eopl:error "The list elements must be numbers.")]
[(null? list) #t]
[(<= num (car list)) #f]
[else (greater-num-list? num (cdr list))]
))
(define (less-num-list? num list)
(cond
[(boolean=? (is-all-num list) #f) (eopl:error "The list elements must be numbers.")]
[(null? list) #t]
[(>= num (car list)) #f]
[else (less-num-list? num (cdr list))]
))
(define (greater-list-str? list str)
(cond
[(boolean=? (is-all-str list) #f) (eopl:error "The list elements must be strings.")]
[(null? list) #t]
[(string<=? (car list) str) #f]
[else (greater-list-str? (cdr list) str)]
))
(define (less-list-str? list str)
(cond
[(boolean=? (is-all-str list) #f) (eopl:error "The list elements must be strings.")]
[(null? list) #t]
[(string>=? (car list) str) #f]
[else (less-list-str? (cdr list) str)]
))
(define (greater-str-list? str list)
(cond
[(boolean=? (is-all-str list) #f) (eopl:error "The list elements must be strings.")]
[(null? list) #t]
[(string<=? str (car list)) #f]
[else (greater-str-list? str (cdr list))]
))
(define (less-str-list? str list)
(cond
[(boolean=? (is-all-str list) #f) (eopl:error "The list elements must be strings.")]
[(null? list) #t]
[(string>=? str (car list)) #f]
[else (less-str-list? str (cdr list))]
))
(define (list-equal? l1 l2)
(cond
[(and (null? l1) (null? l2)) #t]
[(and (null? l1) (not (null? l2))) #f]
[(and (not (null? l1)) (null? l2)) #f]
[(and (number? (car l1))(equal? (car l1) (car l2))) (list-equal? (cdr l1) (cdr l2))]
[(and (string? (car l1))(string=? (car l1) (car l2))) (list-equal? (cdr l1) (cdr l2))]
[(and (boolean? (car l1))(boolean=? (car l1) (car l2))) (list-equal? (cdr l1) (cdr l2))]
[(and (list? (car l1))(list-equal? (car l1) (car l2))) (list-equal? (cdr l1) (cdr l2))]
[else #f]
))
(define (boolean-or b1 b2) (or b1 b2))
(define (boolean-and b1 b2) (and b1 b2))
(define (list-op-num L operand num)
(cond
[(boolean=? (is-all-num L) #f) (eopl:error "The list elements must be numbers.")]
[(null? L) null]
[else (append (list(arithmetic-op (car L) operand num)) (list-op-num (cdr L) operand num))]
))
(define (num-op-list L operand num)
(cond
[(boolean=? (is-all-num L) #f) (eopl:error "The list elements must be numbers.")]
[(null? L) null]
[else (append (list(arithmetic-op num operand (car L))) (num-op-list (cdr L) operand num))]
))
(define (list-op-boolean L operand num)
(cond
[(boolean=? (is-all-boolean L) #f) (eopl:error "The list elements must be booleans.")]
[(null? L) null]
[else (append (list(arithmetic-op (car L) operand num)) (list-op-boolean (cdr L) operand num))]
))
(define (str-plus-list L str)
(cond
[(boolean=? (is-all-str L) #f) (eopl:error "The list elements must be strings.")]
[(null? L) null]
[else (append (list(string-append str (car L))) (str-plus-list (cdr L) str))]
))
(define (list-plus-str L str)
(cond
[(boolean=? (is-all-str L) #f) (eopl:error "The list elements must be strings.")]
[(null? L) null]
[else (append (list(string-append (car L) str)) (list-plus-str (cdr L) str))]
))
(define (greater? arg1 arg2)
(cond
[(and (number? arg1) (number? arg2) (> arg1 arg2)) #t]
[(and (number? arg1) (number? arg2) (< arg1 arg2)) #f]
[(and (number? arg1) (number? arg2) (= arg1 arg2)) #f]
[(and (string? arg1) (string? arg2) (string>? arg1 arg2)) #t]
[(and (string? arg1) (string? arg2) (string<? arg1 arg2)) #f]
[(and (string? arg1) (string? arg2) (string=? arg1 arg2)) #f]
[(and (list? arg1) (number? arg2)) (greater-list-num? arg1 arg2)]
[(and (number? arg1) (list? arg2)) (greater-num-list? arg1 arg2)]
[(and (list? arg1) (string? arg2)) (greater-list-str? arg1 arg2)]
[(and (string? arg1) (list? arg2)) (greater-str-list? arg1 arg2)]
[else (eopl:error "two types are not comparable.")]
))
(define (less? arg1 arg2)
(cond
[(and (number? arg1) (number? arg2) (< arg1 arg2)) #t]
[(and (number? arg1) (number? arg2) (> arg1 arg2)) #f]
[(and (number? arg1) (number? arg2) (= arg1 arg2)) #f]
[(and (string? arg1) (string? arg2) (string<? arg1 arg2)) #t]
[(and (string? arg1) (string? arg2) (string>? arg1 arg2)) #f]
[(and (string? arg1) (string? arg2) (string=? arg1 arg2)) #f]
[(and (list? arg1) (number? arg2)) (less-list-num? arg1 arg2)]
[(and (number? arg1) (list? arg2)) (less-num-list? arg1 arg2)]
[(and (list? arg1) (string? arg2)) (less-list-str? arg1 arg2)]
[(and (string? arg1) (list? arg2)) (less-str-list? arg1 arg2)]
[else (eopl:error "two types are not comparable.")]
))
(define (equality? arg1 arg2)
(cond
[(and (number? arg1) (number? arg2) (= arg1 arg2)) #t]
[(and (number? arg1) (number? arg2) (> arg1 arg2)) #f]
[(and (number? arg1) (number? arg2) (< arg1 arg2)) #f]
[(and (string? arg1) (string? arg2) (string=? arg1 arg2)) #t]
[(and (string? arg1) (string? arg2) (string>? arg1 arg2)) #f]
[(and (string? arg1) (string? arg2) (string<? arg1 arg2)) #f]
[(and (null? arg1) (null? arg2)) #t]
[(and (boolean? arg1) (boolean? arg2) (boolean=? arg1 #t) (boolean=? arg2 #t)) #t]
[(and (boolean? arg1) (boolean? arg2) (boolean=? arg1 #t) (boolean=? arg2 #f)) #f]
[(and (boolean? arg1) (boolean? arg2) (boolean=? arg1 #f) (boolean=? arg2 #f)) #t]
[(and (boolean? arg1) (boolean? arg2) (boolean=? arg1 #f) (boolean=? arg2 #t)) #t]
[(and (list? arg1) (list? arg2)) (list-equal? arg1 arg2)]
[else (eopl:error "two types are not comparable.")]
))
(define (inequality? arg1 arg2) (not (equality? arg1 arg2)))
(define (negate arg1)
(cond
[(number? arg1) (* arg1 -1)]
[(boolean? arg1) (not arg1)]
[(list? arg1) (list-op-num arg1 "*" -1)]
[else (eopl:error "Negation can't be done on the arguement.")]
))
(define (arithmetic-op arg1 arg2 arg3)
(cond
[(and (number? arg1) (number? arg3) (string=? arg2 "*")) (* arg1 arg3)]
[(and (number? arg1) (number? arg3) (string=? arg2 "+")) (+ arg1 arg3)]
[(and (number? arg1) (number? arg3) (string=? arg2 "-")) (- arg1 arg3)]
[(and (number? arg1) (number? arg3) (string=? arg2 "/") (not(= arg3 0))) (/ arg1 arg3)]
[(and (number? arg1) (number? arg3) (string=? arg2 "/") (= arg3 0)) (eopl:error "Division By Zero")]
[(and (number? arg1) (list? arg3)) (num-op-list arg3 arg2 arg1)]
[(and (list? arg1) (number? arg3)) (list-op-num arg1 arg2 arg3)]
[(and (boolean? arg1) (boolean? arg3) (string=? arg2 "*")) (boolean-and arg1 arg3)]
[(and (boolean? arg1) (boolean? arg3) (string=? arg2 "+")) (boolean-or arg1 arg3)]
[(and (boolean? arg1) (list? arg3)) (list-op-boolean arg3 arg2 arg1)]
[(and (list? arg1) (boolean? arg3)) (list-op-boolean arg1 arg2 arg3)]
[(and (string? arg1) (string? arg3) (string=? arg2 "+")) (string-append arg1 arg3)]
[(and (list? arg1) (list? arg3) (string=? arg2 "+")) (append arg1 arg3)]
[(and (string? arg1) (list? arg3) (string=? arg2 "+")) (str-plus-list arg3 arg1)]
[(and (list? arg1) (string? arg3) (string=? arg2 "+")) (list-plus-str arg1 arg3)]
[else (eopl:error "Operand can't be applied on these arguements.")]
))
(provide (all-defined-out))