ViewVC logotype

Contents of /src/code/list.lisp

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.40 - (show annotations)
Tue Apr 20 17:57:44 2010 UTC (4 years ago) by rtoy
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, release-20b-pre1, release-20b-pre2, sparc-tramp-assem-2010-07-19, GIT-CONVERSION, cross-sol-x86-merged, RELEASE_20b, cross-sol-x86-base, snapshot-2010-12, snapshot-2010-11, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, snapshot-2010-05, snapshot-2010-07, snapshot-2010-06, snapshot-2010-08, cross-sol-x86-2010-12-20, cross-sparc-branch-base, HEAD
Branch point for: cross-sparc-branch, RELEASE-20B-BRANCH, sparc-tramp-assem-branch, cross-sol-x86-branch
Changes since 1.39: +12 -12 lines
Change uses of _"foo" to (intl:gettext "foo").  This is because slime
may get confused with source locations if the reader macros are
1 ;;; -*- Log: code.log; Package: Lisp -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/list.lisp,v 1.40 2010/04/20 17:57:44 rtoy Rel $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Functions to implement lists for Spice Lisp.
13 ;;; Written by Joe Ginder and Carl Ebeling.
14 ;;; Rewritten and currently maintained by Skef Wholey.
15 ;;;
16 ;;; Nsublis, things at the beginning broken.
17 ;;;
18 ;;; The list functions are part of the standard Spice Lisp environment.
19 ;;;
20 ;;; **********************************************************************
21 ;;;
22 (in-package "LISP")
24 (intl:textdomain "cmucl")
26 (export '(car cdr caar
27 cadr cdar cddr caaar caadr cadar caddr cdaar cdadr
28 cddar cdddr caaaar caaadr caadar caaddr cadaar cadadr
29 caddar cadddr cdaaar cdaadr cdadar cdaddr cddaar cddadr
30 cdddar cddddr cons tree-equal endp list-length nth first
31 second third fourth fifth sixth seventh eighth
32 ninth tenth rest nthcdr last list list* make-list
33 append copy-list copy-alist copy-tree revappend nconc
34 nreconc butlast nbutlast ldiff rplaca rplacd subst
35 subst-if subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis
36 member member-if member-if-not tailp adjoin union
37 nunion intersection nintersection set-difference
38 nset-difference set-exclusive-or nset-exclusive-or subsetp
39 acons pairlis
40 assoc assoc-if assoc-if-not
41 rassoc rassoc-if rassoc-if-not
42 complement constantly))
44 (declaim (maybe-inline
45 tree-equal list-length nth %setnth nthcdr last make-list append
46 copy-list copy-alist copy-tree revappend nconc nreconc butlast
47 nbutlast ldiff member member-if member-if-not tailp adjoin union
48 nunion intersection nintersection set-difference nset-difference
49 set-exclusive-or nset-exclusive-or subsetp acons pairlis assoc
50 assoc-if assoc-if-not rassoc rassoc-if rassoc-if-not subst subst-if
51 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
54 (in-package "EXTENSIONS")
55 (export '(assq memq delq))
56 (declaim (maybe-inline delq))
57 (in-package "LISP")
60 ;;; These functions perform basic list operations:
62 (defun car (list) "Returns the 1st object in a list." (car list))
63 (defun cdr (list) "Returns all but the first object." (cdr list))
64 (defun cadr (list) "Returns the 2nd object in a list." (cadr list))
65 (defun cdar (list) "Returns the cdr of the 1st sublist." (cdar list))
66 (defun caar (list) "Returns the car of the 1st sublist." (caar list))
67 (defun cddr (list) "Returns all but the 1st two objects of a list." (cddr list))
68 (defun caddr (list) "Returns the 1st object in the cddr of a list." (caddr list))
69 (defun caadr (list) "Returns the 1st object in the cadr of a list." (caadr list))
70 (defun caaar (list) "Returns the 1st object in the caar of a list." (caaar list))
71 (defun cdaar (list) "Returns the cdr of the caar of a list." (cdaar list))
72 (defun cddar (list) "Returns the cdr of the cdar of a list." (cddar list))
73 (defun cdddr (list) "Returns the cdr of the cddr of a list." (cdddr list))
74 (defun cadar (list) "Returns the car of the cdar of a list." (cadar list))
75 (defun cdadr (list) "Returns the cdr of the cadr of a list." (cdadr list))
76 (defun caaaar (list) "Returns the car of the caaar of a list." (caaaar list))
77 (defun caaadr (list) "Returns the car of the caadr of a list." (caaadr list))
78 (defun caaddr (list) "Returns the car of the caddr of a list." (caaddr list))
79 (defun cadddr (list) "Returns the car of the cdddr of a list." (cadddr list))
80 (defun cddddr (list) "Returns the cdr of the cdddr of a list." (cddddr list))
81 (defun cdaaar (list) "Returns the cdr of the caaar of a list." (cdaaar list))
82 (defun cddaar (list) "Returns the cdr of the cdaar of a list." (cddaar list))
83 (defun cdddar (list) "Returns the cdr of the cddar of a list." (cdddar list))
84 (defun caadar (list) "Returns the car of the cadar of a list." (caadar list))
85 (defun cadaar (list) "Returns the car of the cdaar of a list." (cadaar list))
86 (defun cadadr (list) "Returns the car of the cdadr of a list." (cadadr list))
87 (defun caddar (list) "Returns the car of the cddar of a list." (caddar list))
88 (defun cdaadr (list) "Returns the cdr of the caadr of a list." (cdaadr list))
89 (defun cdadar (list) "Returns the cdr of the cadar of a list." (cdadar list))
90 (defun cdaddr (list) "Returns the cdr of the caddr of a list." (cdaddr list))
91 (defun cddadr (list) "Returns the cdr of the cdadr of a list." (cddadr list))
92 (defun cons (se1 se2) "Returns a list with se1 as the car and se2 as the cdr."
93 (cons se1 se2))
96 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
98 (defun tree-equal-test-not (x y test-not)
99 (cond ((consp x)
100 (and (consp y)
101 (tree-equal-test-not (car x) (car y) test-not)
102 (tree-equal-test-not (cdr x) (cdr y) test-not)))
103 ((consp y) nil)
104 ((not (funcall test-not x y)) t)
105 (t ())))
107 (defun tree-equal-test (x y test)
108 (cond ((consp x)
109 (and (consp y)
110 (tree-equal-test (car x) (car y) test)
111 (tree-equal-test (cdr x) (cdr y) test)))
112 ((consp y) nil)
113 ((funcall test x y) t)
114 (t ())))
116 (defun tree-equal (x y &key (test #'eql) test-not)
117 "Returns T if X and Y are isomorphic trees with identical leaves."
118 (if test-not
119 (tree-equal-test-not x y test-not)
120 (tree-equal-test x y test)))
123 (defun endp (object)
124 "The recommended way to test for the end of a list. True if Object is nil,
125 false if Object is a cons, and an error for any other types of arguments."
126 (endp object))
128 (defun list-length (list)
129 "Returns the length of the given List, or Nil if the List is circular."
130 (do ((n 0 (+ n 2))
131 (y list (cddr y))
132 (z list (cdr z)))
133 (())
134 (declare (fixnum n) (list y z))
135 (when (endp y) (return n))
136 (when (endp (cdr y)) (return (+ n 1)))
137 (when (and (eq y z) (> n 0)) (return nil))))
139 (defun nth (n list)
140 "Returns the nth object in a list where the car is the zero-th element."
141 (car (nthcdr n list)))
143 (defun first (list)
144 "Returns the 1st object in a list or NIL if the list is empty."
145 (car list))
146 (defun second (list)
147 "Returns the 2nd object in a list or NIL if there is no 2nd object."
148 (cadr list))
149 (defun third (list)
150 "Returns the 3rd object in a list or NIL if there is no 3rd object."
151 (caddr list))
152 (defun fourth (list)
153 "Returns the 4th object in a list or NIL if there is no 4th object."
154 (cadddr list))
155 (defun fifth (list)
156 "Returns the 5th object in a list or NIL if there is no 5th object."
157 (car (cddddr list)))
158 (defun sixth (list)
159 "Returns the 6th object in a list or NIL if there is no 6th object."
160 (cadr (cddddr list)))
161 (defun seventh (list)
162 "Returns the 7th object in a list or NIL if there is no 7th object."
163 (caddr (cddddr list)))
164 (defun eighth (list)
165 "Returns the 8th object in a list or NIL if there is no 8th object."
166 (cadddr (cddddr list)))
167 (defun ninth (list)
168 "Returns the 9th object in a list or NIL if there is no 9th object."
169 (car (cddddr (cddddr list))))
170 (defun tenth (list)
171 "Returns the 10th object in a list or NIL if there is no 10th object."
172 (cadr (cddddr (cddddr list))))
173 (defun rest (list)
174 "Means the same as the cdr of a list."
175 (cdr list))
177 (defun nthcdr (n list)
178 "Performs the cdr function n times on a list."
179 (flet ((fast-nthcdr (n list)
180 (declare (type index n))
181 (do ((i n (1- i))
182 (result list (cdr result)))
183 ((not (plusp i)) result)
184 (declare (type index i)))))
185 (typecase n
186 (index (fast-nthcdr n list))
187 (t (do ((i 0 (1+ i))
188 (r-i list (cdr r-i))
189 (r-2i list (cddr r-2i)))
190 ((and (eq r-i r-2i) (not (zerop i)))
191 (fast-nthcdr (mod n i) r-i))
192 (declare (type index i)))))))
194 (defun last (list &optional (n 1))
195 "Returns the last N conses (not the last element!) of a list."
196 (declare (type unsigned-byte n))
197 (if (typep n 'index)
198 (do ((checked-list list (cdr checked-list))
199 (returned-list list)
200 (index 0 (1+ index)))
201 ((atom checked-list) returned-list)
202 (declare (type index index))
203 (if (>= index n)
204 (pop returned-list)))
205 list))
207 (defun list (&rest args)
208 "Returns constructs and returns a list of its arguments."
209 args)
211 ;;; List* is done the same as list, except that the last cons is made a
212 ;;; dotted pair
214 (defun list* (arg &rest others)
215 "Returns a list of the arguments with last cons a dotted pair"
216 (cond ((atom others) arg)
217 ((atom (cdr others)) (cons arg (car others)))
218 (t (do ((x others (cdr x)))
219 ((null (cddr x)) (rplacd x (cadr x))))
220 (cons arg others))))
222 (defun make-list (size &key initial-element)
223 "Constructs a list with size elements each set to value"
224 (declare (type index size))
225 (do ((count size (1- count))
226 (result '() (cons initial-element result)))
227 ((zerop count) result)
228 (declare (type index count))))
231 ;; Test if LIST is a proper list. If so, the length of the list is
232 ;; returned.
233 (defun proper-list-p (list)
234 ;; Basically the same as LIST-LENGTH, but with a few extra checks.
235 (do ((n 0 (+ n 2))
236 (y list (cddr y))
237 (z list (cdr z)))
238 (nil)
239 (declare (fixnum n) (list y z))
240 (unless (listp y)
241 (return nil))
242 (when (endp y) (return n))
243 (unless (listp (cdr y))
244 (return nil))
245 (when (endp (cdr y)) (return (+ n 1)))
246 (when (and (eq y z) (> n 0)) (return nil))))
248 ;; Signal a simple-type-error that LIST is not a proper list.
249 (defun not-proper-list-error (list)
250 (error 'simple-type-error
251 :datum list
252 :expected-type '(satisfies proper-list-p)
253 :format-control (intl:gettext "~S is not a proper list")
254 :format-arguments (list list)))
256 ;;; The outer loop finds the first non-null list and the result is started.
257 ;;; The remaining lists in the arguments are tacked to the end of the result
258 ;;; using splice which cdr's down the end of the new list
260 (defun append (&rest args)
261 "Typically, returns a new list that is the concatenation of Args.
263 Each Arg in Args must be a proper list except the last one, which
264 may be any object. The function is not destructive: for all but the
265 last Arg, its list structure is copied. The last argument is not
266 copied; it becomes the cdr of the final dotted pair of the
267 concatenation of the preceding lists, or is returned directly if
268 there are no preceding non-empty lists. In the latter case, if the
269 last Arg is not a list, the returned value is not a list either."
271 (do ((top args (cdr top))) ;;Cdr to first non-null list.
272 ((atom top) '())
273 (cond ((null (car top))) ; Nil -> Keep looping
274 ((not (consp (car top))) ; Non cons
275 (if (cdr top)
276 (error (intl:gettext "~S is not a list.") (car top))
277 (return (car top))))
278 (t ; Start appending
279 (return
280 (if (atom (cdr top))
281 (car top) ;;Special case.
282 (let* ((result (cons (caar top) '()))
283 (splice result))
284 (do ((x (cdar top) (cdr x))) ;;Copy first list
285 ((atom x)
286 (unless (null x)
287 (not-proper-list-error (car top))))
288 (setq splice
289 (cdr (rplacd splice (cons (car x) ()) ))) )
290 (do ((y (cdr top) (cdr y))) ;; Copy rest of args.
291 ((atom (cdr y))
292 (setq splice (rplacd splice (car y)))
293 result)
294 (if (listp (car y))
295 (do ((x (car y) (cdr x))) ;;Inner copy loop.
296 ((atom x)
297 (unless (null x)
298 (not-proper-list-error (car y))))
299 (setq
300 splice
301 (cdr (rplacd splice (cons (car x) ())))))
302 (error (intl:gettext "~S is not a list.") (car y)))))))))))
305 ;;; List Copying Functions
307 ;;; The list is copied correctly even if the list is not terminated by ()
308 ;;; The new list is built by cdr'ing splice which is always at the tail
309 ;;; of the new list
311 (defun copy-list (list)
312 "Returns a new list EQUAL but not EQ to list"
313 (if (atom list)
314 list
315 (let ((result (list (car list))))
316 (do ((x (cdr list) (cdr x))
317 (splice result
318 (cdr (rplacd splice (cons (car x) '() ))) ))
319 ((atom x)
320 (unless (null x)
321 (rplacd splice x))))
322 result)))
324 (defun copy-alist (alist)
325 "Returns a new association list equal to alist, constructed in space"
326 (if (atom alist)
327 alist
328 (let ((result
329 (cons (if (atom (car alist))
330 (car alist)
331 (cons (caar alist) (cdar alist)) )
332 nil)))
333 (do ((x (cdr alist) (cdr x))
334 (splice result
335 (cdr (rplacd splice
336 (cons
337 (if (atom (car x))
338 (car x)
339 (cons (caar x) (cdar x)))
340 nil)))))
341 ((atom x)
342 (unless (null x)
343 (not-proper-list-error alist))))
344 result)))
346 (defun copy-tree (object)
347 "Copy-Tree recursively copys trees of conses."
348 (if (consp object)
349 (cons (copy-tree (car object)) (copy-tree (cdr object)))
350 object))
353 ;;; More Commonly-used List Functions
355 (defun revappend (x y)
356 "Returns (append (reverse x) y)"
357 (do ((top x (cdr top))
358 (result y (cons (car top) result)))
359 ((endp top) result)))
361 ;;; NCONC finds the first non-null list, so it can make splice point to a cons.
362 ;;; After finding the first cons element, it holds it in a result variable
363 ;;; while running down successive elements tacking them together. While
364 ;;; tacking lists together, if we encounter a null list, we set the previous
365 ;;; list's last cdr to nil just in case it wasn't already nil, and it could
366 ;;; have been dotted while the null list was the last argument to NCONC. The
367 ;;; manipulation of splice (that is starting it out on a first cons, setting
368 ;;; LAST of splice, and setting splice to ele) inherently handles (nconc x x),
369 ;;; and it avoids running down the last argument to NCONC which allows the last
370 ;;; argument to be circular.
371 ;;;
372 (defun nconc (&rest lists)
373 "Concatenates the lists given as arguments (by changing them)"
374 (do ((top lists (cdr top)))
375 ((null top) nil)
376 (let ((top-of-top (car top)))
377 (typecase top-of-top
378 (cons
379 (let* ((result top-of-top)
380 (splice result))
381 (do ((elements (cdr top) (cdr elements)))
382 ((endp elements))
383 (let ((ele (car elements)))
384 (typecase ele
385 (cons (rplacd (last splice) ele)
386 (setf splice ele))
387 (null (rplacd (last splice) nil))
388 (atom (if (cdr elements)
389 (error (intl:gettext "Argument is not a list -- ~S.") ele)
390 (rplacd (last splice) ele)))
391 (t (error (intl:gettext "Argument is not a list -- ~S.") ele)))))
392 (return result)))
393 (null)
394 (atom
395 (if (cdr top)
396 (error (intl:gettext "Argument is not a list -- ~S.") top-of-top)
397 (return top-of-top)))
398 (t (error (intl:gettext "Argument is not a list -- ~S.") top-of-top))))))
400 (defun nreconc (x y)
401 "Returns (nconc (nreverse x) y)"
402 (do ((1st (cdr x) (if (atom 1st) 1st (cdr 1st)))
403 (2nd x 1st) ;2nd follows first down the list.
404 (3rd y 2nd)) ;3rd follows 2nd down the list.
405 ((atom 2nd)
406 (if 2nd
407 ;; KLUDGE. The datum here is wrong. We really want X, but
408 ;; we've already trashed it by the time we get here. So
409 ;; use 2nd. (We could check X before doing anything, but
410 ;; that makes this more expensive, so we don't.)
411 (error 'simple-type-error
412 :datum 2nd
413 :expected-type 'list
414 :format-control (intl:gettext "First argument is not a proper list.")
415 :format-arguments nil)
416 3rd))
417 (rplacd 2nd 3rd)))
419 (defun butlast (list &optional (n 1))
420 "Returns a new list the same as List without the last N conses.
421 List must not be circular."
422 (declare (list list) (type unsigned-byte n))
423 (when (and list (typep n 'index))
424 (let ((length (do ((list list (cdr list))
425 (i 0 (1+ i)))
426 ((atom list) (1- i)))))
427 (declare (type index length))
428 (unless (< length n)
429 (do* ((top (cdr list) (cdr top))
430 (result (list (car list)))
431 (splice result)
432 (count length (1- count)))
433 ((= count n) result)
434 (declare (type index count))
435 (setq splice (cdr (rplacd splice (list (car top))))))))))
437 (defun nbutlast (list &optional (n 1))
438 "Modifies List to remove the last N conses. List must not be circular."
439 (declare (list list) (type unsigned-byte n))
440 (when (and list (typep n 'index))
441 (let ((length (do ((list list (cdr list))
442 (i 0 (1+ i)))
443 ((atom list) (1- i)))))
444 (declare (type index length))
445 (unless (< length n)
446 (do ((1st (cdr list) (cdr 1st))
447 (2nd list 1st)
448 (count length (1- count)))
449 ((= count n)
450 (rplacd 2nd ())
451 list)
452 (declare (type index count)))))))
454 (defun ldiff (list object)
455 "Returns a new list, whose elements are those of List that appear before
456 Object. If Object is not a tail of List, a copy of List is returned.
457 List must be a proper list or a dotted list."
458 (do* ((list list (cdr list))
459 (result (list ()))
460 (splice result))
461 ((atom list)
462 (if (eql list object)
463 (cdr result)
464 (progn (rplacd splice list) (cdr result))))
465 (if (eql list object)
466 (return (cdr result))
467 (setq splice (cdr (rplacd splice (list (car list))))))))
469 ;;; Functions to alter list structure
471 (defun rplaca (x y)
472 "Changes the car of x to y and returns the new x."
473 (rplaca x y))
475 (defun rplacd (x y)
476 "Changes the cdr of x to y and returns the new x."
477 (rplacd x y))
479 ;;; The following are for use by SETF.
481 (defun %rplaca (x val) (rplaca x val) val)
483 (defun %rplacd (x val) (rplacd x val) val)
485 (defun %setnth (n list newval)
486 (declare (type index n))
487 "Sets the Nth element of List (zero based) to Newval."
488 (do ((count n (1- count))
489 (list list (cdr list)))
490 ((endp list)
491 (error (intl:gettext "~S is too large an index for SETF of NTH.") n))
492 (declare (fixnum count))
493 (when (<= count 0)
494 (rplaca list newval)
495 (return newval))))
498 ;;;; :key arg optimization to save funcall of IDENTITY.
500 ;;; APPLY-KEY saves us a function call sometimes.
501 ;;; This is not in and (eval-when (compile eval) ...
502 ;;; because this is used in seq.lisp and sort.lisp.
503 ;;;
504 (defmacro apply-key (key element)
505 `(if ,key
506 (funcall ,key ,element)
507 ,element))
509 (defun identity (thing)
510 "Returns what was passed to it."
511 thing)
514 (defun complement (function)
515 "Builds a new function that returns T whenever FUNCTION returns NIL and
516 NIL whenever FUNCTION returns T."
517 #'(lambda (&optional (arg0 nil arg0-p) (arg1 nil arg1-p) (arg2 nil arg2-p)
518 &rest more-args)
519 (not (cond (more-args (apply function arg0 arg1 arg2 more-args))
520 (arg2-p (funcall function arg0 arg1 arg2))
521 (arg1-p (funcall function arg0 arg1))
522 (arg0-p (funcall function arg0))
523 (t (funcall function))))))
526 (defun constantly (value &optional (val1 nil val1-p) (val2 nil val2-p)
527 &rest more-values)
528 "Builds a function that always returns VALUE, and posisbly MORE-VALUES."
529 (cond (more-values
530 (let ((list (list* value val1 val2 more-values)))
531 #'(lambda ()
532 (declare (ext:optimize-interface (speed 3) (safety 0)))
533 (values-list list))))
534 (val2-p
535 #'(lambda ()
536 (declare (ext:optimize-interface (speed 3) (safety 0)))
537 (values value val1 val2)))
538 (val1-p
539 #'(lambda ()
540 (declare (ext:optimize-interface (speed 3) (safety 0)))
541 (values value val1)))
542 (t
543 #'(lambda ()
544 (declare (ext:optimize-interface (speed 3) (safety 0)))
545 value))))
548 ;;;; Macros for (&key (key #'identity) (test #'eql testp) (test-not nil notp)).
549 ;;; Use these with the following keyword args:
550 ;;;
551 (defmacro with-set-keys (funcall)
552 `(cond ((and testp notp) (error "Test and test-not both supplied."))
553 (notp ,(append funcall '(:key key :test-not test-not)))
554 (t ,(append funcall '(:key key :test test)))))
556 (defmacro satisfies-the-test (item elt)
557 (let ((key-tmp (gensym)))
558 `(let ((,key-tmp (apply-key key ,elt)))
559 (cond (testp (funcall test ,item ,key-tmp))
560 (notp (not (funcall test-not ,item ,key-tmp)))
561 (t (funcall test ,item ,key-tmp))))))
564 ;;; Substitution of expressions
568 (defun subst (new old tree &key key (test #'eql testp) (test-not nil notp))
569 "Substitutes new for subtrees matching old."
570 (labels ((s (subtree)
571 (cond ((satisfies-the-test old subtree) new)
572 ((atom subtree) subtree)
573 (t (let ((car (s (car subtree)))
574 (cdr (s (cdr subtree))))
575 (if (and (eq car (car subtree))
576 (eq cdr (cdr subtree)))
577 subtree
578 (cons car cdr)))))))
579 (s tree)))
581 (defun subst-if (new test tree &key key)
582 "Substitutes new for subtrees for which test is true."
583 (labels ((s (subtree)
584 (cond ((funcall test (apply-key key subtree)) new)
585 ((atom subtree) subtree)
586 (t (let ((car (s (car subtree)))
587 (cdr (s (cdr subtree))))
588 (if (and (eq car (car subtree))
589 (eq cdr (cdr subtree)))
590 subtree
591 (cons car cdr)))))))
592 (s tree)))
594 (defun subst-if-not (new test tree &key key)
595 "Substitutes new for subtrees for which test is false."
596 (labels ((s (subtree)
597 (cond ((not (funcall test (apply-key key subtree))) new)
598 ((atom subtree) subtree)
599 (t (let ((car (s (car subtree)))
600 (cdr (s (cdr subtree))))
601 (if (and (eq car (car subtree))
602 (eq cdr (cdr subtree)))
603 subtree
604 (cons car cdr)))))))
605 (s tree)))
607 (defun nsubst (new old tree &key key (test #'eql testp) (test-not nil notp))
608 "Substitutes new for subtrees matching old."
609 (labels ((s (subtree)
610 (cond ((satisfies-the-test old subtree) new)
611 ((atom subtree) subtree)
612 (t (do* ((last nil subtree)
613 (subtree subtree (Cdr subtree)))
614 ((atom subtree)
615 (if (satisfies-the-test old subtree)
616 (setf (cdr last) new)))
617 (if (satisfies-the-test old subtree)
618 (return (setf (cdr last) new))
619 (setf (car subtree) (s (car subtree)))))
620 subtree))))
621 (s tree)))
623 (defun nsubst-if (new test tree &key key)
624 "Substitutes new for subtrees of tree for which test is true."
625 (labels ((s (subtree)
626 (cond ((funcall test (apply-key key subtree)) new)
627 ((atom subtree) subtree)
628 (t (do* ((last nil subtree)
629 (subtree subtree (Cdr subtree)))
630 ((atom subtree)
631 (if (funcall test (apply-key key subtree))
632 (setf (cdr last) new)))
633 (if (funcall test (apply-key key subtree))
634 (return (setf (cdr last) new))
635 (setf (car subtree) (s (car subtree)))))
636 subtree))))
637 (s tree)))
639 (defun nsubst-if-not (new test tree &key key)
640 "Substitutes new for subtrees of tree for which test is false."
641 (labels ((s (subtree)
642 (cond ((not (funcall test (apply-key key subtree))) new)
643 ((atom subtree) subtree)
644 (t (do* ((last nil subtree)
645 (subtree subtree (Cdr subtree)))
646 ((atom subtree)
647 (if (not (funcall test (apply-key key subtree)))
648 (setf (cdr last) new)))
649 (if (not (funcall test (apply-key key subtree)))
650 (return (setf (cdr last) new))
651 (setf (car subtree) (s (car subtree)))))
652 subtree))))
653 (s tree)))
658 (defun sublis (alist tree &key key (test #'eql) (test-not nil notp))
659 "Substitutes from alist into tree nondestructively."
660 (declare (inline assoc))
661 (labels ((s (subtree)
662 (let* ((key-val (apply-key key subtree))
663 (assoc (if notp
664 (assoc key-val alist :test-not test-not)
665 (assoc key-val alist :test test))))
666 (cond (assoc (cdr assoc))
667 ((atom subtree) subtree)
668 (t (let ((car (s (car subtree)))
669 (cdr (s (cdr subtree))))
670 (if (and (eq car (car subtreE))
671 (eq cdr (cdr subtree)))
672 subtree
673 (cons car cdr))))))))
674 (s tree)))
676 ;;; In run-time env, since can be referenced in line expansions.
677 (defmacro nsublis-macro ()
678 (let ((key-tmp (gensym)))
679 `(let ((,key-tmp (apply-key key subtree)))
680 (if notp
681 (assoc ,key-tmp alist :test-not test-not)
682 (assoc ,key-tmp alist :test test)))))
684 (defun nsublis (alist tree &key key (test #'eql) (test-not nil notp))
685 "Substitutes new for subtrees matching old."
686 (declare (inline assoc))
687 (let (temp)
688 (labels ((s (subtree)
689 (cond ((Setq temp (nsublis-macro))
690 (cdr temp))
691 ((atom subtree) subtree)
692 (t (do* ((last nil subtree)
693 (subtree subtree (Cdr subtree)))
694 ((atom subtree)
695 (if (setq temp (nsublis-macro))
696 (setf (cdr last) (cdr temp))))
697 (if (setq temp (nsublis-macro))
698 (return (setf (Cdr last) (Cdr temp)))
699 (setf (car subtree) (s (car subtree)))))
700 subtree))))
701 (s tree))))
704 ;;;; Functions for using lists as sets
706 (defun member (item list &key key (test #'eql testp) (test-not nil notp))
707 "Returns tail of list beginning with first element satisfying EQLity,
708 :test, or :test-not with a given item."
709 (do ((list list (cdr list)))
710 ((null list) nil)
711 (let ((car (car list)))
712 (if (satisfies-the-test item car)
713 (return list)))))
715 (defun member-if (test list &key key)
716 "Returns tail of list beginning with first element satisfying test(element)"
717 (do ((list list (Cdr list)))
718 ((endp list) nil)
719 (if (funcall test (apply-key key (car list)))
720 (return list))))
722 (defun member-if-not (test list &key key)
723 "Returns tail of list beginning with first element not satisfying test(el)"
724 (do ((list list (cdr list)))
725 ((endp list) ())
726 (if (not (funcall test (apply-key key (car list))))
727 (return list))))
729 (defun tailp (object list)
730 "Returns true if Object is the same as some tail of List, otherwise
731 returns false. List must be a proper list or a dotted list."
732 (do ((list list (cdr list)))
733 ((atom list) (eql list object))
734 (if (eql object list)
735 (return t))))
737 (defun adjoin (item list &key key (test #'eql) (test-not nil notp))
738 "Add item to list unless it is already a member"
739 (declare (inline member))
740 (if (let ((key-val (apply-key key item)))
741 (if notp
742 (member key-val list :test-not test-not :key key)
743 (member key-val list :test test :key key)))
744 list
745 (cons item list)))
748 ;;; UNION -- Public.
749 ;;;
750 ;;; This function assumes list2 is the result, adding to it from list1 as
751 ;;; necessary. List2 must initialize the result value, so the call to MEMBER
752 ;;; will apply the test to the elements from list1 and list2 in the correct
753 ;;; order.
754 ;;;
755 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
756 "Returns the union of list1 and list2."
757 (declare (inline member))
758 (when (and testp notp) (error (intl:gettext "Test and test-not both supplied.")))
759 (let ((res list2))
760 (dolist (elt list1)
761 (unless (with-set-keys (member (apply-key key elt) list2))
762 (push elt res)))
763 res))
765 ;;; Destination and source are setf-able and many-evaluable. Sets the source
766 ;;; to the cdr, and "conses" the 1st elt of source to destination.
767 ;;;
768 (defmacro steve-splice (source destination)
769 `(let ((temp ,source))
770 (setf ,source (cdr ,source)
771 (cdr temp) ,destination
772 ,destination temp)))
774 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
775 "Destructively returns the union list1 and list2."
776 (declare (inline member))
777 (if (and testp notp)
778 (error "Test and test-not both supplied."))
779 (let ((res list2)
780 (list1 list1))
781 (do ()
782 ((endp list1))
783 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
784 (steve-splice list1 res)
785 (setf list1 (cdr list1))))
786 res))
789 (defun intersection (list1 list2 &key key
790 (test #'eql testp) (test-not nil notp))
791 "Returns the intersection of list1 and list2."
792 (declare (inline member))
793 (if (and testp notp)
794 (error "Test and test-not both supplied."))
795 (let ((res nil))
796 (dolist (elt list1)
797 (if (with-set-keys (member (apply-key key elt) list2))
798 (push elt res)))
799 res))
801 (defun nintersection (list1 list2 &key key
802 (test #'eql testp) (test-not nil notp))
803 "Destructively returns the intersection of list1 and list2."
804 (declare (inline member))
805 (if (and testp notp)
806 (error "Test and test-not both supplied."))
807 (let ((res nil)
808 (list1 list1))
809 (do () ((endp list1))
810 (if (with-set-keys (member (apply-key key (car list1)) list2))
811 (steve-splice list1 res)
812 (setq list1 (Cdr list1))))
813 res))
815 (defun set-difference (list1 list2 &key key
816 (test #'eql testp) (test-not nil notp))
817 "Returns the elements of list1 which are not in list2."
818 (declare (inline member))
819 (if (and testp notp)
820 (error "Test and test-not both supplied."))
821 (if (null list2)
822 list1
823 (let ((res nil))
824 (dolist (elt list1)
825 (if (not (with-set-keys (member (apply-key key elt) list2)))
826 (push elt res)))
827 res)))
830 (defun nset-difference (list1 list2 &key key
831 (test #'eql testp) (test-not nil notp))
832 "Destructively returns the elements of list1 which are not in list2."
833 (declare (inline member))
834 (if (and testp notp)
835 (error "Test and test-not both supplied."))
836 (let ((res nil)
837 (list1 list1))
838 (do () ((endp list1))
839 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
840 (steve-splice list1 res)
841 (setq list1 (cdr list1))))
842 res))
845 (defun set-exclusive-or (list1 list2 &key key
846 (test #'eql testp) (test-not nil notp))
847 "Return new list of elements appearing exactly once in LIST1 and LIST2."
848 (declare (inline member))
849 (let ((result nil)
850 (key (when key (coerce key 'function)))
851 (test (coerce test 'function))
852 (test-not (if test-not (coerce test-not 'function) #'eql)))
853 (declare (type (or function null) key)
854 (type function test test-not))
855 (dolist (elt list1)
856 (unless (with-set-keys (member (apply-key key elt) list2))
857 (setq result (cons elt result))))
858 (let ((test (if testp
859 (lambda (x y) (funcall test y x))
860 test))
861 (test-not (if notp
862 (lambda (x y) (funcall test-not y x))
863 test-not)))
864 (dolist (elt list2)
865 (unless (with-set-keys (member (apply-key key elt) list1))
866 (setq result (cons elt result)))))
867 result))
870 ;;; The outer loop examines list1 while the inner loop examines list2. If an
871 ;;; element is found in list2 "equal" to the element in list1, both are
872 ;;; spliced out. When the end of list1 is reached, what is left of list2 is
873 ;;; tacked onto what is left of list1. The splicing operation ensures that
874 ;;; the correct operation is performed depending on whether splice is at the
875 ;;; top of the list or not.
877 (defun nset-exclusive-or (list1 list2
878 &key key (test #'eql testp) (test-not #'eql notp))
879 "Destructively return a list with elements which appear but once in LIST1
880 and LIST2."
881 (when (and testp notp)
882 (error ":TEST and :TEST-NOT were both supplied."))
883 ;; The outer loop examines LIST1 while the inner loop examines
884 ;; LIST2. If an element is found in LIST2 "equal" to the element
885 ;; in LIST1, both are spliced out. When the end of LIST1 is
886 ;; reached, what is left of LIST2 is tacked onto what is left of
887 ;; LIST1. The splicing operation ensures that the correct
888 ;; operation is performed depending on whether splice is at the
889 ;; top of the list or not.
890 (do ((list1 list1)
891 (list2 list2)
892 (x list1 (cdr x))
893 (splicex ())
894 ;; elements of LIST2, which are "equal" to some processed
895 ;; earlier elements of LIST1
896 (deleted-y ()))
897 ((endp x)
898 (if (null splicex)
899 (setq list1 list2)
900 (rplacd splicex list2))
901 list1)
902 (let ((key-val-x (apply-key key (car x)))
903 (found-duplicate nil))
905 ;; Move all elements from LIST2, which are "equal" to (CAR X),
906 ;; to DELETED-Y.
907 (do* ((y list2 next-y)
908 (next-y (cdr y) (cdr y))
909 (splicey ()))
910 ((endp y))
911 (cond ((let ((key-val-y (apply-key key (car y))))
912 (if notp
913 (not (funcall test-not key-val-x key-val-y))
914 (funcall test key-val-x key-val-y)))
915 (if (null splicey)
916 (setq list2 (cdr y))
917 (rplacd splicey (cdr y)))
918 (setq deleted-y (rplacd y deleted-y))
919 (setq found-duplicate t))
920 (t (setq splicey y))))
922 (unless found-duplicate
923 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
925 (if found-duplicate
926 (if (null splicex)
927 (setq list1 (cdr x))
928 (rplacd splicex (cdr x)))
929 (setq splicex x)))))
931 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
932 "Returns T if every element in list1 is also in list2."
933 (declare (inline member))
934 (dolist (elt list1)
935 (unless (with-set-keys (member (apply-key key elt) list2))
936 (return-from subsetp nil)))
937 T)
941 ;;; Functions that operate on association lists
943 (defun acons (key datum alist)
944 "Construct a new alist by adding the pair (key . datum) to alist"
945 (cons (cons key datum) alist))
947 (defun pairlis (keys data &optional (alist '()))
948 "Construct an association list from keys and data (adding to alist)"
949 (do ((x keys (cdr x))
950 (y data (cdr y)))
951 ((and (endp x) (endp y)) alist)
952 (if (or (endp x) (endp y))
953 (error (intl:gettext "The lists of keys and data are of unequal length.")))
954 (setq alist (acons (car x) (car y) alist))))
956 ;;; In run-time environment, since these guys can be inline expanded.
957 (defmacro assoc-guts (test-guy)
958 `(do ((alist alist (cdr alist)))
959 ((endp alist))
960 (declare (optimize (inhibit-warnings 3)))
961 (if (car alist)
962 (if ,test-guy (return (car alist))))))
964 (defun assoc (item alist &key key test test-not)
965 "Returns the cons in alist whose car is equal (by a given test or EQL) to
966 the Item."
967 (cond (test
968 (if key
969 (assoc-guts (funcall test item (funcall key (caar alist))))
970 (assoc-guts (funcall test item (caar alist)))))
971 (test-not
972 (if key
973 (assoc-guts (not (funcall test-not item
974 (funcall key (caar alist)))))
975 (assoc-guts (not (funcall test-not item (caar alist))))))
976 (t
977 (if key
978 (assoc-guts (eql item (funcall key (caar alist))))
979 (assoc-guts (eql item (caar alist)))))))
981 (defun assoc-if (predicate alist &key key)
982 "Returns the first cons in alist whose car satisfies the Predicate. If
983 key is supplied, apply it to the car of each cons before testing."
984 (if key
985 (assoc-guts (funcall predicate (funcall key (caar alist))))
986 (assoc-guts (funcall predicate (caar alist)))))
988 (defun assoc-if-not (predicate alist &key key)
989 "Returns the first cons in alist whose car does not satisfiy the Predicate.
990 If key is supplied, apply it to the car of each cons before testing."
991 (if key
992 (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
993 (assoc-guts (not (funcall predicate (caar alist))))))
996 (defun rassoc (item alist &key key test test-not)
997 (declare (list alist))
998 "Returns the cons in alist whose cdr is equal (by a given test or EQL) to
999 the Item."
1000 (cond (test
1001 (if key
1002 (assoc-guts (funcall test item (funcall key (cdar alist))))
1003 (assoc-guts (funcall test item (cdar alist)))))
1004 (test-not
1005 (if key
1006 (assoc-guts (not (funcall test-not item
1007 (funcall key (cdar alist)))))
1008 (assoc-guts (not (funcall test-not item (cdar alist))))))
1009 (t
1010 (if key
1011 (assoc-guts (eql item (funcall key (cdar alist))))
1012 (assoc-guts (eql item (cdar alist)))))))
1014 (defun rassoc-if (predicate alist &key key)
1015 "Returns the first cons in alist whose cdr satisfies the Predicate. If key
1016 is supplied, apply it to the cdr of each cons before testing."
1017 (if key
1018 (assoc-guts (funcall predicate (funcall key (cdar alist))))
1019 (assoc-guts (funcall predicate (cdar alist)))))
1021 (defun rassoc-if-not (predicate alist &key key)
1022 "Returns the first cons in alist whose cdr does not satisfy the Predicate.
1023 If key is supplied, apply it to the cdr of each cons before testing."
1024 (if key
1025 (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
1026 (assoc-guts (not (funcall predicate (cdar alist))))))
1030 ;;;; Mapping functions.
1032 (defun map1 (function original-arglists accumulate take-car)
1033 "This function is called by mapc, mapcar, mapcan, mapl, maplist, and mapcon.
1034 It Maps function over the arglists in the appropriate way. It is done when any
1035 of the arglists runs out. Until then, it CDRs down the arglists calling the
1036 function and accumulating results as desired."
1038 (let* ((arglists (copy-list original-arglists))
1039 (ret-list (list nil))
1040 (temp ret-list))
1041 (do ((res nil)
1042 (args '() '()))
1043 ((dolist (x arglists nil) (if (null x) (return t)))
1044 (if accumulate
1045 (cdr ret-list)
1046 (car original-arglists)))
1047 (do ((l arglists (cdr l)))
1048 ((null l))
1049 (push (if take-car (caar l) (car l)) args)
1050 (setf (car l) (cdar l)))
1051 (setq res (apply function (nreverse args)))
1052 (case accumulate
1053 (:nconc (setq temp (last (nconc temp res))))
1054 (:list (rplacd temp (list res))
1055 (setq temp (cdr temp)))))))
1058 (defun mapc (function list &rest more-lists)
1059 "Applies fn to successive elements of lists, returns its second argument."
1060 (map1 function (cons list more-lists) nil t))
1062 (defun mapcar (function list &rest more-lists)
1063 "Applies fn to successive elements of list, returns list of results."
1064 (map1 function (cons list more-lists) :list t))
1066 (defun mapcan (function list &rest more-lists)
1067 "Applies fn to successive elements of list, returns NCONC of results."
1068 (map1 function (cons list more-lists) :nconc t))
1070 (defun mapl (function list &rest more-lists)
1071 "Applies fn to successive CDRs of list, returns ()."
1072 (map1 function (cons list more-lists) nil nil))
1074 (defun maplist (function list &rest more-lists)
1075 "Applies fn to successive CDRs of list, returns list of results."
1076 (map1 function (cons list more-lists) :list nil))
1078 (defun mapcon (function list &rest more-lists)
1079 "Applies fn to successive CDRs of lists, returns NCONC of results."
1080 (map1 function (cons list more-lists) :nconc nil))
1083 ;;; Functions for compatibility sake:
1085 (defun memq (item list)
1086 "Returns tail of list beginning with first element eq to item"
1087 (declare (inline member)
1088 (optimize (inhibit-warnings 3))) ; from MEMBER optimizations
1089 (member item list :test #'eq))
1091 (defun assq (item alist)
1092 "Return the first pair of alist where item EQ the key of pair"
1093 (declare (inline assoc))
1094 (assoc item alist :test #'eq))
1096 (defun delq (item list)
1097 "Returns list with all elements with all elements EQ to ITEM deleted."
1098 (let ((list list))
1099 (do ((x list (cdr x))
1100 (splice '()))
1101 ((endp x) list)
1102 (cond ((eq item (car x))
1103 (if (null splice)
1104 (setq list (cdr x))
1105 (rplacd splice (cdr x))))
1106 (T (setq splice x)))))) ; move splice along to include element

  ViewVC Help
Powered by ViewVC 1.1.5