-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab 2.rkt
More file actions
335 lines (252 loc) · 11 KB
/
Lab 2.rkt
File metadata and controls
335 lines (252 loc) · 11 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
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
#lang eopl
#|-------------------------------------------------------------------------------
| Name:Connor Haaf
| Pledge:I pledge my honor that I have abided by the Stevens honor system.
|-------------------------------------------------------------------------------|#
#| Some reminders:
| Each progamming assignment must be completed individually.
| If your code does not compile, you will receive a 0.
| Before submitting your code, try test cases on every function
| to make sure that they run.
| If one of your functions is breaking the whole program due to an error,
| you are better off commenting out the body of that function
| than submitting code that doesn't run.
|#
#|-------------------------------------------------------------------------------|
| Lab 2: Lists (20 PTS) |
|-------------------------------------------------------------------------------|#
#| This lab serves as an introduction to working with lists in Racket.
| All lists in Racket are linked lists,
| meaning lists are either null [empty],
| or a pair of a head [the first element]
| and a tail [the rest of the list].
| This means you can only directly access the first element of a list.
| To access subsequent elements, you have to repeatedly access tails.
|
| Racket has the tick operator ' which is a shorthand for the "quote" function.
| The tick operator tells the Racket interpreter to treat an expression as a literal
| rather than try to evaluate it.
| By quoting a sequence of things in parentheses, a list is produced.
| For example, (quote (+ 1 2)), or equivalently '(+ 1 2),
| will yield the list (+ 1 2), rather than 3.
| When you quote a list, it quotes each sub-expression in the list,
| allowing you to create nested lists with just one tick mark.
|#
#| The foundational building block of every list is the empty list,
| which is expressed in the following ways (the first is most common):
| '()
| empty
| (list)
|
| To construct a list of multiple values, use the "list" function.
| (list 1 2 3) -> (1 2 3)
| (list (+ 1 2)) -> (3) <- notice how this differs from (quote (+ 1 2))
| (list 'a 'b) -> (a b) <- '(a b) produces the same result
|#
#| Here are some useful built-in list functions, where L is a list and E is some element:
| (null? L) returns #t if L is empty, otherwise #f.
| (length L) returns the number of elements in L.
| (reverse L) returns L in reverse order.
| (cons E L) returns L with E added to the front of the list.
| (append L1 L2) returns a list of L1 followed by L2.
| (car L) returns the first element of L. It throws an exception when given an empty list.
| (cdr L) returns the tail [everything but the first element] of L. It throws an exception when given an empty list.
|
| You may not need all of these functions for this lab, but they may be useful in the future.
|#
#|-------------------------------------------------------------------------------|
| Part 1: List Manipulation (10 PTS) |
|-------------------------------------------------------------------------------|#
#| Implement "name" to accept a first and last name,
| and return a list of the first and last name.
| Example:
| (name "Sandeep" "Bhatt") -> ("Sandeep" "Bhatt")
|#
;; Type signature: (name string string) -> (string string)
;; 1 PTS
(define (name first last)
(list first last))
#| Implement "last-name" to accept a list of a first and last name
| [like those constructed by the "name" function] and return the last name.
| Example:
| (last-name '("Sandeep" "Bhatt")) -> "Bhatt"
| (last-name (name "Jared" "Pincus")) -> "Pincus"
|#
;; Type signature: (last-name (string string)) -> string
;; 1 PTS
(define (last-name name)
(cdr name))
#| Implement "yoda" to take a three-part sentence [as a list]
| and return the sentence in Yoda-speak.
| In other words, (w1 w2 w3) becomes (w3 w1 w2).
| You may assume the input list has exactly 3 elements.
|
| Examples:
| (yoda '(I love Racket)) -> (Racket I love)
| (yoda '(You are strong-with-the-force)) -> (strong-with-the-force You are)
| (yoda '(Sandeep-Bhatt has a-shiny-head)) -> (a-shiny-head Sandeep-Bhatt has)
|#
;; Type signature: (yoda 3-element-list) -> 3-element-list
;; 2 PTS
(define (yoda words)
(list(cddr words )(car words)(cadr words)))
#| Implement "pig-latin" to accept a word as a list of characters
| and return that word following the rules of Pig Latin.
| You translate a word into Pig Latin by relocating the first letter
| to the end of the word, then adding the suffix "ay".
| The experienced Pig Latin speakers among you will know that sometimes
| you have to move more than one letter from the front of the word to the end,
| but here we'll ignore that rule. You merely need to move the first letter.
| You may assume that the input word is at least one letter long.
|
| Examples:
| (pig-latin '(h a p p y)) -> (a p p y h a y)
| (pig-latin '(b i r t h d a y)) -> (i r t h d a y b a y)
| (pig-latin '(t r u e)) -> (r u e t a y)
|#
;; Type signature: (pig-latin list) -> list
;; 3 PTS
(define (pig-latin word)
(list (cdr word) (car word) 'ay))
#| Implement "quad-roots" to accept integers a, b, and c,
| and return the two roots of the quadratic a*x^2 + b*x + c.
| As a refresher, the two roots are equal to
| (-b ± sqrt(b^2 - 4*a*c)) / (2*a).
| Format the function's output as a list of
| the root produced with minus, followed by
| the root produced with plus.
|
| While Racket does have support for complex numbers,
| you may assume that the roots of the input quadratic
| will not include imaginary components.
| You may also assume that a ≠ 0.
| Your output for the provided test cases may not exactly match
| the provided output. For example, decimals may be represented in fractional form.
| As long as the values are equivalent mathematically, then your output is correct.
|
| Subdefinitions may help to make this function much cleaner!
|
| Examples:
| (quad-roots -3 0 0) -> (0 0)
| (quad-roots 2 0 -2) -> (-1 1)
| (quad-roots -3 15 -18) -> (3 2)
| (quad-roots 8 6 -5) -> (-1.25 0.5)
| (quad-roots 1 -1 -1) -> (-0.618... 1.618...)
| (quad-roots -5 2 2) -> (0.863... -0.463...)
|#
;; Type signature: (quad-roots nonzero-int int int) -> (real-number real-number)
;; 3 PTS
(define (quad-roots a b c)
(define (middle a b c)
(sqrt (* * *(expt b 2) 4 a c)))
(list /(-(*(b -1)(middle a b c)))(* 2 a))
)
#|-------------------------------------------------------------------------------|
| Part 2: Nested Lists (10 PTS) |
|-------------------------------------------------------------------------------|#
#| Implement "rotate" to accept an xy-coordinate c
| and return a list of the four rotations of c around the origin.
| These rotations should be in the order 0°, 90° clockwise, 180°, 270° clockwise.
| As a refresher, the 90° clockwise rotation of
| an arbitrary coordinate (x,y) is (y,-x).
| Consider using subdefinitions to avoid repeatedly accessing the components of c.
|
| Examples:
| (rotate '(0 1)) -> ((0 1) (1 0) (0 -1) (-1 0))
| (rotate '(2 3)) -> ((2 3) (3 -2) (-2 -3) (-3 2))
| (rotate '(-5 7)) -> ((-5 7) (7 5) (5 -7) (-7 -5))
|#
;; Type signature: (rotate (int int)) -> ((int int) (int int) (int int) (int int))
;; 2 PTS
(define (rotate c)
(list(car c) (cdr c) (cdr c) (-((car c) *(2 (car c)))) (-((cdr c) *(2 (cdr c))))))
#| For the remaining functions in this lab, we'll be working with
| an informally defined data structure called a "student".
| A student is represented by a nested list with the following structure:
|#
(define student
'((id-number degree)
(last-name first-name)
(birth-day birth-month birth-year)
(class-year ((major) (minor)) gpa)
((number street apt) (city state zip))
(course1 course1 ... coursen)))
;; Here are two example students for testing your functions:
(define stu1
'((12345 "Bachelor of Science")
("Dongle" "Jonathy")
(29 "February" 1999)
(2021 (("Computer Science") ("Math")) 3.75)
((5 "Bubble Street" 16) ("Hoboken" "NJ" "07030"))
("CS-334" "CS-385" "MA-331" "BT-353")))
(define stu2
'((10101010101 "Bachelor of Science")
("Sprimpling" "Sir Ardlinton")
(1 "December" 1852)
(1874 (("Engineering") ("Literature")) 4.000001)
((999 "Road Street" 11) ("Old Town Place" "MA" "00001"))
("MA-121" "CAL-103" "PE-200" "CH-115" "E-101")))
;; Here's an example function which returns the entire birthday of a student:
(define (get-birthday student)
(car (cdr (cdr student))))
#| Since the birthday is the third element in the student list, we can access it
| by dropping the first two elements (by using cdr twice),
| then using car to get the first remaining element.
|
| Racket has extra functions for shorthands of nesting car and cdr,
| so the body of get-birthday could equivalently be written as (caddr student).
| A shorthand function exists for every permutation of up to 4 car's and cdr's. Here they all are:
| https://docs.racket-lang.org/reference/pairs.html#%28part._.Pair_.Accessor_.Shorthands%29
|#
;; Now implement the following functions for accessing parts of the student datatype:
#| "get-id-number" returns the id-number field.
| Examples:
| (get-id-number stu1) -> 12345
| (get-id-number stu2) -> 10101010101
|#
;; Type signature: (get-id-number student) -> id-number
;; 1 PTS
(define (get-id-number s)
(car (car s)))
#| "get-address" returns the fields which make up the address.
| Examples:
| (get-address stu1) -> ((5 "Bubble Street" 16) ("Hoboken" "NJ" "07030"))
| (get-address stu2) -> ((999 "Road Street" 11) ("Old Town Place" "MA" "00001"))
|#
;; Type signature: (get-address student) -> ((number street apt) (city state zip))
;; 1 PTS
(define (get-address s)
(list (car (cddddr s))))
#| "get-gpa" returns the gpa field.
| Examples:
| (get-gpa stu1) -> 3.75
| (get-gpa stu2) -> 4.000001
|#
;; Type signature: (get-gpa student) -> gpa
;; 1 PTS
(define (get-gpa s)
(cddr(car(cdddr s))))
#| "get-state" returns the state field.
| Consider taking advantage of another function you already wrote
| for implementing this one!
| Examples:
| (get-state stu1) -> "NJ"
| (get-state stu2) -> "MA"
|#
;; Type signature: (get-state student) -> state
;; 2 PTS
(define (get-state s)
(list (car (cdr (cdddr s)))))
#| Now implement "combine courses", which accepts two students
| and returns a list of the first student's courses
| followed by the second student's courses.
| Consider cleaning this function up with a subdefinition
| or external helper function.
| Examples:
| (combine-courses stu1 stu2)
| -> ("CS-334" "CS-385" "MA-331" "BT-353" "MA-121" "CAL-103" "PE-200" "CH-115" "E-101")
|#
;; Type signature: (combine-courses student student) -> string-list
;; 3 PTS
(define (combine-courses s1 s2)
(list (car (cdr (cddddr s1)))(car (cdr (cddddr s2))) ))