-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter4.rkt
186 lines (146 loc) · 2.86 KB
/
chapter4.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
#lang r5rs
(define add1
(lambda (n)
(+ n 1)))
(display (add1 67))
(newline)
(define sub1
(lambda (n)
(- n 1)))
(display (sub1 5))
(newline)
(define o+
(lambda (n m)
(cond
((zero? m) n)
(else (add1 (o+ n (sub1 m)))))))
(display (o+ 12 13))
(newline)
(define o-
(lambda (n m)
(cond
((zero? m) n)
(else (sub1 (o- n (sub1 m)))))))
(display (o- 13 12))
(newline)
(define addtup
(lambda (tup)
(cond
((null? tup) 0)
(else
(o+ (car tup) (addtup (cdr tup)))))))
(display (addtup '(1 2 3 4 5)))
(newline)
(define o*
(lambda (n m)
(cond
((zero? m) 0)
(else (o+ n (o* n (sub1 m)))))))
(display (o* 13 12))
(newline)
(define tup+
(lambda (tup1 tup2)
(cond
((null? tup1) tup2)
((null? tup2) tup1)
(else (cons (o+ (car tup1) (car tup2)) (tup+ (cdr tup1) (cdr tup2)))))))
(display (tup+ '(3 7) '(4 6 8 1)))
(newline)
(define o>
(lambda (n m)
(cond
((zero? n) #f)
((zero? m) #t)
(else (o> (sub1 n) (sub1 m))))))
(display (o> 3 3))
(newline)
(define o<
(lambda (n m)
(o> m n)))
(display (o< 3 1))
(newline)
(define o=
(lambda (n m)
(cond
((o> n m) #f)
((o< n m) #f)
(else #t))))
(display (o= 3 3))
(newline)
(define ^
(lambda (n m)
(cond
((zero? m) 1)
(else (o* n (^ n (sub1 m)))))))
(display (^ 3 3))
(newline)
(define o/
(lambda (n m)
(cond
((o< n m) 0)
(else (add1 (o/ (o- n m) m))))))
(display (o/ 15 4))
(newline)
(define length
(lambda (lat)
(cond
((null? lat) 0)
(else (add1 (length (cdr lat)))))))
(display (length '(ham and cheese on rye)))
(newline)
(define pick
(lambda (n lat)
(cond
((zero? (sub1 n)) (car lat))
(else (pick (sub1 n) (cdr lat))))))
(display (pick 4 '(lasagna spaghetti ravioli macaroni meatball)))
(newline)
(define no-nums
(lambda (lat)
(cond
((null? lat) '())
((number? (car lat))
(no-nums (cdr lat)))
(else (cons (car lat)
(no-nums
(cdr lat)))))))
(display (no-nums '(a 1 b 2 c 3)))
(newline)
(define all-nums
(lambda (lat)
(cond
((null? lat) '())
((number? (car lat))
(cons (car lat) (all-nums (cdr lat))))
(else (all-nums (cdr lat))))))
(display (all-nums '(a 1 b 2 c 3)))
(newline)
(define eqan?
(lambda (a1 a2)
(cond
((and (number? a1) (number? a2))
(o= a1 a2))
((or (number? a1) (number? a2)) #f)
(else (eq? a1 a2)))))
(display (eqan? 2 2))
(newline)
(define occur
(lambda (a lat)
(cond
((null? lat) 0)
((eq? (car lat) a) (add1 (occur a (cdr lat))))
(else (occur a (cdr lat))))))
(display (occur 'a '(a b b a)))
(newline)
(define one?
(lambda (n)
(o= n 1)))
(define rempick
(lambda (n lat)
(cond
((one? n) (cdr lat))
(else (cons (car lat)
(rempick (sub1 n)
(cdr lat)))))))
(display (rempick 4 '(a b c d e f g)))
(newline)