Remove conditional newlines from print-unreadable-object
[sbcl.git] / src / code / list.lisp
blob2ff411625037506c7cc71bebc6fe867970a8bcdd
1 ;;;; functions to implement lists
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;; Limitation: no list might have more than INDEX conses.
16 ;;;; KLUDGE: comment from CMU CL, what does it mean?
17 ;;;; NSUBLIS, things at the beginning broken.
18 ;;;; -- WHN 20000127
20 (declaim (maybe-inline
21 tree-equal %setnth nthcdr
22 tailp union
23 nunion intersection nintersection set-difference nset-difference
24 set-exclusive-or nset-exclusive-or subsetp acons
25 subst subst-if
26 ;; NSUBLIS is >400 lines of assembly. How is it helpful to inline?
27 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
29 ;;; These functions perform basic list operations.
30 (defun car (list) #!+sb-doc "Return the 1st object in a list." (car list))
31 (defun cdr (list)
32 #!+sb-doc "Return all but the first object in a list."
33 (cdr list))
34 (defun cadr (list) #!+sb-doc "Return the 2nd object in a list." (cadr list))
35 (defun cdar (list) #!+sb-doc "Return the cdr of the 1st sublist." (cdar list))
36 (defun caar (list) #!+sb-doc "Return the car of the 1st sublist." (caar list))
37 (defun cddr (list)
38 #!+sb-doc "Return all but the 1st two objects of a list."
39 (cddr list))
40 (defun caddr (list)
41 #!+sb-doc "Return the 1st object in the cddr of a list."
42 (caddr list))
43 (defun caadr (list)
44 #!+sb-doc "Return the 1st object in the cadr of a list."
45 (caadr list))
46 (defun caaar (list)
47 #!+sb-doc "Return the 1st object in the caar of a list."
48 (caaar list))
49 (defun cdaar (list)
50 #!+sb-doc "Return the cdr of the caar of a list."
51 (cdaar list))
52 (defun cddar (list)
53 #!+sb-doc "Return the cdr of the cdar of a list."
54 (cddar list))
55 (defun cdddr (list)
56 #!+sb-doc "Return the cdr of the cddr of a list."
57 (cdddr list))
58 (defun cadar (list)
59 #!+sb-doc "Return the car of the cdar of a list."
60 (cadar list))
61 (defun cdadr (list)
62 #!+sb-doc "Return the cdr of the cadr of a list."
63 (cdadr list))
64 (defun caaaar (list)
65 #!+sb-doc "Return the car of the caaar of a list."
66 (caaaar list))
67 (defun caaadr (list)
68 #!+sb-doc "Return the car of the caadr of a list."
69 (caaadr list))
70 (defun caaddr (list)
71 #!+sb-doc "Return the car of the caddr of a list."
72 (caaddr list))
73 (defun cadddr (list)
74 #!+sb-doc "Return the car of the cdddr of a list."
75 (cadddr list))
76 (defun cddddr (list)
77 #!+sb-doc "Return the cdr of the cdddr of a list."
78 (cddddr list))
79 (defun cdaaar (list)
80 #!+sb-doc "Return the cdr of the caaar of a list."
81 (cdaaar list))
82 (defun cddaar (list)
83 #!+sb-doc "Return the cdr of the cdaar of a list."
84 (cddaar list))
85 (defun cdddar (list)
86 #!+sb-doc "Return the cdr of the cddar of a list."
87 (cdddar list))
88 (defun caadar (list)
89 #!+sb-doc "Return the car of the cadar of a list."
90 (caadar list))
91 (defun cadaar (list)
92 #!+sb-doc "Return the car of the cdaar of a list."
93 (cadaar list))
94 (defun cadadr (list)
95 #!+sb-doc "Return the car of the cdadr of a list."
96 (cadadr list))
97 (defun caddar (list)
98 #!+sb-doc "Return the car of the cddar of a list."
99 (caddar list))
100 (defun cdaadr (list)
101 #!+sb-doc "Return the cdr of the caadr of a list."
102 (cdaadr list))
103 (defun cdadar (list)
104 #!+sb-doc "Return the cdr of the cadar of a list."
105 (cdadar list))
106 (defun cdaddr (list)
107 #!+sb-doc "Return the cdr of the caddr of a list."
108 (cdaddr list))
109 (defun cddadr (list)
110 #!+sb-doc "Return the cdr of the cdadr of a list."
111 (cddadr list))
112 (defun cons (se1 se2)
113 #!+sb-doc "Return a list with SE1 as the CAR and SE2 as the CDR."
114 (cons se1 se2))
116 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
118 (defun tree-equal-test-not (x y test-not)
119 (declare (type function test-not))
120 (cond ((consp x)
121 (and (consp y)
122 (tree-equal-test-not (car x) (car y) test-not)
123 (tree-equal-test-not (cdr x) (cdr y) test-not)))
124 ((consp y) nil)
125 ((not (funcall test-not x y)) t)
126 (t ())))
128 (defun tree-equal-test (x y test)
129 (declare (type function test))
130 (cond ((consp x)
131 (and (consp y)
132 (tree-equal-test (car x) (car y) test)
133 (tree-equal-test (cdr x) (cdr y) test)))
134 ((consp y) nil)
135 ((funcall test x y) t)
136 (t ())))
138 (defun tree-equal (x y &key (test #'eql testp) (test-not nil notp))
139 #!+sb-doc
140 "Return T if X and Y are isomorphic trees with identical leaves."
141 (when (and testp notp)
142 (error ":TEST and :TEST-NOT were both supplied."))
143 (if test-not
144 (tree-equal-test-not x y (%coerce-callable-to-fun test-not))
145 (tree-equal-test x y (%coerce-callable-to-fun test))))
147 (defun endp (object)
148 #!+sb-doc
149 "This is the recommended way to test for the end of a proper list. It
150 returns true if OBJECT is NIL, false if OBJECT is a CONS, and an error
151 for any other type of OBJECT."
152 (endp object))
154 (defun list-length (list)
155 #!+sb-doc
156 "Return the length of the given List, or Nil if the List is circular."
157 (do ((n 0 (+ n 2))
158 (y list (cddr y))
159 (z list (cdr z)))
160 (())
161 (declare (type fixnum n)
162 (type list y z))
163 (when (endp y) (return n))
164 (when (endp (cdr y)) (return (+ n 1)))
165 (when (and (eq y z) (> n 0)) (return nil))))
167 (defun nth (n list)
168 #!+sb-doc
169 "Return the nth object in a list where the car is the zero-th element."
170 (car (nthcdr n list)))
172 (defun first (list)
173 #!+sb-doc
174 "Return the 1st object in a list or NIL if the list is empty."
175 (car list))
176 (defun second (list)
177 #!+sb-doc
178 "Return the 2nd object in a list or NIL if there is no 2nd object."
179 (cadr list))
180 (defun third (list)
181 #!+sb-doc
182 "Return the 3rd object in a list or NIL if there is no 3rd object."
183 (caddr list))
184 (defun fourth (list)
185 #!+sb-doc
186 "Return the 4th object in a list or NIL if there is no 4th object."
187 (cadddr list))
188 (defun fifth (list)
189 #!+sb-doc
190 "Return the 5th object in a list or NIL if there is no 5th object."
191 (car (cddddr list)))
192 (defun sixth (list)
193 #!+sb-doc
194 "Return the 6th object in a list or NIL if there is no 6th object."
195 (cadr (cddddr list)))
196 (defun seventh (list)
197 #!+sb-doc
198 "Return the 7th object in a list or NIL if there is no 7th object."
199 (caddr (cddddr list)))
200 (defun eighth (list)
201 #!+sb-doc
202 "Return the 8th object in a list or NIL if there is no 8th object."
203 (cadddr (cddddr list)))
204 (defun ninth (list)
205 #!+sb-doc
206 "Return the 9th object in a list or NIL if there is no 9th object."
207 (car (cddddr (cddddr list))))
208 (defun tenth (list)
209 #!+sb-doc
210 "Return the 10th object in a list or NIL if there is no 10th object."
211 (cadr (cddddr (cddddr list))))
212 (defun rest (list)
213 #!+sb-doc
214 "Means the same as the cdr of a list."
215 (cdr list))
217 (defun nthcdr (n list)
218 #!+sb-doc
219 "Performs the cdr function n times on a list."
220 (flet ((fast-nthcdr (n list)
221 (declare (type index n))
222 (do ((i n (1- i))
223 (result list (cdr result)))
224 ((not (plusp i)) result)
225 (declare (type index i)))))
226 (typecase n
227 (index (fast-nthcdr n list))
228 (t (do ((i 0 (1+ i))
229 (r-i list (cdr r-i))
230 (r-2i list (cddr r-2i)))
231 ((and (eq r-i r-2i) (not (zerop i)))
232 (fast-nthcdr (mod n i) r-i))
233 (declare (type index i)))))))
235 ;;; LAST
237 ;;; Transforms in src/compiler/srctran.lisp pick the most specific
238 ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
239 (macrolet ((last0-macro ()
240 `(let ((rest list)
241 (list list))
242 (loop (unless (consp rest)
243 (return rest))
244 (shiftf list rest (cdr rest)))))
245 (last1-macro ()
246 `(let ((rest list)
247 (list list))
248 (loop (unless (consp rest)
249 (return list))
250 (shiftf list rest (cdr rest)))))
251 (lastn-macro (type)
252 `(let ((returned-list list)
253 (checked-list list)
254 (n (truly-the ,type n)))
255 (declare (,type n))
256 (tagbody
257 :scan
258 (pop checked-list)
259 (when (atom checked-list)
260 (go :done))
261 (if (zerop (truly-the ,type (decf n)))
262 (go :pop)
263 (go :scan))
264 :pop
265 (pop returned-list)
266 (pop checked-list)
267 (if (atom checked-list)
268 (go :done)
269 (go :pop))
270 :done)
271 returned-list)))
273 (defun %last0 (list)
274 (declare (optimize speed (sb!c::verify-arg-count 0)))
275 (last0-macro))
277 (defun %last1 (list)
278 (declare (optimize speed (sb!c::verify-arg-count 0)))
279 (last1-macro))
281 (defun %lastn/fixnum (list n)
282 (declare (optimize speed (sb!c::verify-arg-count 0))
283 (type (and unsigned-byte fixnum) n))
284 (case n
285 (1 (last1-macro))
286 (0 (last0-macro))
287 (t (lastn-macro fixnum))))
289 (defun %lastn/bignum (list n)
290 (declare (optimize speed (sb!c::verify-arg-count 0))
291 (type (and unsigned-byte bignum) n))
292 (lastn-macro unsigned-byte))
294 (defun last (list &optional (n 1))
295 #!+sb-doc
296 "Return the last N conses (not the last element!) of a list."
297 (case n
298 (1 (last1-macro))
299 (0 (last0-macro))
301 (typecase n
302 (fixnum
303 (lastn-macro fixnum))
304 (bignum
305 (lastn-macro unsigned-byte)))))))
307 (define-compiler-macro last (&whole form list &optional (n 1) &environment env)
308 (if (sb!xc:constantp n env)
309 (case (constant-form-value n env)
310 (0 `(%last0 ,list))
311 (1 `(%last1 ,list))
312 (t form))
313 form))
315 (defun list (&rest args)
316 #!+sb-doc
317 "Return constructs and returns a list of its arguments."
318 args)
320 ;;; LIST* is done the same as LIST, except that the last cons is made
321 ;;; a dotted pair.
323 (defun list* (arg &rest others)
324 #!+sb-doc
325 "Return a list of the arguments with last cons a dotted pair."
326 (let ((length (length others)))
327 (cond ((= length 0) arg)
328 ((= length 1)
329 (cons arg (fast-&rest-nth 0 others)))
331 (let* ((cons (list arg))
332 (result cons)
333 (index 0)
334 (1-length (1- length)))
335 (loop
336 (cond
337 ((< index 1-length)
338 (setf cons
339 (setf (cdr cons)
340 (list (fast-&rest-nth index others))))
341 (incf index))
342 (t (return nil))))
343 (setf (cdr cons) (fast-&rest-nth index others))
344 result)))))
346 (defun make-list (size &key initial-element)
347 #!+sb-doc
348 "Constructs a list with size elements each set to value"
349 (%make-list size initial-element))
350 ;;; This entry point is to be preferred, irrespective of
351 ;;; whether or not the backend has vops for %MAKE-LIST.
352 (defun %make-list (size initial-element)
353 (declare (type index size))
354 (do ((count size (1- count))
355 (result '() (cons initial-element result)))
356 ((<= count 0) result)
357 (declare (type index count))))
359 (defun append (&rest lists)
360 #!+sb-doc
361 "Construct a new list by concatenating the list arguments"
362 (declare (truly-dynamic-extent lists) (optimize speed))
363 (labels ((fail (object)
364 (error 'type-error
365 :datum object
366 :expected-type 'list))
367 (append-into (last-cons current rest)
368 ;; Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST).
369 (declare (cons last-cons rest))
370 (if (listp current)
371 (if (consp current)
372 ;; normal case, cdr down the list
373 (append-into (setf (cdr last-cons) (list (car current)))
374 (cdr current)
375 rest)
376 ;; empty list
377 (let ((more (cdr rest)))
378 (if (null more)
379 (setf (cdr last-cons) (car rest))
380 (append-into last-cons (car rest) more))))
381 (fail current)))
382 (append1 (lists)
383 (let ((current (car lists))
384 (rest (cdr lists)))
385 (cond ((null rest)
386 current)
387 ((consp current)
388 (let ((result (truly-the cons (list (car current)))))
389 (append-into result
390 (cdr current)
391 rest)
392 result))
393 ((null current)
394 (append1 rest))
396 (fail current))))))
397 (append1 lists)))
399 (defun append2 (x y)
400 (declare (optimize speed (sb!c::verify-arg-count 0)))
401 (if (null x)
403 (let ((result (list (car x))))
404 (do ((more (cdr x) (cdr more))
405 (tail result (cdr tail)))
406 ((null more)
407 (rplacd tail y)
408 result)
409 (rplacd tail (list (car more)))))))
412 ;;;; list copying functions
414 (eval-when (:compile-toplevel :load-toplevel :execute)
415 (sb!xc:defmacro !copy-list-macro (list &key check-proper-list)
416 ;; Unless CHECK-PROPER-LIST is true, the list is copied correctly
417 ;; even if the list is not terminated by NIL. The new list is built
418 ;; by CDR'ing SPLICE which is always at the tail of the new list.
419 `(when ,list
420 (let ((copy (list (car ,list))))
421 (do ((orig (cdr ,list) (cdr orig))
422 (splice copy (cdr (rplacd splice (cons (car orig) nil)))))
423 (,@(if check-proper-list
424 '((endp orig))
425 '((atom orig)
426 (unless (null orig)
427 (rplacd splice orig))))
428 copy))))))
430 (defun copy-list (list)
431 #!+sb-doc
432 "Return a new list which is EQUAL to LIST. LIST may be improper."
433 (!copy-list-macro list))
435 (defun copy-alist (alist)
436 #!+sb-doc
437 "Return a new association list which is EQUAL to ALIST."
438 (if (endp alist)
439 alist
440 (let ((result
441 (cons (if (atom (car alist))
442 (car alist)
443 (cons (caar alist) (cdar alist)))
444 nil)))
445 (do ((x (cdr alist) (cdr x))
446 (splice result
447 (cdr (rplacd splice
448 (cons
449 (if (atom (car x))
450 (car x)
451 (cons (caar x) (cdar x)))
452 nil)))))
453 ((endp x)))
454 result)))
456 (defun copy-tree (object)
457 #!+sb-doc
458 "Recursively copy trees of conses."
459 (if (consp object)
460 (let ((result (list (if (consp (car object))
461 (copy-tree (car object))
462 (car object)))))
463 (loop for last-cons = result then new-cons
464 for cdr = (cdr object) then (cdr cdr)
465 for car = (if (consp cdr)
466 (car cdr)
467 (return (setf (cdr last-cons) cdr)))
468 for new-cons = (list (if (consp car)
469 (copy-tree car)
470 car))
471 do (setf (cdr last-cons) new-cons))
472 result)
473 object))
476 ;;;; more commonly-used list functions
478 (defun revappend (x y)
479 #!+sb-doc
480 "Return (append (reverse x) y)."
481 (do ((top x (cdr top))
482 (result y (cons (car top) result)))
483 ((endp top) result)))
485 ;;; NCONC finds the first non-null list, so it can make splice point
486 ;;; to a cons. After finding the first cons element, it holds it in a
487 ;;; result variable while running down successive elements tacking
488 ;;; them together. While tacking lists together, if we encounter a
489 ;;; null list, we set the previous list's last cdr to nil just in case
490 ;;; it wasn't already nil, and it could have been dotted while the
491 ;;; null list was the last argument to NCONC. The manipulation of
492 ;;; splice (that is starting it out on a first cons, setting LAST of
493 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
494 ;;; and it avoids running down the last argument to NCONC which allows
495 ;;; the last argument to be circular.
496 (defun nconc (&rest lists)
497 #!+sb-doc
498 "Concatenates the lists given as arguments (by changing them)"
499 (declare (optimize speed))
500 (flet ((fail (object)
501 (error 'type-error
502 :datum object
503 :expected-type 'list)))
504 (do-rest-arg ((result index) lists)
505 (typecase result
506 (cons
507 (let ((splice result))
508 (do-rest-arg ((ele index) lists (1+ index))
509 (typecase ele
510 (cons (rplacd (last splice) ele)
511 (setf splice ele))
512 (null (rplacd (last splice) nil))
513 (atom (if (< (1+ index) (length lists))
514 (fail ele)
515 (rplacd (last splice) ele)))))
516 (return result)))
517 (null)
518 (atom
519 (if (< (1+ index) (length lists))
520 (fail result)
521 (return result)))))))
523 (defun nreconc (x y)
524 #!+sb-doc
525 "Return (NCONC (NREVERSE X) Y)."
526 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
527 (2nd x 1st) ;2nd follows first down the list.
528 (3rd y 2nd)) ;3rd follows 2nd down the list.
529 ((atom 2nd) 3rd)
530 (rplacd 2nd 3rd)))
532 (defun butlast (list &optional (n 1))
533 (cond ((zerop n)
534 (copy-list list))
535 ((not (typep n 'index))
536 nil)
538 (let ((head (nthcdr (1- n) list)))
539 (and (consp head) ; there are at least n
540 (collect ((copy)) ; conses; copy!
541 (do ((trail list (cdr trail))
542 (head head (cdr head)))
543 ;; HEAD is n-1 conses ahead of TRAIL;
544 ;; when HEAD is at the last cons, return
545 ;; the data copied so far.
546 ((atom (cdr head))
547 (copy))
548 (copy (car trail)))))))))
550 (defun nbutlast (list &optional (n 1))
551 (cond ((zerop n)
552 list)
553 ((not (typep n 'index))
554 nil)
556 (let ((head (nthcdr (1- n) list)))
557 (and (consp head) ; there are more than n
558 (consp (cdr head)) ; conses.
559 ;; TRAIL trails by n cons to be able to
560 ;; cut the list at the cons just before.
561 (do ((trail list (cdr trail))
562 (head (cdr head) (cdr head)))
563 ((atom (cdr head))
564 (setf (cdr trail) nil)
565 list)))))))
567 (defun ldiff (list object)
568 #!+sb-doc
569 "Return a new list, whose elements are those of LIST that appear before
570 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
571 LIST must be a proper list or a dotted list."
572 (do* ((list list (cdr list))
573 (result (list ()))
574 (splice result))
575 ((atom list)
576 (if (eql list object)
577 (cdr result)
578 (progn (rplacd splice list) (cdr result))))
579 (if (eql list object)
580 (return (cdr result))
581 (setq splice (cdr (rplacd splice (list (car list))))))))
583 ;;;; functions to alter list structure
585 (defun rplaca (cons x)
586 #!+sb-doc
587 "Change the CAR of CONS to X and return the CONS."
588 (rplaca cons x))
590 (defun rplacd (cons x)
591 #!+sb-doc
592 "Change the CDR of CONS to X and return the CONS."
593 (rplacd cons x))
595 ;;; The following are for use by SETF.
597 (defun %rplaca (x val) (rplaca x val) val)
599 (defun %rplacd (x val) (rplacd x val) val)
601 ;;; Set the Nth element of LIST to NEWVAL.
602 (defun %setnth (n list newval)
603 (typecase n
604 (index
605 (do ((count n (1- count))
606 (list list (cdr list)))
607 ((endp list)
608 (error "~S is too large an index for SETF of NTH." n))
609 (declare (type fixnum count))
610 (when (<= count 0)
611 (rplaca list newval)
612 (return newval))))
613 (t (let ((cons (nthcdr n list)))
614 (when (endp cons)
615 (error "~S is too large an index for SETF of NTH." n))
616 (rplaca cons newval)
617 newval))))
619 ;;;; :KEY arg optimization to save funcall of IDENTITY
621 ;;; APPLY-KEY saves us a function call sometimes.
622 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
623 ;;; because it's used in seq.lisp and sort.lisp.
624 (defmacro apply-key (key element)
625 `(if ,key
626 (funcall ,key ,element)
627 ,element))
629 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
631 ;;; Use these with the following &KEY args:
632 (defmacro with-set-keys (funcall)
633 `(if notp
634 ,(append funcall '(:key key :test-not test-not))
635 ,(append funcall '(:key key :test test))))
637 (defmacro satisfies-the-test (item elt)
638 (let ((key-tmp (gensym)))
639 `(let ((,key-tmp (apply-key key ,elt)))
640 (cond (testp (funcall test ,item ,key-tmp))
641 (notp (not (funcall test-not ,item ,key-tmp)))
642 (t (funcall test ,item ,key-tmp))))))
644 ;;;; substitution of expressions
646 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
647 #!+sb-doc
648 "Substitutes new for subtrees matching old."
649 (when (and testp notp)
650 (error ":TEST and :TEST-NOT were both supplied."))
651 (let ((key (and key (%coerce-callable-to-fun key)))
652 (test (if testp (%coerce-callable-to-fun test) test))
653 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
654 (declare (type function test test-not))
655 (labels ((s (subtree)
656 (cond ((satisfies-the-test old subtree) new)
657 ((atom subtree) subtree)
658 (t (let ((car (s (car subtree)))
659 (cdr (s (cdr subtree))))
660 (if (and (eq car (car subtree))
661 (eq cdr (cdr subtree)))
662 subtree
663 (cons car cdr)))))))
664 (s tree))))
666 (defun subst-if (new test tree &key key)
667 #!+sb-doc
668 "Substitutes new for subtrees for which test is true."
669 (let ((test (%coerce-callable-to-fun test))
670 (key (and key (%coerce-callable-to-fun key))))
671 (labels ((s (subtree)
672 (cond ((funcall test (apply-key key subtree)) new)
673 ((atom subtree) subtree)
674 (t (let ((car (s (car subtree)))
675 (cdr (s (cdr subtree))))
676 (if (and (eq car (car subtree))
677 (eq cdr (cdr subtree)))
678 subtree
679 (cons car cdr)))))))
680 (s tree))))
682 (defun subst-if-not (new test tree &key key)
683 #!+sb-doc
684 "Substitutes new for subtrees for which test is false."
685 (let ((test (%coerce-callable-to-fun test))
686 (key (and key (%coerce-callable-to-fun key))))
687 (labels ((s (subtree)
688 (cond ((not (funcall test (apply-key key subtree))) new)
689 ((atom subtree) subtree)
690 (t (let ((car (s (car subtree)))
691 (cdr (s (cdr subtree))))
692 (if (and (eq car (car subtree))
693 (eq cdr (cdr subtree)))
694 subtree
695 (cons car cdr)))))))
696 (s tree))))
698 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
699 #!+sb-doc
700 "Substitute NEW for subtrees matching OLD."
701 (when (and testp notp)
702 (error ":TEST and :TEST-NOT were both supplied."))
703 (let ((key (and key (%coerce-callable-to-fun key)))
704 (test (if testp (%coerce-callable-to-fun test) test))
705 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
706 (declare (type function test test-not))
707 (labels ((s (subtree)
708 (cond ((satisfies-the-test old subtree) new)
709 ((atom subtree) subtree)
710 (t (do* ((last nil subtree)
711 (subtree subtree (cdr subtree)))
712 ((atom subtree)
713 (if (satisfies-the-test old subtree)
714 (setf (cdr last) new)))
715 (if (satisfies-the-test old subtree)
716 (return (setf (cdr last) new))
717 (setf (car subtree) (s (car subtree)))))
718 subtree))))
719 (s tree))))
721 (defun nsubst-if (new test tree &key key)
722 #!+sb-doc
723 "Substitute NEW for subtrees of TREE for which TEST is true."
724 (let ((test (%coerce-callable-to-fun test))
725 (key (and key (%coerce-callable-to-fun key))))
726 (labels ((s (subtree)
727 (cond ((funcall test (apply-key key subtree)) new)
728 ((atom subtree) subtree)
729 (t (do* ((last nil subtree)
730 (subtree subtree (cdr subtree)))
731 ((atom subtree)
732 (if (funcall test (apply-key key subtree))
733 (setf (cdr last) new)))
734 (if (funcall test (apply-key key subtree))
735 (return (setf (cdr last) new))
736 (setf (car subtree) (s (car subtree)))))
737 subtree))))
738 (s tree))))
740 (defun nsubst-if-not (new test tree &key key)
741 #!+sb-doc
742 "Substitute NEW for subtrees of TREE for which TEST is false."
743 (let ((test (%coerce-callable-to-fun test))
744 (key (and key (%coerce-callable-to-fun key))))
745 (labels ((s (subtree)
746 (cond ((not (funcall test (apply-key key subtree))) new)
747 ((atom subtree) subtree)
748 (t (do* ((last nil subtree)
749 (subtree subtree (cdr subtree)))
750 ((atom subtree)
751 (if (not (funcall test (apply-key key subtree)))
752 (setf (cdr last) new)))
753 (if (not (funcall test (apply-key key subtree)))
754 (return (setf (cdr last) new))
755 (setf (car subtree) (s (car subtree)))))
756 subtree))))
757 (s tree))))
759 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
760 #!+sb-doc
761 "Substitute from ALIST into TREE nondestructively."
762 (when (and testp notp)
763 (error ":TEST and :TEST-NOT were both supplied."))
764 (let ((key (and key (%coerce-callable-to-fun key)))
765 (test (if testp (%coerce-callable-to-fun test) test))
766 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
767 (declare (type function test test-not))
768 (declare (inline assoc))
769 (labels ((s (subtree)
770 (let* ((key-val (apply-key key subtree))
771 (assoc (if notp
772 (assoc key-val alist :test-not test-not)
773 (assoc key-val alist :test test))))
774 (cond (assoc (cdr assoc))
775 ((atom subtree) subtree)
776 (t (let ((car (s (car subtree)))
777 (cdr (s (cdr subtree))))
778 (if (and (eq car (car subtree))
779 (eq cdr (cdr subtree)))
780 subtree
781 (cons car cdr))))))))
782 (s tree))))
784 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
785 ;;; because it can be referenced in inline expansions.
786 (defmacro nsublis-macro ()
787 (let ((key-tmp (gensym)))
788 `(let ((,key-tmp (apply-key key subtree)))
789 (if notp
790 (assoc ,key-tmp alist :test-not test-not)
791 (assoc ,key-tmp alist :test test)))))
793 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
794 #!+sb-doc
795 "Substitute from ALIST into TRUE destructively."
796 (when (and testp notp)
797 (error ":TEST and :TEST-NOT were both supplied."))
798 (let ((key (and key (%coerce-callable-to-fun key)))
799 (test (if testp (%coerce-callable-to-fun test) test))
800 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
801 (declare (inline assoc))
802 (let (temp)
803 (labels ((s (subtree)
804 (cond ((setq temp (nsublis-macro))
805 (cdr temp))
806 ((atom subtree) subtree)
807 (t (do* ((last nil subtree)
808 (subtree subtree (cdr subtree)))
809 ((atom subtree)
810 (if (setq temp (nsublis-macro))
811 (setf (cdr last) (cdr temp))))
812 (if (setq temp (nsublis-macro))
813 (return (setf (cdr last) (cdr temp)))
814 (setf (car subtree) (s (car subtree)))))
815 subtree))))
816 (s tree)))))
818 ;;;; functions for using lists as sets
820 (defun member (item list &key key (test nil testp) (test-not nil notp))
821 #!+sb-doc
822 "Return the tail of LIST beginning with first element satisfying EQLity,
823 :TEST, or :TEST-NOT with the given ITEM."
824 (when (and testp notp)
825 (error ":TEST and :TEST-NOT were both supplied."))
826 (let ((key (and key (%coerce-callable-to-fun key)))
827 (test (and testp (%coerce-callable-to-fun test)))
828 (test-not (and notp (%coerce-callable-to-fun test-not))))
829 (cond (test
830 (if key
831 (%member-key-test item list key test)
832 (%member-test item list test)))
833 (test-not
834 (if key
835 (%member-key-test-not item list key test-not)
836 (%member-test-not item list test-not)))
838 (if key
839 (%member-key item list key)
840 (%member item list))))))
842 (defun member-if (test list &key key)
843 #!+sb-doc
844 "Return tail of LIST beginning with first element satisfying TEST."
845 (let ((test (%coerce-callable-to-fun test))
846 (key (and key (%coerce-callable-to-fun key))))
847 (if key
848 (%member-if-key test list key)
849 (%member-if test list))))
851 (defun member-if-not (test list &key key)
852 #!+sb-doc
853 "Return tail of LIST beginning with first element not satisfying TEST."
854 (let ((test (%coerce-callable-to-fun test))
855 (key (and key (%coerce-callable-to-fun key))))
856 (if key
857 (%member-if-not-key test list key)
858 (%member-if-not test list))))
860 (defun tailp (object list)
861 #!+sb-doc
862 "Return true if OBJECT is the same as some tail of LIST, otherwise
863 returns false. LIST must be a proper list or a dotted list."
864 (do ((list list (cdr list)))
865 ((atom list) (eql list object))
866 (if (eql object list)
867 (return t))))
869 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
870 #!+sb-doc
871 "Add ITEM to LIST unless it is already a member"
872 (when (and testp notp)
873 (error ":TEST and :TEST-NOT were both supplied."))
874 (let ((key (and key (%coerce-callable-to-fun key)))
875 (test (and testp (%coerce-callable-to-fun test)))
876 (test-not (and notp (%coerce-callable-to-fun test-not))))
877 (cond (test
878 (if key
879 (%adjoin-key-test item list key test)
880 (%adjoin-test item list test)))
881 (test-not
882 (if key
883 (%adjoin-key-test-not item list key test-not)
884 (%adjoin-test-not item list test-not)))
886 (if key
887 (%adjoin-key item list key)
888 (%adjoin item list))))))
890 (defconstant +list-based-union-limit+ 80)
892 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
893 #!+sb-doc
894 "Return the union of LIST1 and LIST2."
895 (declare (inline member))
896 (when (and testp notp)
897 (error ":TEST and :TEST-NOT were both supplied."))
898 ;; We have two possibilities here: for shortish lists we pick up the
899 ;; shorter one as the result, and add the other one to it. For long
900 ;; lists we use a hash-table when possible.
901 (let ((n1 (length list1))
902 (n2 (length list2))
903 (key (and key (%coerce-callable-to-fun key)))
904 (test (if notp
905 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
906 (lambda (x y) (not (funcall test-not-fun x y))))
907 (%coerce-callable-to-fun test))))
908 (multiple-value-bind (short long n-short)
909 (if (< n1 n2)
910 (values list1 list2 n1)
911 (values list2 list1 n2))
912 (if (or (< n-short +list-based-union-limit+)
913 (not (member test (list #'eq #'eql #'equal #'equalp))))
914 (let ((orig short))
915 (dolist (elt long)
916 (unless (member (apply-key key elt) orig :key key :test test)
917 (push elt short)))
918 short)
919 (let ((table (make-hash-table :test test :size (+ n1 n2)))
920 (union nil))
921 (dolist (elt long)
922 (setf (gethash (apply-key key elt) table) elt))
923 (dolist (elt short)
924 (setf (gethash (apply-key key elt) table) elt))
925 (maphash (lambda (k v)
926 (declare (ignore k))
927 (push v union))
928 table)
929 union)))))
931 ;;; Destination and source are SETF-able and many-evaluable. Set the
932 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
934 ;;; FIXME: needs a more mnemonic name
935 (defmacro steve-splice (source destination)
936 `(let ((temp ,source))
937 (setf ,source (cdr ,source)
938 (cdr temp) ,destination
939 ,destination temp)))
941 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
942 #!+sb-doc
943 "Destructively return the union of LIST1 and LIST2."
944 (declare (inline member))
945 (when (and testp notp)
946 (error ":TEST and :TEST-NOT were both supplied."))
947 ;; We have two possibilities here: for shortish lists we pick up the
948 ;; shorter one as the result, and add the other one to it. For long
949 ;; lists we use a hash-table when possible.
950 (let ((n1 (length list1))
951 (n2 (length list2))
952 (key (and key (%coerce-callable-to-fun key)))
953 (test (if notp
954 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
955 (lambda (x y) (not (funcall test-not-fun x y))))
956 (%coerce-callable-to-fun test))))
957 (multiple-value-bind (short long n-short)
958 (if (< n1 n2)
959 (values list1 list2 n1)
960 (values list2 list1 n2))
961 (if (or (< n-short +list-based-union-limit+)
962 (not (member test (list #'eq #'eql #'equal #'equalp))))
963 (let ((orig short))
964 (do ((elt (car long) (car long)))
965 ((endp long))
966 (if (not (member (apply-key key elt) orig :key key :test test))
967 (steve-splice long short)
968 (setf long (cdr long))))
969 short)
970 (let ((table (make-hash-table :test test :size (+ n1 n2))))
971 (dolist (elt long)
972 (setf (gethash (apply-key key elt) table) elt))
973 (dolist (elt short)
974 (setf (gethash (apply-key key elt) table) elt))
975 (let ((union long)
976 (head long))
977 (maphash (lambda (k v)
978 (declare (ignore k))
979 (if head
980 (setf (car head) v
981 head (cdr head))
982 (push v union)))
983 table)
984 union))))))
986 (defun intersection (list1 list2
987 &key key (test #'eql testp) (test-not nil notp))
988 #!+sb-doc
989 "Return the intersection of LIST1 and LIST2."
990 (declare (inline member))
991 (when (and testp notp)
992 (error ":TEST and :TEST-NOT were both supplied."))
993 (let ((key (and key (%coerce-callable-to-fun key))))
994 (let ((res nil))
995 (dolist (elt list1)
996 (if (with-set-keys (member (apply-key key elt) list2))
997 (push elt res)))
998 res)))
1000 (defun nintersection (list1 list2
1001 &key key (test #'eql testp) (test-not nil notp))
1002 #!+sb-doc
1003 "Destructively return the intersection of LIST1 and LIST2."
1004 (declare (inline member))
1005 (when (and testp notp)
1006 (error ":TEST and :TEST-NOT were both supplied."))
1007 (let ((key (and key (%coerce-callable-to-fun key))))
1008 (let ((res nil)
1009 (list1 list1))
1010 (do () ((endp list1))
1011 (if (with-set-keys (member (apply-key key (car list1)) list2))
1012 (steve-splice list1 res)
1013 (setq list1 (cdr list1))))
1014 res)))
1016 (defun set-difference (list1 list2
1017 &key key (test #'eql testp) (test-not nil notp))
1018 #!+sb-doc
1019 "Return the elements of LIST1 which are not in LIST2."
1020 (declare (inline member))
1021 (when (and testp notp)
1022 (error ":TEST and :TEST-NOT were both supplied."))
1023 (let ((key (and key (%coerce-callable-to-fun key))))
1024 (if (null list2)
1025 list1
1026 (let ((res nil))
1027 (dolist (elt list1)
1028 (if (not (with-set-keys (member (apply-key key elt) list2)))
1029 (push elt res)))
1030 res))))
1032 (defun nset-difference (list1 list2
1033 &key key (test #'eql testp) (test-not nil notp))
1034 #!+sb-doc
1035 "Destructively return the elements of LIST1 which are not in LIST2."
1036 (declare (inline member))
1037 (when (and testp notp)
1038 (error ":TEST and :TEST-NOT were both supplied."))
1039 (let ((key (and key (%coerce-callable-to-fun key))))
1040 (let ((res nil)
1041 (list1 list1))
1042 (do () ((endp list1))
1043 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
1044 (steve-splice list1 res)
1045 (setq list1 (cdr list1))))
1046 res)))
1048 (defun set-exclusive-or (list1 list2
1049 &key key (test #'eql testp) (test-not #'eql notp))
1050 #!+sb-doc
1051 "Return new list of elements appearing exactly once in LIST1 and LIST2."
1052 (declare (inline member))
1053 (when (and testp notp)
1054 (error ":TEST and :TEST-NOT were both supplied."))
1055 (let ((result nil)
1056 (key (and key (%coerce-callable-to-fun key)))
1057 (test (if testp (%coerce-callable-to-fun test) test))
1058 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1059 (declare (type function test test-not))
1060 (dolist (elt list1)
1061 (unless (with-set-keys (member (apply-key key elt) list2))
1062 (setq result (cons elt result))))
1063 (let ((test (if testp
1064 (lambda (x y) (funcall test y x))
1065 test))
1066 (test-not (if notp
1067 (lambda (x y) (funcall test-not y x))
1068 test-not)))
1069 (dolist (elt list2)
1070 (unless (with-set-keys (member (apply-key key elt) list1))
1071 (setq result (cons elt result)))))
1072 result))
1074 (defun nset-exclusive-or (list1 list2
1075 &key key (test #'eql testp) (test-not #'eql notp))
1076 #!+sb-doc
1077 "Destructively return a list with elements which appear but once in LIST1
1078 and LIST2."
1079 (when (and testp notp)
1080 (error ":TEST and :TEST-NOT were both supplied."))
1081 (let ((key (and key (%coerce-callable-to-fun key)))
1082 (test (if testp (%coerce-callable-to-fun test) test))
1083 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1084 (declare (type function test test-not))
1085 ;; The outer loop examines LIST1 while the inner loop examines
1086 ;; LIST2. If an element is found in LIST2 "equal" to the element
1087 ;; in LIST1, both are spliced out. When the end of LIST1 is
1088 ;; reached, what is left of LIST2 is tacked onto what is left of
1089 ;; LIST1. The splicing operation ensures that the correct
1090 ;; operation is performed depending on whether splice is at the
1091 ;; top of the list or not.
1092 (do ((list1 list1)
1093 (list2 list2)
1094 (x list1 (cdr x))
1095 (splicex ())
1096 (deleted-y ())
1097 ;; elements of LIST2, which are "equal" to some processed
1098 ;; earlier elements of LIST1
1100 ((endp x)
1101 (if (null splicex)
1102 (setq list1 list2)
1103 (rplacd splicex list2))
1104 list1)
1105 (let ((key-val-x (apply-key key (car x)))
1106 (found-duplicate nil))
1108 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1109 ;; to DELETED-Y.
1110 (do* ((y list2 next-y)
1111 (next-y (cdr y) (cdr y))
1112 (splicey ()))
1113 ((endp y))
1114 (cond ((let ((key-val-y (apply-key key (car y))))
1115 (if notp
1116 (not (funcall test-not key-val-x key-val-y))
1117 (funcall test key-val-x key-val-y)))
1118 (if (null splicey)
1119 (setq list2 (cdr y))
1120 (rplacd splicey (cdr y)))
1121 (setq deleted-y (rplacd y deleted-y))
1122 (setq found-duplicate t))
1123 (t (setq splicey y))))
1125 (unless found-duplicate
1126 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1128 (if found-duplicate
1129 (if (null splicex)
1130 (setq list1 (cdr x))
1131 (rplacd splicex (cdr x)))
1132 (setq splicex x))))))
1134 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1135 #!+sb-doc
1136 "Return T if every element in LIST1 is also in LIST2."
1137 (declare (inline member))
1138 (when (and testp notp)
1139 (error ":TEST and :TEST-NOT were both supplied."))
1140 (let ((key (and key (%coerce-callable-to-fun key))))
1141 (dolist (elt list1)
1142 (unless (with-set-keys (member (apply-key key elt) list2))
1143 (return-from subsetp nil)))
1146 ;;;; functions that operate on association lists
1148 (defun acons (key datum alist)
1149 #!+sb-doc
1150 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1151 (cons (cons key datum) alist))
1153 (defun pairlis (keys data &optional (alist '()))
1154 #!+sb-doc
1155 "Construct an association list from KEYS and DATA (adding to ALIST)."
1156 (do ((x keys (cdr x))
1157 (y data (cdr y)))
1158 ((and (endp x) (endp y)) alist)
1159 (if (or (endp x) (endp y))
1160 (error "The lists of keys and data are of unequal length."))
1161 (setq alist (acons (car x) (car y) alist))))
1163 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1164 #!+sb-doc
1165 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1166 the ITEM."
1167 (when (and testp notp)
1168 (error ":TEST and :TEST-NOT were both supplied."))
1169 (let ((key (and key (%coerce-callable-to-fun key)))
1170 (test (and testp (%coerce-callable-to-fun test)))
1171 (test-not (and notp (%coerce-callable-to-fun test-not))))
1172 (cond (test
1173 (if key
1174 (%assoc-key-test item alist key test)
1175 (%assoc-test item alist test)))
1176 (test-not
1177 (if key
1178 (%assoc-key-test-not item alist key test-not)
1179 (%assoc-test-not item alist test-not)))
1181 (if key
1182 (%assoc-key item alist key)
1183 (%assoc item alist))))))
1185 (defun assoc-if (predicate alist &key key)
1186 #!+sb-doc
1187 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1188 KEY is supplied, apply it to the CAR of each cons before testing."
1189 (let ((predicate (%coerce-callable-to-fun predicate))
1190 (key (and key (%coerce-callable-to-fun key))))
1191 (if key
1192 (%assoc-if-key predicate alist key)
1193 (%assoc-if predicate alist))))
1195 (defun assoc-if-not (predicate alist &key key)
1196 #!+sb-doc
1197 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1198 If KEY is supplied, apply it to the CAR of each cons before testing."
1199 (let ((predicate (%coerce-callable-to-fun predicate))
1200 (key (and key (%coerce-callable-to-fun key))))
1201 (if key
1202 (%assoc-if-not-key predicate alist key)
1203 (%assoc-if-not predicate alist))))
1205 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1206 (declare (list alist))
1207 #!+sb-doc
1208 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1209 the ITEM."
1210 (when (and testp notp)
1211 (error ":TEST and :TEST-NOT were both supplied."))
1212 (let ((key (and key (%coerce-callable-to-fun key)))
1213 (test (and testp (%coerce-callable-to-fun test)))
1214 (test-not (and notp (%coerce-callable-to-fun test-not))))
1215 (cond (test
1216 (if key
1217 (%rassoc-key-test item alist key test)
1218 (%rassoc-test item alist test)))
1219 (test-not
1220 (if key
1221 (%rassoc-key-test-not item alist key test-not)
1222 (%rassoc-test-not item alist test-not)))
1224 (if key
1225 (%rassoc-key item alist key)
1226 (%rassoc item alist))))))
1228 (defun rassoc-if (predicate alist &key key)
1229 #!+sb-doc
1230 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1231 is supplied, apply it to the CDR of each cons before testing."
1232 (let ((predicate (%coerce-callable-to-fun predicate))
1233 (key (and key (%coerce-callable-to-fun key))))
1234 (if key
1235 (%rassoc-if-key predicate alist key)
1236 (%rassoc-if predicate alist))))
1238 (defun rassoc-if-not (predicate alist &key key)
1239 #!+sb-doc
1240 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1241 If KEY is supplied, apply it to the CDR of each cons before testing."
1242 (let ((predicate (%coerce-callable-to-fun predicate))
1243 (key (and key (%coerce-callable-to-fun key))))
1244 (if key
1245 (%rassoc-if-not-key predicate alist key)
1246 (%rassoc-if-not predicate alist))))
1248 ;;;; mapping functions
1250 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1251 ;;; MAPL, MAPLIST, and MAPCON
1253 ;;; Map the designated function over the arglists in the appropriate
1254 ;;; way. It is done when any of the arglists runs out. Until then, it
1255 ;;; CDRs down the arglists calling the function and accumulating
1256 ;;; results as desired.
1257 (defun map1 (fun-designator arglists accumulate take-car)
1258 (do* ((fun (%coerce-callable-to-fun fun-designator))
1259 (non-acc-result (car arglists))
1260 (ret-list (list nil))
1261 (temp ret-list)
1262 (res nil)
1263 (args (make-list (length arglists))))
1264 ((dolist (x arglists) (or x (return t)))
1265 (if accumulate
1266 (cdr ret-list)
1267 non-acc-result))
1268 (do ((l arglists (cdr l))
1269 (arg args (cdr arg)))
1270 ((null l))
1271 (setf (car arg) (if take-car (caar l) (car l)))
1272 (setf (car l) (cdar l)))
1273 (setq res (apply fun args))
1274 (case accumulate
1275 (:nconc
1276 (when res
1277 (setf (cdr temp) res)
1278 ;; KLUDGE: it is said that MAPCON is equivalent to
1279 ;; (apply #'nconc (maplist ...)) which means (nconc 1) would
1280 ;; return 1, but (nconc 1 1) should signal an error.
1281 ;; The transformed MAP code returns the last result, do that
1282 ;; here as well for consistency and simplicity.
1283 (when (consp res)
1284 (setf temp (last res)))))
1285 (:list (setf (cdr temp) (list res)
1286 temp (cdr temp))))))
1288 (defun mapc (function list &rest more-lists)
1289 #!+sb-doc
1290 "Apply FUNCTION to successive elements of lists. Return the second argument."
1291 (map1 function (cons list more-lists) nil t))
1293 (defun mapcar (function list &rest more-lists)
1294 #!+sb-doc
1295 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1296 return values."
1297 (map1 function (cons list more-lists) :list t))
1299 (defun mapcan (function list &rest more-lists)
1300 #!+sb-doc
1301 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1302 results."
1303 (map1 function (cons list more-lists) :nconc t))
1305 (defun mapl (function list &rest more-lists)
1306 #!+sb-doc
1307 "Apply FUNCTION to successive CDRs of list. Return NIL."
1308 (map1 function (cons list more-lists) nil nil))
1310 (defun maplist (function list &rest more-lists)
1311 #!+sb-doc
1312 "Apply FUNCTION to successive CDRs of list. Return list of results."
1313 (map1 function (cons list more-lists) :list nil))
1315 (defun mapcon (function list &rest more-lists)
1316 #!+sb-doc
1317 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1318 (map1 function (cons list more-lists) :nconc nil))
1320 ;;;; Specialized versions
1322 ;;; %ADJOIN-*, %ASSOC-*, %MEMBER-*, and %RASSOC-* functions. Deftransforms
1323 ;;; delegate to TRANSFORM-LIST-PRED-SEEK and TRANSFORM-LIST-ITEM-SEEK which
1324 ;;; pick the appropriate versions. These win because they have only positional
1325 ;;; arguments, the TEST, TEST-NOT & KEY functions are known to exist (or not),
1326 ;;; and are known to be functions instead of function designators. We are also
1327 ;;; able to transform many common cases to -EQ versions, which are
1328 ;;; substantially faster then EQL using ones.
1329 (macrolet
1330 ((def (funs form &optional variant)
1331 (flet ((%def (name &optional conditional)
1332 (let* ((body-loop
1333 `(do ((list list (cdr list)))
1334 ((null list) nil)
1335 (declare (list list))
1336 (let ((this (car list)))
1337 ,(let ((cxx (if (char= #\A (char (string name) 0))
1338 'car ; assoc, assoc-if, assoc-if-not
1339 'cdr))) ; rassoc, rassoc-if, rassoc-if-not
1340 (ecase name
1341 ((assoc rassoc)
1342 (if funs
1343 `(when this
1344 (let ((target (,cxx this)))
1345 (when ,form
1346 (return this))))
1347 ;; If there is no TEST/TEST-NOT or
1348 ;; KEY, do the EQ/EQL test first,
1349 ;; before checking for NIL.
1350 `(let ((target (,cxx this)))
1351 (when (and ,form this)
1352 (return this)))))
1353 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
1354 (aver (equal '(eql x) (subseq form 0 2)))
1355 `(when this
1356 (let ((target (,cxx this)))
1357 (,conditional (funcall ,@(cdr form))
1358 (return this)))))
1359 (member
1360 `(let ((target this))
1361 (when ,form
1362 (return list))))
1363 ((member-if member-if-not)
1364 (aver (equal '(eql x) (subseq form 0 2)))
1365 `(let ((target this))
1366 (,conditional (funcall ,@(cdr form))
1367 (return list))))
1368 (adjoin
1369 `(let ((target this))
1370 (when ,form
1371 (return t)))))))))
1372 (body (if (eq 'adjoin name)
1373 `(if (let ,(when (member 'key funs)
1374 `((x (funcall key x))))
1375 ,body-loop)
1376 list
1377 (cons x list))
1378 body-loop)))
1379 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1380 (x list ,@funs)
1381 (declare (optimize speed (sb!c::verify-arg-count 0)))
1382 ,@(when funs `((declare (function ,@funs))))
1383 ,@(unless (member name '(member assoc adjoin rassoc)) `((declare (function x))))
1384 (declare (explicit-check))
1385 ,body))))
1386 `(progn
1387 ,(%def 'adjoin)
1388 ,(%def 'assoc)
1389 ,(%def 'member)
1390 ,(%def 'rassoc)
1391 ,@(when (and (not variant) (member funs '(() (key)) :test #'equal))
1392 (list (%def 'member-if 'when)
1393 (%def 'member-if-not 'unless)
1394 (%def 'assoc-if 'when)
1395 (%def 'assoc-if-not 'unless)
1396 (%def 'rassoc-if 'when)
1397 (%def 'rassoc-if-not 'unless)))))))
1398 (def ()
1399 (eql x target))
1400 (def ()
1401 (eq x target)
1403 (def (key)
1404 (eql x (funcall key target)))
1405 (def (key)
1406 (eq x (funcall key target))
1408 (def (key test)
1409 (funcall test x (funcall key target)))
1410 (def (key test-not)
1411 (not (funcall test-not x (funcall key target))))
1412 (def (test)
1413 (funcall test x target))
1414 (def (test-not)
1415 (not (funcall test-not x target))))