Repository navigation
Expand file tree
/
Copy pathHelperFunctions.rkt
More file actions
276 lines (232 loc) · 8.71 KB
/
Copy pathHelperFunctions.rkt
File metadata and controls
276 lines (232 loc) · 8.71 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
;EECS345 Programming Project Part 2
;Professor Lewicki
;Project Partners: Peyton Turner (dpt14), Jack La Rue (jvl13), and Jessie Adkins (jsa70)
;4/12/17
;Language used: Pretty Big
;General Project Notes:
;Empty states shall have the format: ((() ()))
;---------------------------------------------------------------------------------
;Abstractions of car/cdr
;---------------------------------------------------------------------------------
;first and rest are already implemented by Pretty Big
;Functionality of caar
(define first_first
(lambda (l)
(caar l)))
;Functionality of cddr
(define rest_rest
(lambda (l)
(cddr l)))
;Functionality of cadr
(define first_rest
(lambda (l)
(cadr l)))
;Functionality of cdar
(define rest_first
(lambda (l)
(cdar l)))
;Functionality of caaar
(define first_first_first
(lambda (l)
(caaar l)))
;Functionality of caadr
(define first_first_rest
(lambda (l)
(caadr l)))
;Functionality of cadar
(define first_rest_first
(lambda (l)
(cadar l)))
;Functionality of caddr
(define first_rest_rest
(lambda (l)
(caddr l)))
;Functionality of cdaar
(define rest_first_first
(lambda (l)
(cdaar l)))
;Functionality of cdadr
(define rest_first_rest
(lambda (l)
(cdadr l)))
;Functionality of cddar
(define rest_rest_first
(lambda (l)
(cddar l)))
;Functionality of cdddr
(define rest_rest_rest
(lambda (l)
(cdddr l)))
;---------------------------------------------------------------------------------
;Operations
;---------------------------------------------------------------------------------
;Tells us if the expression is a math operation
(define is_math_op?
(lambda (expr)
(cond
((null? expr) #f)
((atom? expr) #f)
((eq? (first expr) '+) #t)
((eq? (first expr) '-) #t)
((eq? (first expr) '*) #t)
((eq? (first expr) '/) #t)
((eq? (first expr) '%) #t)
(else #f))))
;If expression is a math operation, it returns the math operation
(define get_math_op
(lambda (expr)
(cond
((eq? (first expr) '+) +)
((eq? (first expr) '-) -)
((eq? (first expr) '*) *)
((eq? (first expr) '/) quotient)
((eq? (first expr) '%) modulo))))
;Tells us if the expression is a boolean operation.
(define is_bool_op?
(lambda (expr)
(cond
((null? expr) #f)
((atom? expr) #f)
((eq? (first expr) '&&) #t)
((eq? (first expr) '||) #t)
((eq? (first expr) '!) #t)
((eq? (first expr) '==) #t)
((eq? (first expr) '!=) #t)
((eq? (first expr) '<) #t)
((eq? (first expr) '>) #t)
((eq? (first expr) '<=) #t)
((eq? (first expr) '>=) #t)
(else #f))))
;If the expression is a boolean operation, it returns the appropriate operation.
;(Not is handled in M_boolean.)
(define get_bool_op
(lambda (expr)
(cond
((eq? (first expr) '&&) and_error)
((eq? (first expr) '||) or_error)
;((eq? (first expr) '!) not_error) This should never happen, since it should be caught in M_boolean
((eq? (first expr) '==) eq_error)
((eq? (first expr) '!=) not_eq_error)
((eq? (first expr) '<) <_error)
((eq? (first expr) '>) >_error)
((eq? (first expr) '<=) <=_error)
((eq? (first expr) '>=) >=_error))))
;---------------------------------------------------------------------------------
;Type Checks
;---------------------------------------------------------------------------------
;Checks to see if inputted value is a primitive boolean.
(define is_boolean?
(lambda (val)
(cond
((null? val) #f)
((or (eq? val #t) (eq? val #f)) #t)
(else #f))))
;Checks if an object is an atom. Returns true if so.
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
;Checks if an expression is unary. Returns true if so.
(define unary?
(lambda (expr)
(eq? (rest_rest expr) '())))
;---------------------------------------------------------------------------------
;Function Helpers
;---------------------------------------------------------------------------------
;Gets the name of an input function expression.
(define get_function_name
(lambda (func)
(first func)))
;Gets the parameter list of an input function expression.
(define get_function_params
(lambda (func)
(first_rest func)))
;Gets the body of an input function expression.
(define get_function_body
(lambda (func)
(first_rest_rest func)))
;---------------------------------------------------------------------------------
;List Manipulation
;---------------------------------------------------------------------------------
;Takes three inputs: two values and a list. Adds new first elements to a list of two lists.
(define newfirsts
(lambda (f1 f2 l)
(list (cons f1 (first l)) (cons f2 (first_rest l)))))
;Takes an input list of two lists and returns a list of the first two elements of those lists
(define getfirsts
(lambda (l)
(cons (first_first l) (cons (first_first_rest l) '()))))
;Removes the first elements of the sublists of a list. Said list is comprised of two lists.
(define removefirsts
(lambda (l)
(list (rest_first l) (rest_first_rest l))))
;Takes two inputs and makes them the first two values in the lists in an input list of two lists.
(define replacefirsts
(lambda (f1 f2 l)
(newfirsts f1 f2 (removefirsts l))))
;---------------------------------------------------------------------------------
;Custom Errors
;---------------------------------------------------------------------------------
;Takes two supposedly boolean inputs and ands them if they are actually booleans.
;Otherwise throws an error
(define and_error
(lambda (bool1 bool2)
(cond
((or (null? bool1) (null? bool2)) (error "Either one or both of your values are null."))
((and (is_boolean? bool1) (is_boolean? bool2)) (and bool1 bool2))
(else (error "Attempted to treat a non-boolean as a boolean.")))))
;Takes two supposedly boolean inputs and ors them if they are actually booleans.
;Otherwise throws an error
(define or_error
(lambda (bool1 bool2)
(cond
((or (null? bool1) (null? bool2)) (error "Either one or both of your values are null."))
((and (is_boolean? bool1) (is_boolean? bool2)) (or bool1 bool2))
(else (error "Attempted to treat a non-boolean as a boolean.")))))
;Takes two values and determines whether or not they are the same value.
;These values can be of type int or boolean.
(define eq_error
(lambda (val1 val2)
(cond
((or (null? val1) (null? val2)) (error "Either one or both of your values are null."))
((or (and (is_boolean? val1) (is_boolean? val2)) (and (not (is_boolean? val1)) (not (is_boolean? val2)))) (eq? val1 val2))
(else (error "Values are of different types. Condition cannot be determined!")))))
;Takes two values and determines whether or not they are not equal.
;These values can be of type int or boolean.
(define not_eq_error
(lambda (val1 val2)
(cond
((or (null? val1) (null? val2)) (error "Either one or both of your values are null."))
((or (and (is_boolean? val1) (is_boolean? val2)) (and (not (is_boolean? val1)) (not (is_boolean? val2)))) (not (eq? val1 val2)))
(else (error "Values are of different types. Condition cannot be determined!")))))
;Takes two values and determines whether or not val1 < val2.
;Values must be of type int.
(define <_error
(lambda (val1 val2)
(cond
((or (null? val1) (null? val2)) (error "Either one or both of your values are null."))
((and (not (is_boolean? val1)) (not (is_boolean? val2))) (< val1 val2))
(else (error "One value is not of type int. Condition cannot be determined!")))))
;Takes two values and determines whether or not val1 > val2.
;Values must be of type int.
(define >_error
(lambda (val1 val2)
(cond
((or (null? val1) (null? val2)) (error "Either one or both of your values are null."))
((and (not (is_boolean? val1)) (not (is_boolean? val2))) (> val1 val2))
(else (error "One value is not of type int. Condition cannot be determined!")))))
;Takes two values and determines whether or not val1 <= val2.
;Values must be of type int.
(define <=_error
(lambda (val1 val2)
(cond
((or (null? val1) (null? val2)) (error "Either one or both of your values are null."))
((and (not (is_boolean? val1)) (not (is_boolean? val2))) (<= val1 val2))
(else (error "One value is not of type int. Condition cannot be determined!")))))
;Takes two values and determines whether or not val1 >= val2.
;Values must be of type int.
(define >=_error
(lambda (val1 val2)
(cond
((or (null? val1) (null? val2)) (error "Either one or both of your values are null."))
((and (not (is_boolean? val1)) (not (is_boolean? val2))) (>= val1 val2))
(else (error "One value is not of type int. Condition cannot be determined!")))))