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