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
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
9 ;1. Any copy made of this software must include this copyright notice
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.
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
21 ;;; Some of these functions may be already defined in your Scheme.
22 ;;; Comment out those definitions for functions which are already defined.
24 ;;;; LIST FUNCTIONS FROM COMMON LISP
26 ;;; Some tail-recursive optimizations made by
27 ;;; Colin Walters <walters@cis.ohio-state.edu>
29 ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
30 (define (comlist:make-list k . init)
31 (set! init (if (pair? init) (car init)))
33 (result '() (cons init result)))
36 (define (comlist:copy-list lst) (append lst '()))
38 (define (comlist:adjoin e l) (if (memv e l) l (cons e l)))
40 (define (comlist:union l1 l2)
43 (else (comlist:union (cdr l1) (comlist:adjoin (car l1) l2)))))
45 (define (comlist:intersection l1 l2)
49 (let build-intersection ((l1 l1)
53 ((memv (car l1) l2) (build-intersection (cdr l1) (cons (car l1) result)))
54 (else (build-intersection (cdr l1) result))))))
56 (define (comlist:set-difference l1 l2)
60 (let build-difference ((l1 l1)
64 ((memv (car l1) l2) (build-difference (cdr l1) result))
65 (else (build-difference (cdr l1) (cons (car l1) result)))))))
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)))))))
74 (define (comlist:reduce-init p init l)
77 (comlist:reduce-init p (p init (car l)) (cdr l))))
79 (define (comlist:reduce p l)
81 ((null? (cdr l)) (car l))
82 (else (comlist:reduce-init p (car l) (cdr l)))))
84 (define (comlist:some pred l . rest)
88 (or (pred (car l)) (mapf (cdr l))))))
89 (else (let mapf ((l l) (rest rest))
91 (or (apply pred (car l) (map car rest))
92 (mapf (cdr l) (map cdr rest))))))))
94 (define (comlist:every pred l . rest)
98 (and (pred (car l)) (mapf (cdr l))))))
99 (else (let mapf ((l l) (rest rest))
101 (and (apply pred (car l) (map car rest))
102 (mapf (cdr l) (map cdr rest))))))))
104 (define (comlist:notany pred . ls) (not (apply comlist:some pred ls)))
106 (define (comlist:notevery pred . ls) (not (apply comlist:every pred ls)))
108 (define (comlist:list-of?? predicate . bound)
109 (define (errout) (apply slib:error 'list-of?? predicate bound))
114 (every predicate obj))))
116 (set! bound (car bound))
117 (cond ((negative? bound)
118 (set! bound (- bound))
121 (<= bound (length obj))
122 (every predicate obj))))
126 (<= (length obj) bound)
127 (every predicate obj))))))
129 (let ((low (car bound))
131 (cond ((or (negative? low) (negative? high)) (errout))
133 (set! high (car bound))
134 (set! low (cadr bound))))
137 (<= low (length obj) high)
138 (every predicate obj)))))
141 (define (comlist:find-if t l)
143 ((t (car l)) (car l))
144 (else (comlist:find-if t (cdr l)))))
146 (define (comlist:member-if t l)
149 (else (comlist:member-if t (cdr l)))))
151 (define (comlist:remove p l)
154 (cond ((null? l) result)
155 ((eqv? p (car l)) (remove (cdr l) result))
156 (else (remove (cdr l) (cons (car l) result))))))
158 (define (comlist:remove-if p l)
159 (let remove-if ((l l)
161 (cond ((null? l) result)
162 ((p (car l)) (remove-if (cdr l) result))
163 (else (remove-if (cdr l) (cons (car l) result))))))
165 (define (comlist:remove-if-not p l)
166 (let remove-if-not ((l l)
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)))))
172 (define comlist:nconc
173 (if (provided? 'rev2-procedures) append!
175 (cond ((null? args) '())
176 ((null? (cdr args)) (car args))
177 ((null? (car args)) (apply comlist:nconc (cdr args)))
179 (set-cdr! (last-pair (car args))
180 (apply comlist:nconc (cdr args)))
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)))))
194 (define (comlist:last lst n)
195 (comlist:nthcdr (- (length lst) n) lst))
197 (define (comlist:butlast lst n)
198 (letrec ((l (- (length lst) n))
200 (let build-until-zero ((lst lst)
203 (cond ((null? lst) (reverse result))
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)
211 (define (comlist:nthcdr n lst)
212 (if (zero? n) lst (comlist:nthcdr (+ -1 n) (cdr lst))))
214 (define (comlist:butnthcdr n lst)
215 (letrec ((bl (lambda (lst n)
216 (let build-until-zero ((lst lst)
219 (cond ((null? lst) (reverse result))
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)
229 (define (comlist:and? . args)
230 (cond ((null? args) #t)
231 ((car args) (apply comlist:and? (cdr args)))
234 (define (comlist:or? . args)
235 (cond ((null? args) #f)
237 (else (apply comlist:or? (cdr args)))))
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)))))
245 ;;; remove duplicates of MEMBERs of a list
246 (define (comlist:remove-duplicates lst)
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)))))))
254 (define (comlist:list* x . y)
258 (cons (car x) (list*1 (cdr x)))))
261 (cons x (list*1 y))))
263 (define (comlist:atom? a)
266 (define (comlist:delete obj list)
267 (let delete ((list list))
268 (cond ((null? list) '())
269 ((equal? obj (car list)) (delete (cdr list)))
271 (set-cdr! list (delete (cdr list)))
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)))
279 (set-cdr! list (delete-if (cdr list)))
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)))
287 (set-cdr! list (delete-if (cdr list)))
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)
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??)