-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab8.scm
359 lines (287 loc) · 7.17 KB
/
lab8.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
;; ====== LAB 8 ======
;; Author(s): Nishad Trivedi
;;
;; Lab Section: 11
;;;; Utility Functions
(load "lab7.scm")
;; Reloads the current file.
(define (reload)
(load "lab8.scm") ; Change file name if copied to a new file.
)
;; Square
(define (square x) (* x x))
;;;; Test Case Values:
;;;; These are to help simplify the test cases by reusing the lists.
;; Lists
(define test-list1 '(1 2 3 4 5 6 7 8 9))
(define test-list2 '(5))
(define test-list3 '(1 2))
(define test-list4 '())
(define test-list5 '(1 2 3 4))
;; Trees
(define test-tree1 '(1 (2 3) 4))
(define test-tree2 '((1 2 3 4)))
(define test-tree3 '(1 ((2) 3) (4)))
(define test-tree4 '((1 (2 (3 (4 (5)))))))
(define test-tree5 '(((((1) 2) 3) 4) 5))
(define test-tree6 '((((1 2) 3 4) 5 6) 7 8))
;;;; Test Case Code:
;;;; This will handle execution of the test cases we've included below.
;;;; To run test cases for a step, uncomment the (do-tests #) line.
;;;; Note: This code will run on MIT Scheme, but would have to be modified
;;;; to work with other versions of Scheme.
;;;; Change #t to #f in the line below to use for Dr Scheme / STk.
;;;; Behavior under Dr Scheme / STk is not tested.
(define (do-tests n)
(let* ((in-mit-scheme #t) ;; ** Change this value
(tests-symbol
(string->symbol
(string-append "test-cases-step-"
(number->string n))))
(test-cases
(if in-mit-scheme
(eval tests-symbol user-initial-environment)
(eval tests-symbol)))
(display-string (string-append
"\n--- Test Cases for Step "
(number->string n)
" ---\n")))
(display display-string)
(for-each
(lambda (x)
(if (and (pair? x) (eq? (car x) 'define))
(if in-mit-scheme
(eval x user-initial-environment)
(eval x))
(begin
(display x)
(newline)
(display (if in-mit-scheme
(eval x user-initial-environment)
(eval x)))
(newline))))
test-cases)))
;;;;
;;;; Step 1 - Skipping Over Elements
;;;;
;; get-tail
;; returns the remaining list after `index` elements
;; INPUTS: lst, a list ; index, an integer
;; OUTPUT: list
(define (get-tail lst index)
(cond ((null? lst) ())
((<= index 0) lst)
(else
(get-tail (cdr lst) (- index 1)))))
;;=============;;
;; Test Code ;;
;;=============;;
(define test-cases-step-1
'(
(get-tail test-list1 0)
(get-tail test-list1 6)
(get-tail test-list1 8)
(get-tail test-list1 9)
(get-tail test-list1 10)
(get-tail test-list2 0)
(get-tail test-list2 1)
(get-tail test-list2 2)
(get-tail test-list3 0)
(get-tail test-list3 1)
(get-tail test-list3 2)
(get-tail test-list4 0)
(get-tail test-list4 5)
(get-tail test-list5 -1)
))
(do-tests 1)
;;;;
;;;; Step 2 - Yippy Skippy
;;;;
;; skip
;; returns the last element of a list
;; INPUTS: a list
;; OUTPUT: last element of list
(define (skip lst)
(cond ((not (pair? lst)) lst)
((null? (cdr lst)) (car lst))
(else
(skip (cdr lst)))))
;;=============;;
;; Test Code ;;
;;=============;;
(define test-cases-step-2
'(
(skip test-list1)
(skip test-list2)
(skip test-list3)
(skip test-list4)
(skip test-list5)
))
(do-tests 2)
;;;;
;;;; Step 3 - Reverse
;;;;
;; reverse
;; reverses the order of a list
;; INPUTS: list
;; OUTPUT: reverse of list
(define (reverse lst)
(define (helper lst val)
(cond ((null? lst) val)
(else
(helper (cdr lst) (cons (car lst) val)))))
(helper lst ()))
;;=============;;
;; Test Code ;;
;;=============;;
(define test-cases-step-3
'(
(reverse test-list1)
(reverse test-list2)
(reverse test-list3)
(reverse test-list4)
(reverse test-list5)
))
(do-tests 3)
;;;;
;;;; Step 4 - Accepting a Variable Number of Arguments
;;;;
;; getargs
;; returns a list of any arguments passed to it
;; INPUTS: any
;; OUTPUT: list
(define (getargs . args) args)
;; howmanyargs?
;; counts the number of args passed to it
;; INPUTS: any
;; OUTPUT: integer
(define (howmanyargs? . args)
(define (helper args)
(cond ((null? args) 0)
((pair? (car args)) (+ 1 (helper (cdr args))))
(else
(helper (cdr args)))))
(helper args))
;;=============;;
;; Test Code ;;
;;=============;;
(define test-cases-step-4
'(
(getargs)
(getargs 1)
(getargs 1 2)
(getargs 1 2 3)
(getargs 1 2 3 4)
(getargs 1 (list 2 3) 4)
(howmanyargs?)
(howmanyargs? 1)
(howmanyargs? (list 1))
(howmanyargs? (list 1) 2)
(howmanyargs? (cons 1 2) 3)
(howmanyargs? (cons 1 2) (cons 3 4))
(howmanyargs? (cons 1 2) (list 3 4))
(howmanyargs? 1 (list 2 3) 4)
))
(do-tests 4)
;;;;
;;;; Step 5 - Mapping
;;;;
;; square-list using cons
;; squares each number in a list
(define (square-list-cons items)
(if (null? items)
'()
(cons (square (car items)) (square-list-cons (cdr items)))))
;; square-list using map
;; squares each number in a list
(define (square-list-map items)
(map square items))
;;=============;;
;; Test Code ;;
;;=============;;
(define test-cases-step-5
'(
(square-list-cons test-list1)
(square-list-cons test-list2)
(square-list-cons test-list3)
(square-list-cons test-list4)
(square-list-cons test-list5)
(square-list-map test-list1)
(square-list-map test-list2)
(square-list-map test-list3)
(square-list-map test-list4)
(square-list-map test-list5)
))
(do-tests 5)
;;;;
;;;; Step 6 - Deep-Reverse
;;;;
;; deep-reverse
;; reverses lists and nested lists
;; INPUTS: list
;; OUTPUT: reverse of list
(define (deep-reverse lst)
(define (helper lst val)
(cond ((null? lst) val)
((not (pair? lst)) lst)
(else
(helper (cdr lst) (cons (deep-reverse (car lst)) val)))))
(helper lst ()))
;;=============;;
;; Test Code ;;
;;=============;;
(define test-cases-step-6
'(
(deep-reverse test-list1)
(deep-reverse test-list2)
(deep-reverse test-list3)
(deep-reverse test-list4)
(deep-reverse test-list5)
(deep-reverse test-tree1)
(deep-reverse test-tree2)
(deep-reverse test-tree3)
(deep-reverse test-tree4)
(deep-reverse test-tree5)
(deep-reverse test-tree6)
))
(do-tests 6)
;;;;
;;;; Step 7 - Using Accumulate
;;;;
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
;; map
(define (my-map proc sequence)
(accumulate (lambda (x y) (cons (proc x) y)) () sequence))
;; append
(define (my-append seq1 seq2)
(accumulate cons seq2 seq1))
;; length
(define (my-length sequence)
(accumulate (lambda (x y) (+ 1 y)) 0 sequence))
;;=============;;
;; Test Code ;;
;;=============;;
(define test-cases-step-7
'(
(my-map square test-list4)
(my-map square test-list2)
(my-map square test-list3)
(my-map square test-list5)
(my-append test-list4 test-list5)
(my-append test-list5 test-list4)
(my-append test-list3 test-list3)
(my-append test-list2 test-list3)
(my-length test-list1)
(my-length test-list2)
(my-length test-list3)
(my-length test-list4)
(my-length test-list5)
(my-length test-tree1)
(my-length test-tree2)
(my-length test-tree3)
))
(do-tests 7)