-
Notifications
You must be signed in to change notification settings - Fork 1
/
r4rsprim.rkt
316 lines (311 loc) · 11.2 KB
/
r4rsprim.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
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
#lang racket
(require (only-in "macros.rkt" special))
(provide r4rs-prims primitives-to-rename)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Non-primitive primitives.
;; At present these cannot refer to each other.
(define r4rs-prims
`((define caar (lambda (x) (car (car x))))
(define cadr (lambda (x) (car (cdr x))))
(define cdar (lambda (x) (cdr (car x))))
(define cddr (lambda (x) (cdr (cdr x))))
(define caaar (lambda (x) (car (car (car x)))))
(define caadr (lambda (x) (car (car (cdr x)))))
(define cadar (lambda (x) (car (cdr (car x)))))
(define caddr (lambda (x) (car (cdr (cdr x)))))
(define cdaar (lambda (x) (cdr (car (car x)))))
(define cdadr (lambda (x) (cdr (car (cdr x)))))
(define cddar (lambda (x) (cdr (cdr (car x)))))
(define cdddr (lambda (x) (cdr (cdr (cdr x)))))
(define caaaar (lambda (x) (car (car (car (car x))))))
(define caaadr (lambda (x) (car (car (car (cdr x))))))
(define caadar (lambda (x) (car (car (cdr (car x))))))
(define caaddr (lambda (x) (car (car (cdr (cdr x))))))
(define cadaar (lambda (x) (car (cdr (car (car x))))))
(define cadadr (lambda (x) (car (cdr (car (cdr x))))))
(define caddar (lambda (x) (car (cdr (cdr (car x))))))
(define cadddr (lambda (x) (car (cdr (cdr (cdr x))))))
(define cdaaar (lambda (x) (cdr (car (car (car x))))))
(define cdaadr (lambda (x) (cdr (car (car (cdr x))))))
(define cdadar (lambda (x) (cdr (car (cdr (car x))))))
(define cdaddr (lambda (x) (cdr (car (cdr (cdr x))))))
(define cddaar (lambda (x) (cdr (cdr (car (car x))))))
(define cddadr (lambda (x) (cdr (cdr (car (cdr x))))))
(define cdddar (lambda (x) (cdr (cdr (cdr (car x))))))
(define cddddr (lambda (x) (cdr (cdr (cdr (cdr x))))))
(define list (lambda a a))
(define length
(lambda (a)
(recur loop ((a a) (len 0))
(if (null? a)
len
(loop (cdr a) (+ 1 len))))))
(define append
(lambda a
(letrec ((app2 (lambda (a b)
(if (null? a)
b
(cons (car a) (app2 (cdr a) b))))))
(recur loop ((a a))
(cond ((null? a) '())
((null? (cdr a)) (car a))
(else (app2 (car a) (loop (cdr a)))))))))
(define reverse
(lambda (a)
(recur loop ((a a) (acc '()))
(if (null? a)
acc
(loop (cdr a) (cons (car a) acc))))))
(define list-tail
(lambda (a n)
(if (zero? n)
a
(list-tail (cdr a) (- n 1)))))
(define list-ref
(lambda (a n)
(if (zero? n)
(car a)
(list-ref (cdr a) (- n 1)))))
(define memq
(lambda (x a)
(cond ((null? a) #f)
((eq? x (car a)) a)
(else (memq x (cdr a))))))
(define memv
(lambda (x a)
(cond ((null? a) #f)
((eqv? x (car a)) a)
(else (memv x (cdr a))))))
(define member
(lambda (x a)
(cond ((null? a) #f)
((equal? x (car a)) a)
(else (member x (cdr a))))))
(define assq
(lambda (x a)
(cond ((null? a) #f)
((eq? x (car (car a))) (car a))
(else (assq x (cdr a))))))
(define assv
(lambda (x a)
(cond ((null? a) #f)
((eqv? x (car (car a))) (car a))
(else (assv x (cdr a))))))
(define assoc
(lambda (x a)
(cond ((null? a) #f)
((equal? x (car (car a))) (car a))
(else (assoc x (cdr a))))))
(define string->list
(lambda (s)
(recur loop ((n (- (string-length s) 1)) (acc '()))
(if (negative? n)
acc
(loop (- n 1) (cons (string-ref s n) acc))))))
; (define list->string
; (lambda (a)
; (apply string a)))
(define list->string
(lambda (a)
(define length
(lambda (a)
(recur loop ((a a) (len 0))
(if (null? a)
len
(loop (cdr a) (+ 1 len))))))
(let ((s (make-string (length a))))
(recur loop ((i 0) (a a))
(if (null? a)
s
(begin
(string-set! s i (car a))
(loop (+ 1 i) (cdr a))))))))
(define vector->list
(lambda (v)
(recur loop ((n (- (vector-length v) 1)) (acc '()))
(if (negative? n)
acc
(loop (- n 1) (cons (vector-ref v n) acc))))))
; (define list->vector
; (lambda (a)
; (apply vector a)))
(define list->vector
(lambda (a)
(define length
(lambda (a)
(recur loop ((a a) (len 0))
(if (null? a)
len
(loop (cdr a) (+ 1 len))))))
(if (null? a)
(vector)
(let ((v (make-vector (length a) (car a))))
(recur loop ((i 1) (a (cdr a)))
(if (null? a)
v
(begin
(vector-set! v i (car a))
(loop (+ 1 i) (cdr a)))))))))
(define map
(lambda (f a . args)
(letrec ((map1 (lambda (f l)
(if (null? l)
'()
(cons (f (car l))
(map1 f (cdr l))))))
(map2 (lambda (f l1 l2)
(cond ((null? l1)
'())
((null? l2)
(error 'map "lists differ in length"))
(else
(cons (f (car l1) (car l2))
(map2 f (cdr l1) (cdr l2)))))))
(map* (lambda (f l*)
(if (null? (car l*))
'()
(cons (let ((l (map1 car l*)))
(if (null? l)
(f)
((,special internal-apply) f l)))
(map* f (map1 cdr l*)))))))
(cond ((null? args)
(map1 f a))
((null? (cdr args))
(map2 f a (car args)))
(else
(map* f (cons a args)))))))
(define for-each
(lambda (f a . args)
(letrec ((map (lambda (f l)
(if (null? l)
'()
(cons (f (car l))
(map f (cdr l)))))))
(letrec ((for-each1 (lambda (f l)
(if (null? l)
(void)
(begin
(f (car l))
(for-each1 f (cdr l))))))
(for-each2 (lambda (f l1 l2)
(cond ((null? l1)
(void))
((null? l2)
(error 'for-each "lists differ in length"))
(else
(f (car l1) (car l2))
(for-each2 f (cdr l1) (cdr l2))))))
(for-each* (lambda (f l*)
(if (null? (car l*))
(void)
(begin
(let ((l (map car l*)))
(if (null? l)
(f)
((,special internal-apply) f l)))
(for-each* f (map cdr l*)))))))
(cond ((null? args)
(for-each1 f a))
((null? (cdr args))
(for-each2 f a (car args)))
(else
(for-each* f (cons a args))))))))
(define call-with-current-continuation
(lambda (f)
(letcc x (f x))))
(define call-with-input-file
(lambda (s f)
(let* ((p (open-input-file s))
(v (f p)))
(close-input-port p)
v)))
(define call-with-output-file
(lambda (s f)
(let* ((p (open-output-file s))
(v (f p)))
(close-output-port p)
v)))
(define with-input-from-file
(lambda (s f)
; no way to switch current input in R4RS Scheme
(error 'with-input-from-file "not supported")
(f)))
(define with-output-to-file
(lambda (s f)
; no way to switch current output in R4RS Scheme
(error 'with-output-to-file "not supported")
(f)))
(define apply
(lambda (f arg0 . args)
(define list-copy
(lambda (l)
(if (null? l)
'()
(cons (car l) (list-copy (cdr l))))))
(define flatten
(lambda (args)
(if (null? (cdr args))
(car args)
(cons (car args) (flatten (cdr args))))))
(let ((m (flatten (cons arg0 args))))
(cond ((null? m) (f))
((null? (cdr m)) (f (car m)))
((null? (cdr (cdr m))) (f (car m) (car (cdr m))))
(else (let ((n (list-copy m)))
(if (null? n)
(error 'apply "can't happen")
((,special internal-apply) f n))))))))
(define make-promise
(lambda (thunk)
(let ([first #t]
[val #f])
(lambda ()
(cond [(eq? first 'forcing) (error 'force "recursive force")]
[first (set! first 'forcing)
(set! val (thunk))
(set! first #f)
val]
[else val])))))
(define force (lambda (promise) (promise)))
(define make-list
(lambda (n val)
(recur loop ((n n))
(if (< n 1)
'()
(cons val (loop (- n 1)))))))
(define andmap
(lambda (f list0 . lists)
(if (null? list0)
(and)
(recur loop ((lists (cons list0 lists)))
(if (null? (cdr (car lists)))
(apply f (map car lists))
(and (apply f (map car lists))
(loop (map cdr lists))))))))
(define ormap
(lambda (f list0 . lists)
(if (null? list0)
(or)
(recur loop ((lists (cons list0 lists)))
(if (null? (cdr (car lists)))
(apply f (map car lists))
(or (apply f (map car lists))
(loop (map cdr lists))))))))
(define call/cc
(lambda (f)
(letcc x (f x))))
(define dynamic-wind
(lambda (in doit out)
(let* ([a (in)]
[b (doit)]
[c (out)])
b)))
(define sort
(lambda (compare in)
in))
))
(define primitives-to-rename
(append
(map cadr r4rs-prims)
'(reverse! append! add1 sub1 vector-copy sort sort! reset pretty-print random)))