add env script
[bpt/guile.git] / module / slib / comlist.scm
1 ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
2 ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
3 ; Copyright (C) 2000 Colin Walters
4 ;
5 ;Permission to copy this software, to redistribute it, and to use it
6 ;for any purpose is granted, subject to the following restrictions and
7 ;understandings.
8 ;
9 ;1. Any copy made of this software must include this copyright notice
10 ;in full.
11 ;
12 ;2. I have made no warrantee or representation that the operation of
13 ;this software will be error-free, and I am under no obligation to
14 ;provide any services, by way of maintenance, update, or otherwise.
15 ;
16 ;3. In conjunction with products arising from the use of this
17 ;material, there shall be no use of my name in any advertising,
18 ;promotional, or sales literature without prior written consent in
19 ;each case.
20
21 ;;; Some of these functions may be already defined in your Scheme.
22 ;;; Comment out those definitions for functions which are already defined.
23
24 ;;;; LIST FUNCTIONS FROM COMMON LISP
25
26 ;;; Some tail-recursive optimizations made by
27 ;;; Colin Walters <walters@cis.ohio-state.edu>
28
29 ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
30 (define (comlist:make-list k . init)
31 (set! init (if (pair? init) (car init)))
32 (do ((k k (+ -1 k))
33 (result '() (cons init result)))
34 ((<= k 0) result)))
35
36 (define (comlist:copy-list lst) (append lst '()))
37
38 (define (comlist:adjoin e l) (if (memv e l) l (cons e l)))
39
40 (define (comlist:union l1 l2)
41 (cond ((null? l1) l2)
42 ((null? l2) l1)
43 (else (comlist:union (cdr l1) (comlist:adjoin (car l1) l2)))))
44
45 (define (comlist:intersection l1 l2)
46 ;; optimization
47 (if (null? l2)
48 l2
49 (let build-intersection ((l1 l1)
50 (result '()))
51 (cond ((null? l1)
52 result)
53 ((memv (car l1) l2) (build-intersection (cdr l1) (cons (car l1) result)))
54 (else (build-intersection (cdr l1) result))))))
55
56 (define (comlist:set-difference l1 l2)
57 ;; optimization
58 (if (null? l2)
59 l1
60 (let build-difference ((l1 l1)
61 (result '()))
62 (cond ((null? l1)
63 result)
64 ((memv (car l1) l2) (build-difference (cdr l1) result))
65 (else (build-difference (cdr l1) (cons (car l1) result)))))))
66
67 (define (comlist:position obj lst)
68 (letrec ((pos (lambda (n lst)
69 (cond ((null? lst) #f)
70 ((eqv? obj (car lst)) n)
71 (else (pos (+ 1 n) (cdr lst)))))))
72 (pos 0 lst)))
73
74 (define (comlist:reduce-init p init l)
75 (if (null? l)
76 init
77 (comlist:reduce-init p (p init (car l)) (cdr l))))
78
79 (define (comlist:reduce p l)
80 (cond ((null? l) l)
81 ((null? (cdr l)) (car l))
82 (else (comlist:reduce-init p (car l) (cdr l)))))
83
84 (define (comlist:some pred l . rest)
85 (cond ((null? rest)
86 (let mapf ((l l))
87 (and (not (null? l))
88 (or (pred (car l)) (mapf (cdr l))))))
89 (else (let mapf ((l l) (rest rest))
90 (and (not (null? l))
91 (or (apply pred (car l) (map car rest))
92 (mapf (cdr l) (map cdr rest))))))))
93
94 (define (comlist:every pred l . rest)
95 (cond ((null? rest)
96 (let mapf ((l l))
97 (or (null? l)
98 (and (pred (car l)) (mapf (cdr l))))))
99 (else (let mapf ((l l) (rest rest))
100 (or (null? l)
101 (and (apply pred (car l) (map car rest))
102 (mapf (cdr l) (map cdr rest))))))))
103
104 (define (comlist:notany pred . ls) (not (apply comlist:some pred ls)))
105
106 (define (comlist:notevery pred . ls) (not (apply comlist:every pred ls)))
107
108 (define (comlist:list-of?? predicate . bound)
109 (define (errout) (apply slib:error 'list-of?? predicate bound))
110 (case (length bound)
111 ((0)
112 (lambda (obj)
113 (and (list? obj)
114 (every predicate obj))))
115 ((1)
116 (set! bound (car bound))
117 (cond ((negative? bound)
118 (set! bound (- bound))
119 (lambda (obj)
120 (and (list? obj)
121 (<= bound (length obj))
122 (every predicate obj))))
123 (else
124 (lambda (obj)
125 (and (list? obj)
126 (<= (length obj) bound)
127 (every predicate obj))))))
128 ((2)
129 (let ((low (car bound))
130 (high (cadr bound)))
131 (cond ((or (negative? low) (negative? high)) (errout))
132 ((< high low)
133 (set! high (car bound))
134 (set! low (cadr bound))))
135 (lambda (obj)
136 (and (list? obj)
137 (<= low (length obj) high)
138 (every predicate obj)))))
139 (else (errout))))
140
141 (define (comlist:find-if t l)
142 (cond ((null? l) #f)
143 ((t (car l)) (car l))
144 (else (comlist:find-if t (cdr l)))))
145
146 (define (comlist:member-if t l)
147 (cond ((null? l) #f)
148 ((t (car l)) l)
149 (else (comlist:member-if t (cdr l)))))
150
151 (define (comlist:remove p l)
152 (let remove ((l l)
153 (result '()))
154 (cond ((null? l) result)
155 ((eqv? p (car l)) (remove (cdr l) result))
156 (else (remove (cdr l) (cons (car l) result))))))
157
158 (define (comlist:remove-if p l)
159 (let remove-if ((l l)
160 (result '()))
161 (cond ((null? l) result)
162 ((p (car l)) (remove-if (cdr l) result))
163 (else (remove-if (cdr l) (cons (car l) result))))))
164
165 (define (comlist:remove-if-not p l)
166 (let remove-if-not ((l l)
167 (result '()))
168 (cond ((null? l) result)
169 ((p (car l)) (remove-if-not (cdr l) (cons (car l) result)))
170 (else (remove-if-not (cdr l) result)))))
171
172 (define comlist:nconc
173 (if (provided? 'rev2-procedures) append!
174 (lambda args
175 (cond ((null? args) '())
176 ((null? (cdr args)) (car args))
177 ((null? (car args)) (apply comlist:nconc (cdr args)))
178 (else
179 (set-cdr! (last-pair (car args))
180 (apply comlist:nconc (cdr args)))
181 (car args))))))
182
183 ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
184 (define (comlist:nreverse rev-it)
185 ;;; Reverse order of elements of LIST by mutating cdrs.
186 (cond ((null? rev-it) rev-it)
187 ((not (list? rev-it))
188 (slib:error "nreverse: Not a list in arg1" rev-it))
189 (else (do ((reved '() rev-it)
190 (rev-cdr (cdr rev-it) (cdr rev-cdr))
191 (rev-it rev-it rev-cdr))
192 ((begin (set-cdr! rev-it reved) (null? rev-cdr)) rev-it)))))
193
194 (define (comlist:last lst n)
195 (comlist:nthcdr (- (length lst) n) lst))
196
197 (define (comlist:butlast lst n)
198 (letrec ((l (- (length lst) n))
199 (bl (lambda (lst n)
200 (let build-until-zero ((lst lst)
201 (n n)
202 (result '()))
203 (cond ((null? lst) (reverse result))
204 ((positive? n)
205 (build-until-zero (cdr lst) (- n 1) (cons (car lst) result)))
206 (else (reverse result)))))))
207 (bl lst (if (negative? n)
208 (slib:error "negative argument to butlast" n)
209 l))))
210
211 (define (comlist:nthcdr n lst)
212 (if (zero? n) lst (comlist:nthcdr (+ -1 n) (cdr lst))))
213
214 (define (comlist:butnthcdr n lst)
215 (letrec ((bl (lambda (lst n)
216 (let build-until-zero ((lst lst)
217 (n n)
218 (result '()))
219 (cond ((null? lst) (reverse result))
220 ((positive? n)
221 (build-until-zero (cdr lst) (- n 1) (cons (car lst) result)))
222 (else (reverse result)))))))
223 (bl lst (if (negative? n)
224 (slib:error "negative argument to butnthcdr" n)
225 n))))
226
227 ;;;; CONDITIONALS
228
229 (define (comlist:and? . args)
230 (cond ((null? args) #t)
231 ((car args) (apply comlist:and? (cdr args)))
232 (else #f)))
233
234 (define (comlist:or? . args)
235 (cond ((null? args) #f)
236 ((car args) #t)
237 (else (apply comlist:or? (cdr args)))))
238
239 ;;; Checks to see if a list has any duplicate MEMBERs.
240 (define (comlist:has-duplicates? lst)
241 (cond ((null? lst) #f)
242 ((member (car lst) (cdr lst)) #t)
243 (else (comlist:has-duplicates? (cdr lst)))))
244
245 ;;; remove duplicates of MEMBERs of a list
246 (define (comlist:remove-duplicates lst)
247 (letrec ((rem-dup
248 (lambda (lst nlst)
249 (cond ((null? lst) nlst)
250 ((member (car lst) nlst) (rem-dup (cdr lst) nlst))
251 (else (rem-dup (cdr lst) (cons (car lst) nlst)))))))
252 (rem-dup lst '())))
253
254 (define (comlist:list* x . y)
255 (define (list*1 x)
256 (if (null? (cdr x))
257 (car x)
258 (cons (car x) (list*1 (cdr x)))))
259 (if (null? y)
260 x
261 (cons x (list*1 y))))
262
263 (define (comlist:atom? a)
264 (not (pair? a)))
265
266 (define (comlist:delete obj list)
267 (let delete ((list list))
268 (cond ((null? list) '())
269 ((equal? obj (car list)) (delete (cdr list)))
270 (else
271 (set-cdr! list (delete (cdr list)))
272 list))))
273
274 (define (comlist:delete-if pred list)
275 (let delete-if ((list list))
276 (cond ((null? list) '())
277 ((pred (car list)) (delete-if (cdr list)))
278 (else
279 (set-cdr! list (delete-if (cdr list)))
280 list))))
281
282 (define (comlist:delete-if-not pred list)
283 (let delete-if ((list list))
284 (cond ((null? list) '())
285 ((not (pred (car list))) (delete-if (cdr list)))
286 (else
287 (set-cdr! list (delete-if (cdr list)))
288 list))))
289
290 ;;; exports
291
292 (define make-list comlist:make-list)
293 (define copy-list comlist:copy-list)
294 (define adjoin comlist:adjoin)
295 (define union comlist:union)
296 (define intersection comlist:intersection)
297 (define set-difference comlist:set-difference)
298 (define position comlist:position)
299 (define reduce-init comlist:reduce-init)
300 (define reduce comlist:reduce) ; reduce is also in collect.scm
301 (define some comlist:some)
302 (define every comlist:every)
303 (define notevery comlist:notevery)
304 (define notany comlist:notany)
305 (define find-if comlist:find-if)
306 (define member-if comlist:member-if)
307 (define remove comlist:remove)
308 (define remove-if comlist:remove-if)
309 (define remove-if-not comlist:remove-if-not)
310 (define nconc comlist:nconc)
311 (define nreverse comlist:nreverse)
312 (define last comlist:last)
313 (define butlast comlist:butlast)
314 (define nthcdr comlist:nthcdr)
315 (define butnthcdr comlist:butnthcdr)
316 (define and? comlist:and?)
317 (define or? comlist:or?)
318 (define has-duplicates? comlist:has-duplicates?)
319 (define remove-duplicates comlist:remove-duplicates)
320
321 (define delete-if-not comlist:delete-if-not)
322 (define delete-if comlist:delete-if)
323 (define delete comlist:delete)
324 (define comlist:atom comlist:atom?)
325 (define atom comlist:atom?)
326 (define atom? comlist:atom?)
327 (define list* comlist:list*)
328 (define list-of?? comlist:list-of??)