Don't ad-hoc reimplement DEFCONSTANT-EQX for LAMBDA-LIST-KEYWORDS.
[sbcl.git] / src / code / list.lisp
bloba41cc140fc0efe613b708f3df0a6486448d054af
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 ;; We know the &REST is a proper list.
327 (declare (optimize (sb!c::type-check 0)))
328 (cond ((atom others) arg)
329 ((atom (cdr others)) (cons arg (car others)))
330 (t (do ((x others (cdr x)))
331 ((null (cddr x)) (rplacd x (cadr x))))
332 (cons arg others))))
334 (defun make-list (size &key initial-element)
335 #!+sb-doc
336 "Constructs a list with size elements each set to value"
337 (%make-list size initial-element))
338 ;;; This entry point is to be preferred, irrespective of
339 ;;; whether or not the backend has vops for %MAKE-LIST.
340 (defun %make-list (size initial-element)
341 (declare (type index size))
342 (do ((count size (1- count))
343 (result '() (cons initial-element result)))
344 ((<= count 0) result)
345 (declare (type index count))))
347 (defun append (&rest lists)
348 #!+sb-doc
349 "Construct a new list by concatenating the list arguments"
350 (declare (truly-dynamic-extent lists) (optimize speed))
351 (labels ((fail (object)
352 (error 'type-error
353 :datum object
354 :expected-type 'list))
355 (append-into (last-cons current rest)
356 ;; Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST).
357 (declare (cons last-cons rest))
358 (if (listp current)
359 (if (consp current)
360 ;; normal case, cdr down the list
361 (append-into (setf (cdr last-cons) (list (car current)))
362 (cdr current)
363 rest)
364 ;; empty list
365 (let ((more (cdr rest)))
366 (if (null more)
367 (setf (cdr last-cons) (car rest))
368 (append-into last-cons (car rest) more))))
369 (fail current)))
370 (append1 (lists)
371 (let ((current (car lists))
372 (rest (cdr lists)))
373 (cond ((null rest)
374 current)
375 ((consp current)
376 (let ((result (truly-the cons (list (car current)))))
377 (append-into result
378 (cdr current)
379 rest)
380 result))
381 ((null current)
382 (append1 rest))
384 (fail current))))))
385 (append1 lists)))
387 (defun append2 (x y)
388 (declare (optimize speed (sb!c::verify-arg-count 0)))
389 (if (null x)
391 (let ((result (list (car x))))
392 (do ((more (cdr x) (cdr more))
393 (tail result (cdr tail)))
394 ((null more)
395 (rplacd tail y)
396 result)
397 (rplacd tail (list (car more)))))))
400 ;;;; list copying functions
402 (eval-when (:compile-toplevel :load-toplevel :execute)
403 (sb!xc:defmacro !copy-list-macro (list &key check-proper-list)
404 ;; Unless CHECK-PROPER-LIST is true, the list is copied correctly
405 ;; even if the list is not terminated by NIL. The new list is built
406 ;; by CDR'ing SPLICE which is always at the tail of the new list.
407 `(when ,list
408 (let ((copy (list (car ,list))))
409 (do ((orig (cdr ,list) (cdr orig))
410 (splice copy (cdr (rplacd splice (cons (car orig) nil)))))
411 (,@(if check-proper-list
412 '((endp orig))
413 '((atom orig)
414 (unless (null orig)
415 (rplacd splice orig))))
416 copy))))))
418 (defun copy-list (list)
419 #!+sb-doc
420 "Return a new list which is EQUAL to LIST. LIST may be improper."
421 (!copy-list-macro list))
423 (defun copy-alist (alist)
424 #!+sb-doc
425 "Return a new association list which is EQUAL to ALIST."
426 (if (endp alist)
427 alist
428 (let ((result
429 (cons (if (atom (car alist))
430 (car alist)
431 (cons (caar alist) (cdar alist)))
432 nil)))
433 (do ((x (cdr alist) (cdr x))
434 (splice result
435 (cdr (rplacd splice
436 (cons
437 (if (atom (car x))
438 (car x)
439 (cons (caar x) (cdar x)))
440 nil)))))
441 ((endp x)))
442 result)))
444 (defun copy-tree (object)
445 #!+sb-doc
446 "Recursively copy trees of conses."
447 (if (consp object)
448 (let ((result (list (if (consp (car object))
449 (copy-tree (car object))
450 (car object)))))
451 (loop for last-cons = result then new-cons
452 for cdr = (cdr object) then (cdr cdr)
453 for car = (if (consp cdr)
454 (car cdr)
455 (return (setf (cdr last-cons) cdr)))
456 for new-cons = (list (if (consp car)
457 (copy-tree car)
458 car))
459 do (setf (cdr last-cons) new-cons))
460 result)
461 object))
464 ;;;; more commonly-used list functions
466 (defun revappend (x y)
467 #!+sb-doc
468 "Return (append (reverse x) y)."
469 (do ((top x (cdr top))
470 (result y (cons (car top) result)))
471 ((endp top) result)))
473 ;;; NCONC finds the first non-null list, so it can make splice point
474 ;;; to a cons. After finding the first cons element, it holds it in a
475 ;;; result variable while running down successive elements tacking
476 ;;; them together. While tacking lists together, if we encounter a
477 ;;; null list, we set the previous list's last cdr to nil just in case
478 ;;; it wasn't already nil, and it could have been dotted while the
479 ;;; null list was the last argument to NCONC. The manipulation of
480 ;;; splice (that is starting it out on a first cons, setting LAST of
481 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
482 ;;; and it avoids running down the last argument to NCONC which allows
483 ;;; the last argument to be circular.
484 (defun nconc (&rest lists)
485 #!+sb-doc
486 "Concatenates the lists given as arguments (by changing them)"
487 (declare (optimize speed))
488 (flet ((fail (object)
489 (error 'type-error
490 :datum object
491 :expected-type 'list)))
492 (do-rest-arg ((result index) lists)
493 (typecase result
494 (cons
495 (let ((splice result))
496 (do-rest-arg ((ele index) lists (1+ index))
497 (typecase ele
498 (cons (rplacd (last splice) ele)
499 (setf splice ele))
500 (null (rplacd (last splice) nil))
501 (atom (if (< (1+ index) (length lists))
502 (fail ele)
503 (rplacd (last splice) ele)))))
504 (return result)))
505 (null)
506 (atom
507 (if (< (1+ index) (length lists))
508 (fail result)
509 (return result)))))))
511 (defun nreconc (x y)
512 #!+sb-doc
513 "Return (NCONC (NREVERSE X) Y)."
514 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
515 (2nd x 1st) ;2nd follows first down the list.
516 (3rd y 2nd)) ;3rd follows 2nd down the list.
517 ((atom 2nd) 3rd)
518 (rplacd 2nd 3rd)))
520 (defun butlast (list &optional (n 1))
521 (cond ((zerop n)
522 (copy-list list))
523 ((not (typep n 'index))
524 nil)
526 (let ((head (nthcdr (1- n) list)))
527 (and (consp head) ; there are at least n
528 (collect ((copy)) ; conses; copy!
529 (do ((trail list (cdr trail))
530 (head head (cdr head)))
531 ;; HEAD is n-1 conses ahead of TRAIL;
532 ;; when HEAD is at the last cons, return
533 ;; the data copied so far.
534 ((atom (cdr head))
535 (copy))
536 (copy (car trail)))))))))
538 (defun nbutlast (list &optional (n 1))
539 (cond ((zerop n)
540 list)
541 ((not (typep n 'index))
542 nil)
544 (let ((head (nthcdr (1- n) list)))
545 (and (consp head) ; there are more than n
546 (consp (cdr head)) ; conses.
547 ;; TRAIL trails by n cons to be able to
548 ;; cut the list at the cons just before.
549 (do ((trail list (cdr trail))
550 (head (cdr head) (cdr head)))
551 ((atom (cdr head))
552 (setf (cdr trail) nil)
553 list)))))))
555 (defun ldiff (list object)
556 #!+sb-doc
557 "Return a new list, whose elements are those of LIST that appear before
558 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
559 LIST must be a proper list or a dotted list."
560 (do* ((list list (cdr list))
561 (result (list ()))
562 (splice result))
563 ((atom list)
564 (if (eql list object)
565 (cdr result)
566 (progn (rplacd splice list) (cdr result))))
567 (if (eql list object)
568 (return (cdr result))
569 (setq splice (cdr (rplacd splice (list (car list))))))))
571 ;;;; functions to alter list structure
573 (defun rplaca (cons x)
574 #!+sb-doc
575 "Change the CAR of CONS to X and return the CONS."
576 (rplaca cons x))
578 (defun rplacd (cons x)
579 #!+sb-doc
580 "Change the CDR of CONS to X and return the CONS."
581 (rplacd cons x))
583 ;;; The following are for use by SETF.
585 (defun %rplaca (x val) (rplaca x val) val)
587 (defun %rplacd (x val) (rplacd x val) val)
589 ;;; Set the Nth element of LIST to NEWVAL.
590 (defun %setnth (n list newval)
591 (typecase n
592 (index
593 (do ((count n (1- count))
594 (list list (cdr list)))
595 ((endp list)
596 (error "~S is too large an index for SETF of NTH." n))
597 (declare (type fixnum count))
598 (when (<= count 0)
599 (rplaca list newval)
600 (return newval))))
601 (t (let ((cons (nthcdr n list)))
602 (when (endp cons)
603 (error "~S is too large an index for SETF of NTH." n))
604 (rplaca cons newval)
605 newval))))
607 ;;;; :KEY arg optimization to save funcall of IDENTITY
609 ;;; APPLY-KEY saves us a function call sometimes.
610 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
611 ;;; because it's used in seq.lisp and sort.lisp.
612 (defmacro apply-key (key element)
613 `(if ,key
614 (funcall ,key ,element)
615 ,element))
617 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
619 ;;; Use these with the following &KEY args:
620 (defmacro with-set-keys (funcall)
621 `(if notp
622 ,(append funcall '(:key key :test-not test-not))
623 ,(append funcall '(:key key :test test))))
625 (defmacro satisfies-the-test (item elt)
626 (let ((key-tmp (gensym)))
627 `(let ((,key-tmp (apply-key key ,elt)))
628 (cond (testp (funcall test ,item ,key-tmp))
629 (notp (not (funcall test-not ,item ,key-tmp)))
630 (t (funcall test ,item ,key-tmp))))))
632 ;;;; substitution of expressions
634 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
635 #!+sb-doc
636 "Substitutes new for subtrees matching old."
637 (when (and testp notp)
638 (error ":TEST and :TEST-NOT were both supplied."))
639 (let ((key (and key (%coerce-callable-to-fun key)))
640 (test (if testp (%coerce-callable-to-fun test) test))
641 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
642 (declare (type function test test-not))
643 (labels ((s (subtree)
644 (cond ((satisfies-the-test old subtree) new)
645 ((atom subtree) subtree)
646 (t (let ((car (s (car subtree)))
647 (cdr (s (cdr subtree))))
648 (if (and (eq car (car subtree))
649 (eq cdr (cdr subtree)))
650 subtree
651 (cons car cdr)))))))
652 (s tree))))
654 (defun subst-if (new test tree &key key)
655 #!+sb-doc
656 "Substitutes new for subtrees for which test is true."
657 (let ((test (%coerce-callable-to-fun test))
658 (key (and key (%coerce-callable-to-fun key))))
659 (labels ((s (subtree)
660 (cond ((funcall test (apply-key key subtree)) new)
661 ((atom subtree) subtree)
662 (t (let ((car (s (car subtree)))
663 (cdr (s (cdr subtree))))
664 (if (and (eq car (car subtree))
665 (eq cdr (cdr subtree)))
666 subtree
667 (cons car cdr)))))))
668 (s tree))))
670 (defun subst-if-not (new test tree &key key)
671 #!+sb-doc
672 "Substitutes new for subtrees for which test is false."
673 (let ((test (%coerce-callable-to-fun test))
674 (key (and key (%coerce-callable-to-fun key))))
675 (labels ((s (subtree)
676 (cond ((not (funcall test (apply-key key subtree))) new)
677 ((atom subtree) subtree)
678 (t (let ((car (s (car subtree)))
679 (cdr (s (cdr subtree))))
680 (if (and (eq car (car subtree))
681 (eq cdr (cdr subtree)))
682 subtree
683 (cons car cdr)))))))
684 (s tree))))
686 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
687 #!+sb-doc
688 "Substitute NEW for subtrees matching OLD."
689 (when (and testp notp)
690 (error ":TEST and :TEST-NOT were both supplied."))
691 (let ((key (and key (%coerce-callable-to-fun key)))
692 (test (if testp (%coerce-callable-to-fun test) test))
693 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
694 (declare (type function test test-not))
695 (labels ((s (subtree)
696 (cond ((satisfies-the-test old subtree) new)
697 ((atom subtree) subtree)
698 (t (do* ((last nil subtree)
699 (subtree subtree (cdr subtree)))
700 ((atom subtree)
701 (if (satisfies-the-test old subtree)
702 (setf (cdr last) new)))
703 (if (satisfies-the-test old subtree)
704 (return (setf (cdr last) new))
705 (setf (car subtree) (s (car subtree)))))
706 subtree))))
707 (s tree))))
709 (defun nsubst-if (new test tree &key key)
710 #!+sb-doc
711 "Substitute NEW for subtrees of TREE for which TEST is true."
712 (let ((test (%coerce-callable-to-fun test))
713 (key (and key (%coerce-callable-to-fun key))))
714 (labels ((s (subtree)
715 (cond ((funcall test (apply-key key subtree)) new)
716 ((atom subtree) subtree)
717 (t (do* ((last nil subtree)
718 (subtree subtree (cdr subtree)))
719 ((atom subtree)
720 (if (funcall test (apply-key key subtree))
721 (setf (cdr last) new)))
722 (if (funcall test (apply-key key subtree))
723 (return (setf (cdr last) new))
724 (setf (car subtree) (s (car subtree)))))
725 subtree))))
726 (s tree))))
728 (defun nsubst-if-not (new test tree &key key)
729 #!+sb-doc
730 "Substitute NEW for subtrees of TREE for which TEST is false."
731 (let ((test (%coerce-callable-to-fun test))
732 (key (and key (%coerce-callable-to-fun key))))
733 (labels ((s (subtree)
734 (cond ((not (funcall test (apply-key key subtree))) new)
735 ((atom subtree) subtree)
736 (t (do* ((last nil subtree)
737 (subtree subtree (cdr subtree)))
738 ((atom subtree)
739 (if (not (funcall test (apply-key key subtree)))
740 (setf (cdr last) new)))
741 (if (not (funcall test (apply-key key subtree)))
742 (return (setf (cdr last) new))
743 (setf (car subtree) (s (car subtree)))))
744 subtree))))
745 (s tree))))
747 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
748 #!+sb-doc
749 "Substitute from ALIST into TREE nondestructively."
750 (when (and testp notp)
751 (error ":TEST and :TEST-NOT were both supplied."))
752 (let ((key (and key (%coerce-callable-to-fun key)))
753 (test (if testp (%coerce-callable-to-fun test) test))
754 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
755 (declare (type function test test-not))
756 (declare (inline assoc))
757 (labels ((s (subtree)
758 (let* ((key-val (apply-key key subtree))
759 (assoc (if notp
760 (assoc key-val alist :test-not test-not)
761 (assoc key-val alist :test test))))
762 (cond (assoc (cdr assoc))
763 ((atom subtree) subtree)
764 (t (let ((car (s (car subtree)))
765 (cdr (s (cdr subtree))))
766 (if (and (eq car (car subtree))
767 (eq cdr (cdr subtree)))
768 subtree
769 (cons car cdr))))))))
770 (s tree))))
772 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
773 ;;; because it can be referenced in inline expansions.
774 (defmacro nsublis-macro ()
775 (let ((key-tmp (gensym)))
776 `(let ((,key-tmp (apply-key key subtree)))
777 (if notp
778 (assoc ,key-tmp alist :test-not test-not)
779 (assoc ,key-tmp alist :test test)))))
781 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
782 #!+sb-doc
783 "Substitute from ALIST into TRUE destructively."
784 (when (and testp notp)
785 (error ":TEST and :TEST-NOT were both supplied."))
786 (let ((key (and key (%coerce-callable-to-fun key)))
787 (test (if testp (%coerce-callable-to-fun test) test))
788 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
789 (declare (inline assoc))
790 (let (temp)
791 (labels ((s (subtree)
792 (cond ((setq temp (nsublis-macro))
793 (cdr temp))
794 ((atom subtree) subtree)
795 (t (do* ((last nil subtree)
796 (subtree subtree (cdr subtree)))
797 ((atom subtree)
798 (if (setq temp (nsublis-macro))
799 (setf (cdr last) (cdr temp))))
800 (if (setq temp (nsublis-macro))
801 (return (setf (cdr last) (cdr temp)))
802 (setf (car subtree) (s (car subtree)))))
803 subtree))))
804 (s tree)))))
806 ;;;; functions for using lists as sets
808 (defun member (item list &key key (test nil testp) (test-not nil notp))
809 #!+sb-doc
810 "Return the tail of LIST beginning with first element satisfying EQLity,
811 :TEST, or :TEST-NOT with the given ITEM."
812 (when (and testp notp)
813 (error ":TEST and :TEST-NOT were both supplied."))
814 (let ((key (and key (%coerce-callable-to-fun key)))
815 (test (and testp (%coerce-callable-to-fun test)))
816 (test-not (and notp (%coerce-callable-to-fun test-not))))
817 (cond (test
818 (if key
819 (%member-key-test item list key test)
820 (%member-test item list test)))
821 (test-not
822 (if key
823 (%member-key-test-not item list key test-not)
824 (%member-test-not item list test-not)))
826 (if key
827 (%member-key item list key)
828 (%member item list))))))
830 (defun member-if (test list &key key)
831 #!+sb-doc
832 "Return tail of LIST beginning with first element satisfying TEST."
833 (let ((test (%coerce-callable-to-fun test))
834 (key (and key (%coerce-callable-to-fun key))))
835 (if key
836 (%member-if-key test list key)
837 (%member-if test list))))
839 (defun member-if-not (test list &key key)
840 #!+sb-doc
841 "Return tail of LIST beginning with first element not satisfying TEST."
842 (let ((test (%coerce-callable-to-fun test))
843 (key (and key (%coerce-callable-to-fun key))))
844 (if key
845 (%member-if-not-key test list key)
846 (%member-if-not test list))))
848 (defun tailp (object list)
849 #!+sb-doc
850 "Return true if OBJECT is the same as some tail of LIST, otherwise
851 returns false. LIST must be a proper list or a dotted list."
852 (do ((list list (cdr list)))
853 ((atom list) (eql list object))
854 (if (eql object list)
855 (return t))))
857 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
858 #!+sb-doc
859 "Add ITEM to LIST unless it is already a member"
860 (when (and testp notp)
861 (error ":TEST and :TEST-NOT were both supplied."))
862 (let ((key (and key (%coerce-callable-to-fun key)))
863 (test (and testp (%coerce-callable-to-fun test)))
864 (test-not (and notp (%coerce-callable-to-fun test-not))))
865 (cond (test
866 (if key
867 (%adjoin-key-test item list key test)
868 (%adjoin-test item list test)))
869 (test-not
870 (if key
871 (%adjoin-key-test-not item list key test-not)
872 (%adjoin-test-not item list test-not)))
874 (if key
875 (%adjoin-key item list key)
876 (%adjoin item list))))))
878 (defconstant +list-based-union-limit+ 80)
880 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
881 #!+sb-doc
882 "Return the union of LIST1 and LIST2."
883 (declare (inline member))
884 (when (and testp notp)
885 (error ":TEST and :TEST-NOT were both supplied."))
886 ;; We have two possibilities here: for shortish lists we pick up the
887 ;; shorter one as the result, and add the other one to it. For long
888 ;; lists we use a hash-table when possible.
889 (let ((n1 (length list1))
890 (n2 (length list2))
891 (key (and key (%coerce-callable-to-fun key)))
892 (test (if notp
893 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
894 (lambda (x y) (not (funcall test-not-fun x y))))
895 (%coerce-callable-to-fun test))))
896 (multiple-value-bind (short long n-short)
897 (if (< n1 n2)
898 (values list1 list2 n1)
899 (values list2 list1 n2))
900 (if (or (< n-short +list-based-union-limit+)
901 (not (member test (list #'eq #'eql #'equal #'equalp))))
902 (let ((orig short))
903 (dolist (elt long)
904 (unless (member (apply-key key elt) orig :key key :test test)
905 (push elt short)))
906 short)
907 (let ((table (make-hash-table :test test :size (+ n1 n2)))
908 (union nil))
909 (dolist (elt long)
910 (setf (gethash (apply-key key elt) table) elt))
911 (dolist (elt short)
912 (setf (gethash (apply-key key elt) table) elt))
913 (maphash (lambda (k v)
914 (declare (ignore k))
915 (push v union))
916 table)
917 union)))))
919 ;;; Destination and source are SETF-able and many-evaluable. Set the
920 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
922 ;;; FIXME: needs a more mnemonic name
923 (defmacro steve-splice (source destination)
924 `(let ((temp ,source))
925 (setf ,source (cdr ,source)
926 (cdr temp) ,destination
927 ,destination temp)))
929 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
930 #!+sb-doc
931 "Destructively return the union of LIST1 and LIST2."
932 (declare (inline member))
933 (when (and testp notp)
934 (error ":TEST and :TEST-NOT were both supplied."))
935 ;; We have two possibilities here: for shortish lists we pick up the
936 ;; shorter one as the result, and add the other one to it. For long
937 ;; lists we use a hash-table when possible.
938 (let ((n1 (length list1))
939 (n2 (length list2))
940 (key (and key (%coerce-callable-to-fun key)))
941 (test (if notp
942 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
943 (lambda (x y) (not (funcall test-not-fun x y))))
944 (%coerce-callable-to-fun test))))
945 (multiple-value-bind (short long n-short)
946 (if (< n1 n2)
947 (values list1 list2 n1)
948 (values list2 list1 n2))
949 (if (or (< n-short +list-based-union-limit+)
950 (not (member test (list #'eq #'eql #'equal #'equalp))))
951 (let ((orig short))
952 (do ((elt (car long) (car long)))
953 ((endp long))
954 (if (not (member (apply-key key elt) orig :key key :test test))
955 (steve-splice long short)
956 (setf long (cdr long))))
957 short)
958 (let ((table (make-hash-table :test test :size (+ n1 n2))))
959 (dolist (elt long)
960 (setf (gethash (apply-key key elt) table) elt))
961 (dolist (elt short)
962 (setf (gethash (apply-key key elt) table) elt))
963 (let ((union long)
964 (head long))
965 (maphash (lambda (k v)
966 (declare (ignore k))
967 (if head
968 (setf (car head) v
969 head (cdr head))
970 (push v union)))
971 table)
972 union))))))
974 (defun intersection (list1 list2
975 &key key (test #'eql testp) (test-not nil notp))
976 #!+sb-doc
977 "Return the intersection of LIST1 and LIST2."
978 (declare (inline member))
979 (when (and testp notp)
980 (error ":TEST and :TEST-NOT were both supplied."))
981 (let ((key (and key (%coerce-callable-to-fun key))))
982 (let ((res nil))
983 (dolist (elt list1)
984 (if (with-set-keys (member (apply-key key elt) list2))
985 (push elt res)))
986 res)))
988 (defun nintersection (list1 list2
989 &key key (test #'eql testp) (test-not nil notp))
990 #!+sb-doc
991 "Destructively return the intersection of LIST1 and LIST2."
992 (declare (inline member))
993 (when (and testp notp)
994 (error ":TEST and :TEST-NOT were both supplied."))
995 (let ((key (and key (%coerce-callable-to-fun key))))
996 (let ((res nil)
997 (list1 list1))
998 (do () ((endp list1))
999 (if (with-set-keys (member (apply-key key (car list1)) list2))
1000 (steve-splice list1 res)
1001 (setq list1 (cdr list1))))
1002 res)))
1004 (defun set-difference (list1 list2
1005 &key key (test #'eql testp) (test-not nil notp))
1006 #!+sb-doc
1007 "Return the elements of LIST1 which are not in LIST2."
1008 (declare (inline member))
1009 (when (and testp notp)
1010 (error ":TEST and :TEST-NOT were both supplied."))
1011 (let ((key (and key (%coerce-callable-to-fun key))))
1012 (if (null list2)
1013 list1
1014 (let ((res nil))
1015 (dolist (elt list1)
1016 (if (not (with-set-keys (member (apply-key key elt) list2)))
1017 (push elt res)))
1018 res))))
1020 (defun nset-difference (list1 list2
1021 &key key (test #'eql testp) (test-not nil notp))
1022 #!+sb-doc
1023 "Destructively return the elements of LIST1 which are not in LIST2."
1024 (declare (inline member))
1025 (when (and testp notp)
1026 (error ":TEST and :TEST-NOT were both supplied."))
1027 (let ((key (and key (%coerce-callable-to-fun key))))
1028 (let ((res nil)
1029 (list1 list1))
1030 (do () ((endp list1))
1031 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
1032 (steve-splice list1 res)
1033 (setq list1 (cdr list1))))
1034 res)))
1036 (defun set-exclusive-or (list1 list2
1037 &key key (test #'eql testp) (test-not #'eql notp))
1038 #!+sb-doc
1039 "Return new list of elements appearing exactly once in LIST1 and LIST2."
1040 (declare (inline member))
1041 (when (and testp notp)
1042 (error ":TEST and :TEST-NOT were both supplied."))
1043 (let ((result nil)
1044 (key (and key (%coerce-callable-to-fun key)))
1045 (test (if testp (%coerce-callable-to-fun test) test))
1046 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1047 (declare (type function test test-not))
1048 (dolist (elt list1)
1049 (unless (with-set-keys (member (apply-key key elt) list2))
1050 (setq result (cons elt result))))
1051 (let ((test (if testp
1052 (lambda (x y) (funcall test y x))
1053 test))
1054 (test-not (if notp
1055 (lambda (x y) (funcall test-not y x))
1056 test-not)))
1057 (dolist (elt list2)
1058 (unless (with-set-keys (member (apply-key key elt) list1))
1059 (setq result (cons elt result)))))
1060 result))
1062 (defun nset-exclusive-or (list1 list2
1063 &key key (test #'eql testp) (test-not #'eql notp))
1064 #!+sb-doc
1065 "Destructively return a list with elements which appear but once in LIST1
1066 and LIST2."
1067 (when (and testp notp)
1068 (error ":TEST and :TEST-NOT were both supplied."))
1069 (let ((key (and key (%coerce-callable-to-fun key)))
1070 (test (if testp (%coerce-callable-to-fun test) test))
1071 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1072 (declare (type function test test-not))
1073 ;; The outer loop examines LIST1 while the inner loop examines
1074 ;; LIST2. If an element is found in LIST2 "equal" to the element
1075 ;; in LIST1, both are spliced out. When the end of LIST1 is
1076 ;; reached, what is left of LIST2 is tacked onto what is left of
1077 ;; LIST1. The splicing operation ensures that the correct
1078 ;; operation is performed depending on whether splice is at the
1079 ;; top of the list or not.
1080 (do ((list1 list1)
1081 (list2 list2)
1082 (x list1 (cdr x))
1083 (splicex ())
1084 (deleted-y ())
1085 ;; elements of LIST2, which are "equal" to some processed
1086 ;; earlier elements of LIST1
1088 ((endp x)
1089 (if (null splicex)
1090 (setq list1 list2)
1091 (rplacd splicex list2))
1092 list1)
1093 (let ((key-val-x (apply-key key (car x)))
1094 (found-duplicate nil))
1096 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1097 ;; to DELETED-Y.
1098 (do* ((y list2 next-y)
1099 (next-y (cdr y) (cdr y))
1100 (splicey ()))
1101 ((endp y))
1102 (cond ((let ((key-val-y (apply-key key (car y))))
1103 (if notp
1104 (not (funcall test-not key-val-x key-val-y))
1105 (funcall test key-val-x key-val-y)))
1106 (if (null splicey)
1107 (setq list2 (cdr y))
1108 (rplacd splicey (cdr y)))
1109 (setq deleted-y (rplacd y deleted-y))
1110 (setq found-duplicate t))
1111 (t (setq splicey y))))
1113 (unless found-duplicate
1114 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1116 (if found-duplicate
1117 (if (null splicex)
1118 (setq list1 (cdr x))
1119 (rplacd splicex (cdr x)))
1120 (setq splicex x))))))
1122 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1123 #!+sb-doc
1124 "Return T if every element in LIST1 is also in LIST2."
1125 (declare (inline member))
1126 (when (and testp notp)
1127 (error ":TEST and :TEST-NOT were both supplied."))
1128 (let ((key (and key (%coerce-callable-to-fun key))))
1129 (dolist (elt list1)
1130 (unless (with-set-keys (member (apply-key key elt) list2))
1131 (return-from subsetp nil)))
1134 ;;;; functions that operate on association lists
1136 (defun acons (key datum alist)
1137 #!+sb-doc
1138 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1139 (cons (cons key datum) alist))
1141 (defun pairlis (keys data &optional (alist '()))
1142 #!+sb-doc
1143 "Construct an association list from KEYS and DATA (adding to ALIST)."
1144 (do ((x keys (cdr x))
1145 (y data (cdr y)))
1146 ((and (endp x) (endp y)) alist)
1147 (if (or (endp x) (endp y))
1148 (error "The lists of keys and data are of unequal length."))
1149 (setq alist (acons (car x) (car y) alist))))
1151 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1152 #!+sb-doc
1153 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1154 the ITEM."
1155 (when (and testp notp)
1156 (error ":TEST and :TEST-NOT were both supplied."))
1157 (let ((key (and key (%coerce-callable-to-fun key)))
1158 (test (and testp (%coerce-callable-to-fun test)))
1159 (test-not (and notp (%coerce-callable-to-fun test-not))))
1160 (cond (test
1161 (if key
1162 (%assoc-key-test item alist key test)
1163 (%assoc-test item alist test)))
1164 (test-not
1165 (if key
1166 (%assoc-key-test-not item alist key test-not)
1167 (%assoc-test-not item alist test-not)))
1169 (if key
1170 (%assoc-key item alist key)
1171 (%assoc item alist))))))
1173 (defun assoc-if (predicate alist &key key)
1174 #!+sb-doc
1175 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1176 KEY is supplied, apply it to the CAR of each cons before testing."
1177 (let ((predicate (%coerce-callable-to-fun predicate))
1178 (key (and key (%coerce-callable-to-fun key))))
1179 (if key
1180 (%assoc-if-key predicate alist key)
1181 (%assoc-if predicate alist))))
1183 (defun assoc-if-not (predicate alist &key key)
1184 #!+sb-doc
1185 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1186 If KEY is supplied, apply it to the CAR of each cons before testing."
1187 (let ((predicate (%coerce-callable-to-fun predicate))
1188 (key (and key (%coerce-callable-to-fun key))))
1189 (if key
1190 (%assoc-if-not-key predicate alist key)
1191 (%assoc-if-not predicate alist))))
1193 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1194 (declare (list alist))
1195 #!+sb-doc
1196 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1197 the ITEM."
1198 (when (and testp notp)
1199 (error ":TEST and :TEST-NOT were both supplied."))
1200 (let ((key (and key (%coerce-callable-to-fun key)))
1201 (test (and testp (%coerce-callable-to-fun test)))
1202 (test-not (and notp (%coerce-callable-to-fun test-not))))
1203 (cond (test
1204 (if key
1205 (%rassoc-key-test item alist key test)
1206 (%rassoc-test item alist test)))
1207 (test-not
1208 (if key
1209 (%rassoc-key-test-not item alist key test-not)
1210 (%rassoc-test-not item alist test-not)))
1212 (if key
1213 (%rassoc-key item alist key)
1214 (%rassoc item alist))))))
1216 (defun rassoc-if (predicate alist &key key)
1217 #!+sb-doc
1218 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1219 is supplied, apply it to the CDR of each cons before testing."
1220 (let ((predicate (%coerce-callable-to-fun predicate))
1221 (key (and key (%coerce-callable-to-fun key))))
1222 (if key
1223 (%rassoc-if-key predicate alist key)
1224 (%rassoc-if predicate alist))))
1226 (defun rassoc-if-not (predicate alist &key key)
1227 #!+sb-doc
1228 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1229 If KEY is supplied, apply it to the CDR of each cons before testing."
1230 (let ((predicate (%coerce-callable-to-fun predicate))
1231 (key (and key (%coerce-callable-to-fun key))))
1232 (if key
1233 (%rassoc-if-not-key predicate alist key)
1234 (%rassoc-if-not predicate alist))))
1236 ;;;; mapping functions
1238 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1239 ;;; MAPL, MAPLIST, and MAPCON
1241 ;;; Map the designated function over the arglists in the appropriate
1242 ;;; way. It is done when any of the arglists runs out. Until then, it
1243 ;;; CDRs down the arglists calling the function and accumulating
1244 ;;; results as desired.
1245 (defun map1 (fun-designator arglists accumulate take-car)
1246 (do* ((fun (%coerce-callable-to-fun fun-designator))
1247 (non-acc-result (car arglists))
1248 (ret-list (list nil))
1249 (temp ret-list)
1250 (res nil)
1251 (args (make-list (length arglists))))
1252 ((dolist (x arglists) (or x (return t)))
1253 (if accumulate
1254 (cdr ret-list)
1255 non-acc-result))
1256 (do ((l arglists (cdr l))
1257 (arg args (cdr arg)))
1258 ((null l))
1259 (setf (car arg) (if take-car (caar l) (car l)))
1260 (setf (car l) (cdar l)))
1261 (setq res (apply fun args))
1262 (case accumulate
1263 (:nconc
1264 (when res
1265 (setf (cdr temp) res)
1266 ;; KLUDGE: it is said that MAPCON is equivalent to
1267 ;; (apply #'nconc (maplist ...)) which means (nconc 1) would
1268 ;; return 1, but (nconc 1 1) should signal an error.
1269 ;; The transformed MAP code returns the last result, do that
1270 ;; here as well for consistency and simplicity.
1271 (when (consp res)
1272 (setf temp (last res)))))
1273 (:list (setf (cdr temp) (list res)
1274 temp (cdr temp))))))
1276 (defun mapc (function list &rest more-lists)
1277 #!+sb-doc
1278 "Apply FUNCTION to successive elements of lists. Return the second argument."
1279 (map1 function (cons list more-lists) nil t))
1281 (defun mapcar (function list &rest more-lists)
1282 #!+sb-doc
1283 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1284 return values."
1285 (map1 function (cons list more-lists) :list t))
1287 (defun mapcan (function list &rest more-lists)
1288 #!+sb-doc
1289 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1290 results."
1291 (map1 function (cons list more-lists) :nconc t))
1293 (defun mapl (function list &rest more-lists)
1294 #!+sb-doc
1295 "Apply FUNCTION to successive CDRs of list. Return NIL."
1296 (map1 function (cons list more-lists) nil nil))
1298 (defun maplist (function list &rest more-lists)
1299 #!+sb-doc
1300 "Apply FUNCTION to successive CDRs of list. Return list of results."
1301 (map1 function (cons list more-lists) :list nil))
1303 (defun mapcon (function list &rest more-lists)
1304 #!+sb-doc
1305 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1306 (map1 function (cons list more-lists) :nconc nil))
1308 ;;;; Specialized versions
1310 ;;; %ADJOIN-*, %ASSOC-*, %MEMBER-*, and %RASSOC-* functions. Deftransforms
1311 ;;; delegate to TRANSFORM-LIST-PRED-SEEK and TRANSFORM-LIST-ITEM-SEEK which
1312 ;;; pick the appropriate versions. These win because they have only positional
1313 ;;; arguments, the TEST, TEST-NOT & KEY functions are known to exist (or not),
1314 ;;; and are known to be functions instead of function designators. We are also
1315 ;;; able to transform many common cases to -EQ versions, which are
1316 ;;; substantially faster then EQL using ones.
1317 (macrolet
1318 ((def (funs form &optional variant)
1319 (flet ((%def (name &optional conditional)
1320 (let* ((body-loop
1321 `(do ((list list (cdr list)))
1322 ((null list) nil)
1323 (declare (list list))
1324 (let ((this (car list)))
1325 ,(let ((cxx (if (char= #\A (char (string name) 0))
1326 'car ; assoc, assoc-if, assoc-if-not
1327 'cdr))) ; rassoc, rassoc-if, rassoc-if-not
1328 (ecase name
1329 ((assoc rassoc)
1330 (if funs
1331 `(when this
1332 (let ((target (,cxx this)))
1333 (when ,form
1334 (return this))))
1335 ;; If there is no TEST/TEST-NOT or
1336 ;; KEY, do the EQ/EQL test first,
1337 ;; before checking for NIL.
1338 `(let ((target (,cxx this)))
1339 (when (and ,form this)
1340 (return this)))))
1341 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
1342 (aver (equal '(eql x) (subseq form 0 2)))
1343 `(when this
1344 (let ((target (,cxx this)))
1345 (,conditional (funcall ,@(cdr form))
1346 (return this)))))
1347 (member
1348 `(let ((target this))
1349 (when ,form
1350 (return list))))
1351 ((member-if member-if-not)
1352 (aver (equal '(eql x) (subseq form 0 2)))
1353 `(let ((target this))
1354 (,conditional (funcall ,@(cdr form))
1355 (return list))))
1356 (adjoin
1357 `(let ((target this))
1358 (when ,form
1359 (return t)))))))))
1360 (body (if (eq 'adjoin name)
1361 `(if (let ,(when (member 'key funs)
1362 `((x (funcall key x))))
1363 ,body-loop)
1364 list
1365 (cons x list))
1366 body-loop)))
1367 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1368 (x list ,@funs)
1369 (declare (optimize speed (sb!c::verify-arg-count 0)))
1370 ,@(when funs `((declare (function ,@funs))))
1371 ,@(unless (member name '(member assoc adjoin rassoc)) `((declare (function x))))
1372 (declare (explicit-check))
1373 ,body))))
1374 `(progn
1375 ,(%def 'adjoin)
1376 ,(%def 'assoc)
1377 ,(%def 'member)
1378 ,(%def 'rassoc)
1379 ,@(when (and (not variant) (member funs '(() (key)) :test #'equal))
1380 (list (%def 'member-if 'when)
1381 (%def 'member-if-not 'unless)
1382 (%def 'assoc-if 'when)
1383 (%def 'assoc-if-not 'unless)
1384 (%def 'rassoc-if 'when)
1385 (%def 'rassoc-if-not 'unless)))))))
1386 (def ()
1387 (eql x target))
1388 (def ()
1389 (eq x target)
1391 (def (key)
1392 (eql x (funcall key target)))
1393 (def (key)
1394 (eq x (funcall key target))
1396 (def (key test)
1397 (funcall test x (funcall key target)))
1398 (def (key test-not)
1399 (not (funcall test-not x (funcall key target))))
1400 (def (test)
1401 (funcall test x target))
1402 (def (test-not)
1403 (not (funcall test-not x target))))