-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini-srfi-1.scm
241 lines (203 loc) · 6.95 KB
/
mini-srfi-1.scm
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
;;;; minimal implementation of SRFI-1 primitives
;
;
; Copyright (c) 2015-2021, The CHICKEN Team
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
; disclaimer.
; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
; disclaimer in the documentation and/or other materials provided with the distribution.
; Neither the name of the author nor the names of its contributors may be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
(declare
(unused take span drop partition split-at append-map every any cons* concatenate
first second third fourth alist-cons fifth remove
filter filter-map unzip1 last list-index lset-adjoin/eq? lset-difference/eq?
lset-union/eq? lset-intersection/eq? list-tabulate lset<=/eq? lset=/eq? length+
find find-tail iota make-list posq posv)
(hide take span drop partition split-at append-map every any cons* concatenate delete
first second third fourth alist-cons delete-duplicates fifth remove
filter filter-map unzip1 last list-index lset-adjoin/eq? lset-difference/eq?
lset-union/eq? lset-intersection/eq? list-tabulate lset<=/eq? lset=/eq? length+
find find-tail iota make-list posq posv))
(define (partition pred lst)
(let loop ((yes '()) (no '()) (lst lst))
(cond ((null? lst) (values (reverse yes) (reverse no)))
((pred (car lst)) (loop (cons (car lst) yes) no (cdr lst)))
(else (loop yes (cons (car lst) no) (cdr lst))))))
(define (span pred lst)
(let loop ((lst lst) (head '()))
(cond ((null? lst) (values (reverse head) lst))
((pred (car lst)) (loop (cdr lst) (cons (car lst) head)))
(else (values (reverse head) lst)))))
(define (take lst n)
(if (fx<= n 0)
'()
(cons (car lst) (take (cdr lst) (fx- n 1)))))
(define (drop lst n)
(let loop ((lst lst) (n n))
(if (fx<= n 0)
lst
(loop (cdr lst) (fx- n 1)))))
(define (split-at lst n)
(let loop ((n n) (prev '()) (node lst))
(if (fx<= n 0)
(values (reverse prev) node)
(loop (fx- n 1) (cons (car node) prev) (cdr node)))))
(define (append-map proc lst1 . lsts)
(if (null? lsts)
(foldr (lambda (x r) (append (proc x) r)) '() lst1)
(let loop ((lsts (cons lst1 lsts)))
(if (any null? lsts)
'()
(append (apply proc (map (lambda (x) (car x)) lsts))
(loop (map (lambda (x) (cdr x)) lsts)))))))
(define (every pred lst)
(let loop ((lst lst))
(cond ((null? lst))
((not (pred (car lst))) #f)
(else (loop (cdr lst))))))
(define (any pred lst)
(let loop ((lst lst))
(cond ((null? lst) #f)
((pred (car lst)))
(else (loop (cdr lst))))))
(define (cons* x1 . xs)
(let loop ((x x1) (rest xs))
(if (null? rest)
x
(cons x (loop (car rest) (cdr rest))))))
(define (concatenate lst)
(let loop ((lst lst))
(if (null? lst)
'()
(append (car lst) (loop (cdr lst))))))
(define (delete x lst test)
(let loop ((lst lst))
(cond ((null? lst) lst)
((test x (car lst))
(loop (cdr lst)))
(else
(cons (car lst) (loop (cdr lst)))))))
(define (first x) (car x))
(define (second x) (cadr x))
(define (third x) (caddr x))
(define (fourth x) (cadddr x))
(define (fifth x) (car (cddddr x)))
(define (delete-duplicates lst test)
(let loop ((lst lst))
(if (null? lst)
lst
(let* ((x (car lst))
(tail (cdr lst))
(new-tail (loop (delete x tail test))))
(if (equal? tail new-tail)
lst
(cons x new-tail))))))
(define (alist-cons x y z) (cons (cons x y) z))
(define (filter pred lst)
(foldr (lambda (x r) (if (pred x) (cons x r) r)) '() lst))
(define (filter-map pred lst)
(foldr (lambda (x r)
(cond ((pred x) => (lambda (y) (cons y r)))
(else r)))
'()
lst))
(define (remove pred lst)
(filter (lambda (x) (not (pred x))) lst))
(define (unzip1 lst) (map (lambda (x) (car x)) lst))
(define (last lst)
(let loop ((lst lst))
(if (null? (cdr lst))
(car lst)
(loop (cdr lst)))))
(define (list-index pred lst)
(let loop ((i 0) (lst lst))
(cond ((null? lst) #f)
((pred (car lst)) i)
(else (loop (fx+ i 1) (cdr lst))))))
(define (lset-adjoin/eq? lst . vals)
(let loop ((vals vals) (lst lst))
(cond ((null? vals) lst)
((memq (car vals) lst) (loop (cdr vals) lst))
(else (loop (cdr vals) (cons (car vals) lst))))))
(define (lset-difference/eq? ls . lss)
(foldl
(lambda (ls lst)
(filter (lambda (x) (not (memq x lst))) ls))
ls
lss))
(define (lset-union/eq? ls . lss)
(foldl
(lambda (ls lst)
(foldl
(lambda (ls x)
(if (any (lambda (y) (eq? y x)) ls)
ls
(cons x ls)))
ls lst))
ls lss))
(define (lset-intersection/eq? ls1 . lss)
(filter (lambda (x)
(every (lambda (lis) (memq x lis)) lss))
ls1))
(define (list-tabulate n proc)
(let loop ((i 0))
(if (fx>= i n)
'()
(cons (proc i) (loop (fx+ i 1))))))
(define (lset<=/eq? s1 s2)
(every (lambda (s) (memq s s2)) s1))
(define (lset=/eq? s1 s2)
(and (eq? (length s1) (length s2))
(every (lambda (s) (memq s s2)) s1)))
;; from SRFI-1 ref. impl.
(define (length+ x) ; Returns #f if X is circular.
(let lp ((x x) (lag x) (len 0))
(if (pair? x)
(let ((x (cdr x))
(len (fx+ len 1)))
(if (pair? x)
(let ((x (cdr x))
(lag (cdr lag))
(len (fx+ len 1)))
(and (not (eq? x lag)) (lp x lag len)))
len))
len)))
(define (find pred lst)
(let loop ((lst lst))
(cond ((null? lst) #f)
((pred (car lst)) (car lst))
(else (loop (cdr lst))))))
(define (find-tail pred ls)
(let lp ((ls ls))
(cond ((null? ls) #f)
((pred (car ls)) ls)
(else (lp (cdr ls))))))
(define (iota n) (list-tabulate n (lambda (i) i)))
(define (make-list n #!optional x)
(list-tabulate n (lambda _ x)))
(define (posq x lst)
(let loop ((i 0) (lst lst))
(cond ((null? lst) #f)
((eq? (car lst) x) i)
(else (loop (fx+ i 1) (cdr lst))))))
(define (posv x lst)
(let loop ((i 0) (lst lst))
(cond ((null? lst) #f)
((eqv? (car lst) x) i)
(else (loop (fx+ i 1) (cdr lst))))))