-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter7.rkt
229 lines (192 loc) · 4.32 KB
/
chapter7.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
#lang r5rs
;Some auxiliary functions from previous chapters
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
(define eqan?
(lambda (a1 a2)
(cond
((and (number? a1) (number? a2))
(= a1 a2))
((or (number? a1) (number? a2)) #f)
(else (eq? a1 a2)))))
(define eqlist?
(lambda (l1 l2)
(cond
((and (null? l1) (null? l2)) #t)
((or (null? l1) (null? l2)) #f)
(else
(and (equal? (car l1) (car l2))
(eqlist? (cdr l1) (cdr l2)))))))
(define equal?
(lambda (s1 s2)
(cond
((and (atom? s1) (atom? s2))
(eqan? s1 s2))
((or (atom? s1) (atom? s2)) #f)
(else (eqlist? s1 s2)))))
(define member?
(lambda (a lat)
(cond
((null? lat) #f)
(else (or (equal? (car lat) a)
(member? a (cdr lat)))))))
(define multirember
(lambda (a lat)
(cond
((null? lat) (quote ()))
(else (cond
((eq? (car lat) a) (multirember a (cdr lat)))
(else (cons (car lat)
(multirember a
(cdr lat)))))))))
(define firsts
(lambda (l)
(cond
((null? l) (quote ()))
(else (cons (car (car l))(firsts (cdr l)))))))
;Here the Chapter 7 functions actually begin
(define set?
(lambda (lat)
(cond
((null? lat) #t)
((member? (car lat) (cdr lat)) #f)
(else (set? (cdr lat))))))
(display (set? '(algumas palavras aqui)))
(newline)
(display (set? '(mais algumais palavras mais)))
(newline)
(define makeset
(lambda (lat)
(cond
((null? lat) '())
(else (cons (car lat)
(makeset
(multirember (car lat)
(cdr lat))))))))
(newline)
(display (makeset '(oranges and apples and pears and)))
(newline)
(define subset?
(lambda (set1 set2)
(cond
((null? set1) #t)
(else
(and (member? (car set1) set2)
(subset? (cdr set1) set2))))))
(newline)
(display (subset? '(a b) '(a b c d)))
(newline)
(display (subset? '(a b c) '(a b d)))
(newline)
(define eqset?
(lambda (set1 set2)
(and (subset? set1 set2)
(subset? set2 set1))))
(newline)
(display (eqset? '(a b) '(b a)))
(newline)
(display (eqset? '(a b) '(a c)))
(newline)
(define intersect?
(lambda (set1 set2)
(cond
((null? set1) #f)
(else (or (member? (car set1) set2)
(intersect?
(cdr set1) set2))))))
(newline)
(display (intersect? '(a b c d) '(d e f g)))
(newline)
(display (intersect? '(a b c) '(d e f)))
(newline)
(define intersect
(lambda (set1 set2)
(cond
((null? set1) '())
((member? (car set1) set2)
(cons (car set1)
(intersect (cdr set1) set2)))
(else (intersect (cdr set1) set2)))))
(newline)
(display (intersect '(a b c) '(b c d)))
(newline)
(define union
(lambda (set1 set2)
(cond
((null? set1) set2)
((member? (car set1) set2)
(union (cdr set1) set2))
(else (cons (car set1)
(union (cdr set1) set2))))))
(newline)
(display (union '(a b c) '(b c d)))
(newline)
(define diff
(lambda (set1 set2)
(cond
((null? set1) '())
((member? (car set1) set2)
(diff (cdr set1) set2))
(else (cons (car set1)
(diff (cdr set1) set2))))))
(newline)
(display (diff '(a b c) '(b c d)))
(newline)
(define intersectall
(lambda (l-set)
(cond
((null? (cdr l-set)) (car l-set))
(else (intersect (car l-set)
(intersectall (cdr l-set)))))))
(newline)
(display (intersectall '((a b c)(b c d)(c d a r))))
(newline)
(define a-pair?
(lambda (x)
(cond
((atom? x) #f)
((null? x) #f)
((null? (cdr x)) #f)
((null? (cdr (cdr x))) #t)
(else #f))))
(newline)
(display (a-pair? '(1 2)))
(newline)
(display (a-pair? '(1 2 3)))
(newline)
;Auxiliary one-liners to build pairs
(define first car)
(define second
(lambda (p)
(car (cdr p))))
(define build
(lambda (s1 s2)
(cons s1 (cons s2 '()))))
(define third
(lambda (p)
(car (cdr (cdr p)))))
(define fun?
(lambda (rel)
(set? (firsts rel))))
(define revpair
(lambda (pair)
(build (second pair) (first pair))))
;Back to the main functions of the chapter
(define revrel
(lambda (rel)
(cond
((null? rel) '())
(else (cons (revpair (car rel))
(revrel (cdr rel)))))))
(newline)
(display (revrel '((a 1) (b 2) (c 3))))
(newline)
(define one-to-one?
(lambda (fun)
(and (fun? fun)(fun? (revrel fun)))))
(newline)
(display (one-to-one? '((1 2) (3 4))))
(newline)
(display (one-to-one? '((1 2) (3 2))))
(newline)