-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.rkt
177 lines (139 loc) · 4.71 KB
/
03.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
#lang racket
(require rackunit)
; Atom [List-of Atom] -> [List-of Atom]
(define rember.v1 (lambda (a lat)
(cond
((null? lat) (quote ()))
(else (cond
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember.v1 a
(cdr lat)))))))))
(check-equal? (rember.v1 'and '(bacon lettuce and tomato))
'(bacon lettuce tomato))
; Atom [List-of Atom] -> [List-of Atom]
(define rember.v2 (lambda (a lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember.v2 a (cdr lat)))))))
(check-equal? (rember.v2 'and '(bacon lettuce and tomato))
'(bacon lettuce tomato))
; See if you can write the function firsts
; [List-of [Non-emoty List-of S-expr] -> [List-of S-expr]
; (define (firsts los) '()) ;stub
; Scheme version for the reference
#;(define firsts
(lambda (los)
(cond ((null? los) '())
(else
(cons (car (car los))
(firsts (cdr los)))))))
; Racket version
(define firsts
(lambda (los)
(cond [(empty? los) '()]
[else
(cons (first (first los))
(firsts (rest los)))])))
(provide firsts)
(check-equal? (firsts '()) '())
(check-equal? (firsts '((a b) (c d) (e f)))
'(a c e))
; Atom Atom [List-of Atom] -> [List-of Atom]
; build a lat with new inserted to the right of the first occurrence of old
; (define (insertR new old lat) '()) ;stub
(define insertR
(lambda (new old lat)
(cond
((null? lat) '())
(else
(cond
((eq? (car lat) old)
(cons old
(cons new (cdr lat))))
(else
(cons (car lat)
(insertR new old (cdr lat)))))))))
(check-equal? (insertR 'e 'd '(a b c d f g d h))
'(a b c d e f g d h))
; Atom Atom [List-of Atom] -> [List-of Atom]
; replace the first occurrence of old in the lat with new
; (define (subst new old lat) '()) ;stub
(define subst
(lambda (new old lat)
(cond
((null? lat) '())
(else
(cond
((eq? (car lat) old)
(cons new (cdr lat)))
(else
(cons (car lat)
(subst new old (cdr lat)))))))))
(check-equal? (subst 'e 'd '(a b c d f g d h))
'(a b c e f g d h))
; Atom Atom Atom [List-of Atom] -> [List-of Atom]
; replaces either the first occurrence of o1 or
; the first occurrence of o2 by new
; (define (subst new old lat) '()) ;stub
(define subst2
(lambda (new o1 o2 lat)
(cond
((null? lat) '())
(else
(cond
((or
(eq? (car lat) o1)
(eq? (car lat) o2))
(cons new (cdr lat)))
(else
(cons (car lat)
(subst2 new o1 o2 (cdr lat)))))))))
(check-equal? (subst2 'vanilla 'chocolate 'banana
'(banana ice cream with chocolate topping))
'(vanilla ice cream with chocolate topping))
(provide multirember)
; Atom [List-of Atom] -> [List-of Atom]
; return lat with all occurrences of a removed.
; (define (multirember a lat) '()) ;stub
(define multirember
(lambda (a lat)
(cond ((null? lat) '())
((eq? (car lat) a) (multirember a (cdr lat)))
(else
(cons (car lat)
(multirember a (cdr lat)))))))
(check-equal? (multirember 'cup
'(coffee cup tea cup and hick cup))
'(coffee tea and hick))
; Atom Atom [List-of Atom] -> [List-of Atom]
; build a lat with new inserted to the right of the all occurrences of old
;(define (multiinsertR new old lat) '())
(define multiinsertR
(lambda (new old lat)
(cond
((null? lat) '())
((eq? (car lat) old)
(cons old
(cons new
(multiinsertR new old (cdr lat)))))
(else
(cons (car lat)
(multiinsertR new old (cdr lat)))))))
(check-equal? (multiinsertR 'a 1 '(1 2 3 1 2 1))
'(1 a 2 3 1 a 2 1 a))
; Atom Atom [List-of Atom] -> [List-of Atom]
; replace the all occurrences of old in the lat with new
; (define (multisubst new old lat) '()) ;stub
(define multisubst
(lambda (new old lat)
(cond ((null? lat) '())
((eq? (car lat) old)
(cons new (multisubst new old (cdr lat))))
(else
(cons (car lat)
(multisubst new old (cdr lat)))))))
(check-equal? (multisubst 'a 1 '(1 2 3 1 2 1 2))
'(a 2 3 a 2 a 2))