PageRenderTime 62ms CodeModel.GetById 17ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/comlist.scm

http://github.com/digego/extempore
Lisp | 316 lines | 238 code | 14 blank | 64 comment | 0 complexity | 2cf3f4a5d11a372037f5103b8e89d484 MD5 | raw file
  1;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
  2; Copyright (C) 1991, 1993, 1995, 2001, 2003 Aubrey Jaffer.
  3; Copyright (C) 2000 Colin Walters
  4;
  5;Permission to copy this software, to modify it, to redistribute it,
  6;to distribute modified versions, and to use it for any purpose is
  7;granted, subject to the following restrictions and 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 warranty 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
 27(define (cl:assoc-adjoin pair lst) 
 28   (if (assoc (car pair) lst) 
 29       lst 
 30       (cons pair lst)))
 31
 32;; with precedence to first lst
 33(define cl:assoc-union
 34  (letrec ((onion (lambda (lst1 lst2)
 35                     (if (null? lst1)
 36                         lst2
 37                         (onion (cdr lst1) (cl:assoc-adjoin (car lst1) lst2))))))
 38     (lambda (lst1 lst2)
 39        (cond ((null? lst1) lst2)
 40              ((null? lst2) lst1)
 41              (else (onion (reverse lst2) lst1))))))
 42
 43
 44;;; Some tail-recursive optimizations made by
 45;;; Colin Walters <walters@cis.ohio-state.edu>
 46;;; AGJ restored order July 2001.
 47
 48;;;@ From: hugh@ear.mit.edu (Hugh Secker-Walker)
 49(define (cl:make-list k . init)
 50  (set! init (if (pair? init) (car init)))
 51  (do ((k (+ -1 k) (+ -1 k))
 52       (result '() (cons init result)))
 53      ((negative? k) result)))
 54;@
 55(define (cl:copy-list lst) (append lst '()))
 56;@
 57(define (cl:adjoin obj lst) (if (member obj lst) lst (cons obj lst)))
 58;@
 59(define cl:union
 60  (letrec ((onion
 61	    (lambda (lst1 lst2)
 62	      (if (null? lst1)
 63		  lst2
 64		  (onion (cdr lst1) (cl:adjoin (car lst1) lst2))))))
 65    (lambda (lst1 lst2)
 66      (cond ((null? lst1) lst2)
 67	    ((null? lst2) lst1)
 68	    ((null? (cdr lst1)) (cl:adjoin (car lst1) lst2))
 69	    ((null? (cdr lst2)) (cl:adjoin (car lst2) lst1))
 70	    ((< (length lst2) (length lst1)) (onion (reverse lst2) lst1))
 71	    (else (onion (reverse lst1) lst2))))))
 72;@
 73(define (cl:intersection lst1 lst2)
 74   (if (null? lst2)
 75       lst2
 76       (let build-intersection ((lst1 lst1)
 77                                (result '()))
 78          (cond ((null? lst1) 
 79                 (if (null? result)
 80                     '()
 81                     (reverse result)))
 82                ((member (car lst1) lst2)
 83                 (build-intersection (cdr lst1) (cons (car lst1) result)))
 84                (else (build-intersection (cdr lst1) result))))))
 85;@
 86(define (cl:set-difference lst1 lst2)
 87  (if (null? lst2)
 88      lst1
 89      (let build-difference ((lst1 lst1)
 90			     (result '()))
 91	(cond ((null? lst1) (reverse result))
 92	      ((member (car lst1) lst2) (build-difference (cdr lst1) result))
 93	      (else (build-difference (cdr lst1) (cons (car lst1) result)))))))
 94;@
 95(define (cl:subset? lst1 lst2)
 96  (or (eq? lst1 lst2)
 97      (let loop ((lst1 lst1))
 98	(or (null? lst1)
 99	    (and (member (car lst1) lst2)
100		 (loop (cdr lst1)))))))
101;@
102(define (cl:position obj lst)
103  (define pos (lambda (n lst)
104		(cond ((null? lst) #f)
105		      ((equal? obj (car lst)) n)
106		      (else (pos (+ 1 n) (cdr lst))))))
107  (pos 0 lst))
108;@
109(define (cl:reduce-init pred? init lst)
110  (if (null? lst)
111      init
112      (cl:reduce-init pred? (pred? init (car lst)) (cdr lst))))
113;@
114(define (cl:reduce pred? lst)
115  (cond ((null? lst) lst)
116	((null? (cdr lst)) (car lst))
117	(else (cl:reduce-init pred? (car lst) (cdr lst)))))
118;@
119(define (cl:some pred lst . rest)
120  (cond ((null? rest)
121	 (let mapf ((lst lst))
122	   (and (not (null? lst))
123		(or (pred (car lst)) (mapf (cdr lst))))))
124	(else (let mapf ((lst lst) (rest rest))
125		(and (not (null? lst))
126		     (or (apply pred (car lst) (map car rest))
127			 (mapf (cdr lst) (map cdr rest))))))))
128;@
129(define (cl:every pred lst . rest)
130  (cond ((null? rest)
131	 (let mapf ((lst lst))
132	   (or (null? lst)
133	       (and (pred (car lst)) (mapf (cdr lst))))))
134	(else (let mapf ((lst lst) (rest rest))
135		(or (null? lst)
136		    (and (apply pred (car lst) (map car rest))
137			 (mapf (cdr lst) (map cdr rest))))))))
138;@
139(define (cl:notany pred . ls) (not (apply cl:some pred ls)))
140;@
141(define (cl:notevery pred . ls) (not (apply cl:every pred ls)))
142;@
143(define (cl:list-of?? predicate . bound)
144  (define (errout) (apply slib:error 'list-of?? predicate bound))
145  (case (length bound)
146    ((0)
147     (lambda (obj)
148       (and (list? obj)
149	    (cl:every predicate obj))))
150    ((1)
151     (set! bound (car bound))
152     (cond ((negative? bound)
153	    (set! bound (- bound))
154	    (lambda (obj)
155	      (and (list? obj)
156		   (<= bound (length obj))
157		   (cl:every predicate obj))))
158	   (else
159	    (lambda (obj)
160	      (and (list? obj)
161		   (<= (length obj) bound)
162		   (cl:every predicate obj))))))
163    ((2)
164     (let ((low (car bound))
165	   (high (cadr bound)))
166       (cond ((or (negative? low) (negative? high)) (errout))
167	     ((< high low)
168	      (set! high (car bound))
169	      (set! low (cadr bound))))
170       (lambda (obj)
171	 (and (list? obj)
172	      (<= low (length obj) high)
173	      (cl:every predicate obj)))))
174    (else (errout))))
175;@
176(define (cl:find-if pred? lst)
177  (cond ((null? lst) #f)
178	((pred? (car lst)) (car lst))
179	(else (cl:find-if pred? (cdr lst)))))
180;@
181(define (cl:member-if pred? lst)
182  (cond ((null? lst) #f)
183	((pred? (car lst)) lst)
184	(else (cl:member-if pred? (cdr lst)))))
185;@
186(define (cl:remove obj lst)
187  (define head (list '*head*))
188  (let remove ((lst lst)
189	       (tail head))
190    (cond ((null? lst))
191	  ((eqv? obj (car lst)) (remove (cdr lst) tail))
192	  (else
193	   (set-cdr! tail (list (car lst)))
194	   (remove (cdr lst) (cdr tail)))))
195  (cdr head))
196;@
197(define (cl:remove-if pred? lst)
198  (let remove-if ((lst lst)
199		  (result '()))
200    (cond ((null? lst) (reverse result))
201	  ((pred? (car lst)) (remove-if (cdr lst) result))
202	  (else (remove-if (cdr lst) (cons (car lst) result))))))
203;@
204(define (cl:remove-if-not pred? lst)
205  (let remove-if-not ((lst lst)
206		      (result '()))
207    (cond ((null? lst) (reverse result))
208	  ((pred? (car lst)) (remove-if-not (cdr lst) (cons (car lst) result)))
209	  (else (remove-if-not (cdr lst) result)))))
210;@
211(define cl:nconc
212      (lambda args
213	(cond ((null? args) '())
214	      ((null? (cdr args)) (car args))
215	      ((null? (car args)) (apply cl:nconc (cdr args)))
216	      (else
217	       (set-cdr! (last-pair (car args))
218			 (apply cl:nconc (cdr args)))
219	       (car args)))))
220
221;;;@ From: hugh@ear.mit.edu (Hugh Secker-Walker)
222(define (cl:nreverse rev-it)
223;;; Reverse order of elements of LIST by mutating cdrs.
224  (cond ((null? rev-it) rev-it)
225	((not (list? rev-it))
226	 (slib:error "nreverse: Not a list in arg1" rev-it))
227	(else (do ((reved '() rev-it)
228		   (rev-cdr (cdr rev-it) (cdr rev-cdr))
229		   (rev-it rev-it rev-cdr))
230		  ((begin (set-cdr! rev-it reved) (null? rev-cdr)) rev-it)))))
231;@
232(define (cl:last lst n)
233  (cl:nthcdr (- (length lst) n) lst))
234;@
235(define (cl:butlast lst n)
236  (cl:butnthcdr (- (length lst) n) lst))
237;@
238(define (cl:nthcdr n lst)
239  (if (zero? n) lst (cl:nthcdr (+ -1 n) (cdr lst))))
240;@
241(define (cl:butnthcdr k lst)
242  (cond ((negative? k) lst) ;(slib:error "negative argument to butnthcdr" k)
243					; SIMSYNCH FIFO8 uses negative k.
244	((or (zero? k) (null? lst)) '())
245	(else (let ((ans (list (car lst))))
246		(do ((lst (cdr lst) (cdr lst))
247		     (tail ans (cdr tail))
248		     (k (+ -2 k) (+ -1 k)))
249		    ((or (negative? k) (null? lst)) ans)
250		  (set-cdr! tail (list (car lst))))))))
251
252;;;; CONDITIONALS
253;@
254(define (cl:and? . args)
255  (cond ((null? args) #t)
256	((car args) (apply cl:and? (cdr args)))
257	(else #f)))
258;@
259(define (cl:or? . args)
260  (cond ((null? args) #f)
261	((car args) #t)
262	(else (apply cl:or? (cdr args)))))
263
264;;;@ Checks to see if a list has any duplicate MEMBERs.
265(define (cl:has-duplicates? lst)
266  (cond ((null? lst) #f)
267	((member (car lst) (cdr lst)) #t)
268	(else (cl:has-duplicates? (cdr lst)))))
269
270
271;;;@ remove duplicates of MEMBERs of a list
272(define cl:remove-duplicates
273   (letrec ((rem-dup  (lambda (lst nlst)
274                         (cond ((null? lst) (if (null? nlst) nlst (reverse nlst)))
275                               ((member (car lst) nlst) (rem-dup (cdr lst) nlst))
276                               (else (rem-dup (cdr lst) (cons (car lst) nlst)))))))
277      (lambda (lst)
278         (rem-dup lst '()))))
279		 
280;@
281(define cl:list*
282  (letrec ((list*1 (lambda (obj)
283		     (if (null? (cdr obj))
284			 (car obj)
285			 (cons (car obj) (list*1 (cdr obj)))))))
286    (lambda (obj1 . obj2)
287      (if (null? obj2)
288	  obj1
289	  (cons obj1 (list*1 obj2))))))
290;@
291(define (cl:atom? obj)
292  (not (pair? obj)))
293;@
294(define (cl:delete obj lst)
295  (let delete ((lst lst))
296    (cond ((null? lst) '())
297	  ((equal? obj (car lst)) (delete (cdr lst)))
298	  (else
299	   (set-cdr! lst (delete (cdr lst)))
300	   lst))))
301;@
302(define (cl:delete-if pred lst)
303  (let delete-if ((lst lst))
304    (cond ((null? lst) '())
305	  ((pred (car lst)) (delete-if (cdr lst)))
306	  (else
307	   (set-cdr! lst (delete-if (cdr lst)))
308	   lst))))
309;@
310(define (cl:delete-if-not pred lst)
311  (let delete-if ((lst lst))
312    (cond ((null? lst) '())
313	  ((not (pred (car lst))) (delete-if (cdr lst)))
314	  (else
315	   (set-cdr! lst (delete-if (cdr lst)))
316	   lst))))