-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_sets.rkt~
255 lines (171 loc) · 6.86 KB
/
lab_sets.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
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
#lang eopl
;;-------------------------------------------------------------------------------
;; Name:
;; Pledge:
;;-------------------------------------------------------------------------------
;; In this lab, you'll write some basic functions which operate on sets.
;; Scheme doesn't have a built-in "set" datatype,
;; so we'll use the list datatype as a stand-in for sets.
;; We'll make sure these lists don't contain duplicates,
;; and we won't care about the order of elements in the lists,
;; so for all intents and purposes they'll be sets!
;; So, in this lab, we can treat sets as lists,
;; but we can't treat regular lists as sets.
;; Here's a helper function which converts a list
;; to a set by removing duplicate elements.
;; It'll be useful for the subsequent functions you implement.
;; Type signature: (make-set list) -> set
(define (make-set L)
(cond [(null? L) '()]
[(member (car L) (cdr L)) (make-set (cdr L))]
[else (cons (car L) (make-set (cdr L)))]))
;;_______________________________________________________________________________
;; As you complete the following functions,
;; see if you can utilize them in the subsequent ones!
;; Take particular note of the inputs and outputs of each function
;; to confirm whether the expected types are lists or sets.
;; For all of the functions whose outputs are sets,
;; your outputs will be considered correct if they contain the correct elements,
;; regardless of their order.
;; Define element?
;; Given an item e and a list of items L,
;; returns #t if e is in L, #f otherwise.
;; Hint: to compare equality of any object type, use "equal?".
;; Examples:
;; (element? 0 '()) => #f
;; (element? 8 '(7 8 9)) => #t
;; (element? 7 '(1 2 3 4)) => #f
;; (element? 'saw '(the man saw a dog)) => #t
;; (element? 'sa '(the man saw a dog)) => #f
;; Type signature: (element? item list) -> boolean
(define (element? e L)
"Not implemented")
;; Define union
;; Given two lists, returns a set containing all of the elements from either list.
;; Remember: for the functions that return a set,
;; the order of the elements in the output doesn't need to exactly match the examples.
;; Examples:
;; (union '(1 2 3) '(4 5 6)) => '(1 2 3 4 5 6)
;; (union '(1 2 3) '(0 1 2 3)) => '(0 1 2 3)
;; (union '(1 1 1) '()) => '(1)
;; Type signature: (union list list) -> set
(define (union LA LB)
"Not implemented")
;; Define intersection
;; Given two lists A and B, returns the set
;; containing all elements in both A and B.
;; Examples:
;; (intersection '(1 2 3 4) '(2 4 5)) => '(2 4)
;; (intersection '(s a n d e e p) '(b h a t t)) => '(a)
;; (intersection '(c c c) '(c a c)) => '(c)
;; (intersection '(a a a) '()) => '()
;; Type signature: (intersection list list) -> set
(define (intersection LA LB)
"Not implemented")
;; Define subset?
;; Given two sets A and B, returns whether A is a subset of B
;; (every element in A is also in B).
;; Examples:
;; (subset? '() '()) => #t
;; (subset? '(1 2 3) '(1 4 2 5 3)) => #t
;; (subset? '(115 284 385) '(115 146 284 135 385 392)) => #t
;; (subset? '(-2 0 2) '(-1 1 3)) => #f
;; (subset? '(-1 1 2) '(-1 1 3 5 7)) => #f
;; (subset? '(1 3 2) '(3 2 1)) => #t
;; Type signature: (subset? set set) -> boolean
(define (subset? SA SB)
"Not implemented")
;; Define set-equal?
;; Given two sets A and B, returns whether A = B
;; (i.e. every element in A is in B and every element in B is in A).
;; NOTE: Since the order of elements in the sets may be different,
;; you can't simply use (equal? A B).
;; Examples:
;; (set-equal? '() '()) => #t
;; (set-equal? '(a b c) '(a b c)) => #t
;; (set-equal? '(1 2 3 4) '(4 3 1 2)) => #t
;; (set-equal? '(1 2 3) '(1 2 4)) => #f
;; (set-equal? '(5 5 5 5) '(5)) => #t
;; Type signature: (set-equal? set set) -> boolean
(define (set-equal? SA SB)
"Not implemented")
;; Define set-difference
;; Given lists A and B, returns the set of A - B
;; (i.e. every element in A which is not in B).
;; Examples:
;; (set-difference '(1 2 3) '(2 3 4)) => '(1)
;; (set-difference '(1 2 3) '(1 2 3)) => '()
;; (set-difference '(1 2 3) '(4 5 6)) => '(1 2 3)
;; (set-difference '() '(1 2 3)) => '()
;; (set-difference '(1 1 2 3 3) '()) => '(1 2 3)
;; Type signature: (set-difference list list) -> set
(define (set-difference LA LB)
"Not implemented")
;; Define sym-diff
;; Given two lists A and B, returns the symmetric difference of A and B as sets,
;; which equals the union of A - B and B - A (i.e. every element in exactly one of the lists).
;; Examples:
;; (sym-diff '(1 2 3) '(3 4 5)) => '(1 2 4 5)
;; (sym-diff '(1 2 3) '(4 5 6)) => '(1 2 3 4 5 6)
;; (sym-diff '(1 2 3) '(1 2 3)) => '()
;; (sym-diff '(1 2) '(1 2 3 4)) => '(3 4)
;; (sym-diff '(1 1 1) '()) => '(1)
;; Type signature: (sym-diff list list) -> set
(define (sym-diff LA LB)
"Not implemented")
;; Define cardinality
;; Given a list L, returns |L|,
;; the number of unique elements in L.
;; Examples:
;; (cardinality '(1 2 3)) => 3
;; (cardinality '(1 1 2 3 3)) => 3
;; (cardinality '(5 5 5 5 5)) => 1
;; (cardinality '()) => 0
;; Type signature: (cardinality list) -> int
(define (cardinality L)
"Not implemented")
;; Define disjoint
;; Given two sets, returns if the sets are disjoint
;; (i.e. they have no elements in common).
;; Examples:
;; (disjoint? '(1 2 3) '()) => #t
;; (disjoint? '(1 2 3) '(1)) => #f
;; (disjoint? '(1 2 3) '(4 5 6)) => #t
;; Type signature: (disjoint? set set) -> boolean
(define (disjoint? SA SB)
"Not implemented")
;; Define superset?
;; Given sets A and B, returns whether A is a superset of B
;; (i.e. every element in B is in A).
;; Examples:
;; (superset? '() '()) => #t
;; (superset? '(1 2 3 4 5) '(1 2 3)) => #t
;; (superset? '(-1 1 3) '(-2 0 2)) => #f
;; Type signature: (superset? set set) -> boolean
(define (superset? SA SB)
"Not implemented")
;; Define insert
;; Given a list L and an item e,
;; returns the set of L with e added to the set.
;; Remember, if the new element was already in the set,
;; the resultant set does not change.
;; Examples:
;; (insert 0 '(1 2 3)) => '(0 1 2 3)
;; (insert 1 '(1 2 3)) => '(1 2 3)
;; (insert 0 '(0 0 0)) => '(0)
;; Type signature: (insert element list) -> set
(define (insert e L)
"Not implemented")
;; Define remove
;; Given a set S and an item e,
;; returns S with e removed from it.
;; If e was not in S to begin with,
;; the function should return S unchanged.
;; (remove 2 '(1 2 3)) => '(1 3)
;; (remove 3 '(3)) => '()
;; (remove 4 '(1 2 3)) => '(1 2 3)
;; Type signature: (remove element set) -> set
(define (remove e S)
"Not implemented")
;; Created January 2018 by Samuel Kraus and Edward Minnix
;; Updated February 2020 by Jared Pincus