1.0.19.7: refactor stack allocation decisions
[sbcl/pkhuong.git] / src / code / list.lisp
blob46cbb32c76d52554e7942853966f3708b6c9e2c7
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 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 ;; We know the &REST is a proper list.
325 (declare (optimize (sb!c::type-check 0)))
326 (cond ((atom others) arg)
327 ((atom (cdr others)) (cons arg (car others)))
328 (t (do ((x others (cdr x)))
329 ((null (cddr x)) (rplacd x (cadr x))))
330 (cons arg others))))
332 (defun make-list (size &key initial-element)
333 #!+sb-doc
334 "Constructs a list with size elements each set to value"
335 (declare (type index size))
336 (do ((count size (1- count))
337 (result '() (cons initial-element result)))
338 ((<= count 0) result)
339 (declare (type index count))))
341 (defun append (&rest lists)
342 #!+sb-doc
343 "Construct a new list by concatenating the list arguments"
344 (declare (truly-dynamic-extent lists) (optimize speed))
345 (labels ((fail (object)
346 (error 'type-error
347 :datum object
348 :expected-type 'list))
349 (append-into (last-cons current rest)
350 ;; Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST).
351 (declare (cons last-cons rest))
352 (if (listp current)
353 (if (consp current)
354 ;; normal case, cdr down the list
355 (append-into (setf (cdr last-cons) (list (car current)))
356 (cdr current)
357 rest)
358 ;; empty list
359 (let ((more (cdr rest)))
360 (if (null more)
361 (setf (cdr last-cons) (car rest))
362 (append-into last-cons (car rest) more))))
363 (fail current)))
364 (append1 (lists)
365 (let ((current (car lists))
366 (rest (cdr lists)))
367 (cond ((null rest)
368 current)
369 ((consp current)
370 (let ((result (truly-the cons (list (car current)))))
371 (append-into result
372 (cdr current)
373 rest)
374 result))
375 ((null current)
376 (append1 rest))
378 (fail current))))))
379 (append1 lists)))
381 (defun append2 (x y)
382 (declare (optimize speed (sb!c::verify-arg-count 0)))
383 (if (null x)
385 (let ((result (list (car x))))
386 (do ((more (cdr x) (cdr more))
387 (tail result (cdr tail)))
388 ((null more)
389 (rplacd tail y)
390 result)
391 (rplacd tail (list (car more)))))))
393 (define-compiler-macro append (&whole form &rest lists)
394 (case (length lists)
395 (0 nil)
396 (1 (car lists))
397 (2 `(append2 ,@lists))
398 (t form)))
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 (cons (copy-tree (car object)) (copy-tree (cdr object)))
449 object))
451 ;;;; more commonly-used list functions
453 (defun revappend (x y)
454 #!+sb-doc
455 "Return (append (reverse x) y)."
456 (do ((top x (cdr top))
457 (result y (cons (car top) result)))
458 ((endp top) result)))
460 ;;; NCONC finds the first non-null list, so it can make splice point
461 ;;; to a cons. After finding the first cons element, it holds it in a
462 ;;; result variable while running down successive elements tacking
463 ;;; them together. While tacking lists together, if we encounter a
464 ;;; null list, we set the previous list's last cdr to nil just in case
465 ;;; it wasn't already nil, and it could have been dotted while the
466 ;;; null list was the last argument to NCONC. The manipulation of
467 ;;; splice (that is starting it out on a first cons, setting LAST of
468 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
469 ;;; and it avoids running down the last argument to NCONC which allows
470 ;;; the last argument to be circular.
471 (defun nconc (&rest lists)
472 #!+sb-doc
473 "Concatenates the lists given as arguments (by changing them)"
474 (declare (truly-dynamic-extent lists) (optimize speed))
475 (flet ((fail (object)
476 (error 'type-error
477 :datum object
478 :expected-type 'list)))
479 (do ((top lists (cdr top)))
480 ((null top) nil)
481 (let ((top-of-top (car top)))
482 (typecase top-of-top
483 (cons
484 (let* ((result top-of-top)
485 (splice result))
486 (do ((elements (cdr top) (cdr elements)))
487 ((endp elements))
488 (let ((ele (car elements)))
489 (typecase ele
490 (cons (rplacd (last splice) ele)
491 (setf splice ele))
492 (null (rplacd (last splice) nil))
493 (atom (if (cdr elements)
494 (fail ele)
495 (rplacd (last splice) ele))))))
496 (return result)))
497 (null)
498 (atom
499 (if (cdr top)
500 (fail top-of-top)
501 (return top-of-top))))))))
503 (defun nreconc (x y)
504 #!+sb-doc
505 "Return (NCONC (NREVERSE X) Y)."
506 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
507 (2nd x 1st) ;2nd follows first down the list.
508 (3rd y 2nd)) ;3rd follows 2nd down the list.
509 ((atom 2nd) 3rd)
510 (rplacd 2nd 3rd)))
512 (flet (;; Return the number of conses at the head of the
513 ;; possibly-improper list LIST. (Or if LIST is circular, you
514 ;; lose.)
515 (count-conses (list)
516 (do ((in-list list (cdr in-list))
517 (result 0 (1+ result)))
518 ((atom in-list)
519 result)
520 (declare (type index result)))))
521 (declare (ftype (function (t) index) count-conses))
522 (defun butlast (list &optional (n 1))
523 (if (typep n 'index)
524 (let ((n-conses-in-list (count-conses list)))
525 (cond ((zerop n)
526 ;; (We can't use SUBSEQ in this case because LIST isn't
527 ;; necessarily a proper list, but SUBSEQ expects a
528 ;; proper sequence. COPY-LIST isn't so fussy.)
529 (copy-list list))
530 ((>= n n-conses-in-list)
531 nil)
533 ;; (LIST isn't necessarily a proper list in this case
534 ;; either, and technically SUBSEQ wants a proper
535 ;; sequence, but no reasonable implementation of SUBSEQ
536 ;; will actually walk down to the end of the list to
537 ;; check, and since we're calling our own implementation
538 ;; we know it's reasonable, so it's OK.)
539 (subseq list 0 (- n-conses-in-list n)))))
540 nil))
541 (defun nbutlast (list &optional (n 1))
542 (cond ((zerop n)
543 list)
544 ((not (typep n 'index))
545 nil)
546 (t (let ((n-conses-in-list (count-conses list)))
547 (unless (<= n-conses-in-list n)
548 (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
549 nil)
550 list))))))
552 (defun ldiff (list object)
553 "Return a new list, whose elements are those of LIST that appear before
554 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
555 LIST must be a proper list or a dotted list."
556 (do* ((list list (cdr list))
557 (result (list ()))
558 (splice result))
559 ((atom list)
560 (if (eql list object)
561 (cdr result)
562 (progn (rplacd splice list) (cdr result))))
563 (if (eql list object)
564 (return (cdr result))
565 (setq splice (cdr (rplacd splice (list (car list))))))))
567 ;;;; functions to alter list structure
569 (defun rplaca (cons x)
570 #!+sb-doc
571 "Change the CAR of CONS to X and return the CONS."
572 (rplaca cons x))
574 (defun rplacd (cons x)
575 #!+sb-doc
576 "Change the CDR of CONS to X and return the CONS."
577 (rplacd cons x))
579 ;;; The following are for use by SETF.
581 (defun %rplaca (x val) (rplaca x val) val)
583 (defun %rplacd (x val) (rplacd x val) val)
585 ;;; Set the Nth element of LIST to NEWVAL.
586 (defun %setnth (n list newval)
587 (typecase n
588 (index
589 (do ((count n (1- count))
590 (list list (cdr list)))
591 ((endp list)
592 (error "~S is too large an index for SETF of NTH." n))
593 (declare (type fixnum count))
594 (when (<= count 0)
595 (rplaca list newval)
596 (return newval))))
597 (t (let ((cons (nthcdr n list)))
598 (when (endp cons)
599 (error "~S is too large an index for SETF of NTH." n))
600 (rplaca cons newval)
601 newval))))
603 ;;;; :KEY arg optimization to save funcall of IDENTITY
605 ;;; APPLY-KEY saves us a function call sometimes.
606 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
607 ;;; because it's used in seq.lisp and sort.lisp.
608 (defmacro apply-key (key element)
609 `(if ,key
610 (funcall ,key ,element)
611 ,element))
613 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
615 ;;; Use these with the following &KEY args:
616 (defmacro with-set-keys (funcall)
617 `(if notp
618 ,(append funcall '(:key key :test-not test-not))
619 ,(append funcall '(:key key :test test))))
621 (defmacro satisfies-the-test (item elt)
622 (let ((key-tmp (gensym)))
623 `(let ((,key-tmp (apply-key key ,elt)))
624 (cond (testp (funcall test ,item ,key-tmp))
625 (notp (not (funcall test-not ,item ,key-tmp)))
626 (t (funcall test ,item ,key-tmp))))))
628 ;;;; substitution of expressions
630 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
631 #!+sb-doc
632 "Substitutes new for subtrees matching old."
633 (when (and testp notp)
634 (error ":TEST and :TEST-NOT were both supplied."))
635 (let ((key (and key (%coerce-callable-to-fun key)))
636 (test (if testp (%coerce-callable-to-fun test) test))
637 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
638 (declare (type function test test-not))
639 (labels ((s (subtree)
640 (cond ((satisfies-the-test old subtree) new)
641 ((atom subtree) subtree)
642 (t (let ((car (s (car subtree)))
643 (cdr (s (cdr subtree))))
644 (if (and (eq car (car subtree))
645 (eq cdr (cdr subtree)))
646 subtree
647 (cons car cdr)))))))
648 (s tree))))
650 (defun subst-if (new test tree &key key)
651 #!+sb-doc
652 "Substitutes new for subtrees for which test is true."
653 (let ((test (%coerce-callable-to-fun test))
654 (key (and key (%coerce-callable-to-fun key))))
655 (labels ((s (subtree)
656 (cond ((funcall test (apply-key key 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-not (new test tree &key key)
667 #!+sb-doc
668 "Substitutes new for subtrees for which test is false."
669 (let ((test (%coerce-callable-to-fun test))
670 (key (and key (%coerce-callable-to-fun key))))
671 (labels ((s (subtree)
672 (cond ((not (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 nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
683 #!+sb-doc
684 "Substitute NEW for subtrees matching OLD."
685 (when (and testp notp)
686 (error ":TEST and :TEST-NOT were both supplied."))
687 (let ((key (and key (%coerce-callable-to-fun key)))
688 (test (if testp (%coerce-callable-to-fun test) test))
689 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
690 (declare (type function test test-not))
691 (labels ((s (subtree)
692 (cond ((satisfies-the-test old subtree) new)
693 ((atom subtree) subtree)
694 (t (do* ((last nil subtree)
695 (subtree subtree (cdr subtree)))
696 ((atom subtree)
697 (if (satisfies-the-test old subtree)
698 (setf (cdr last) new)))
699 (if (satisfies-the-test old subtree)
700 (return (setf (cdr last) new))
701 (setf (car subtree) (s (car subtree)))))
702 subtree))))
703 (s tree))))
705 (defun nsubst-if (new test tree &key key)
706 #!+sb-doc
707 "Substitute NEW for subtrees of TREE for which TEST is true."
708 (let ((test (%coerce-callable-to-fun test))
709 (key (and key (%coerce-callable-to-fun key))))
710 (labels ((s (subtree)
711 (cond ((funcall test (apply-key key subtree)) new)
712 ((atom subtree) subtree)
713 (t (do* ((last nil subtree)
714 (subtree subtree (cdr subtree)))
715 ((atom subtree)
716 (if (funcall test (apply-key key subtree))
717 (setf (cdr last) new)))
718 (if (funcall test (apply-key key subtree))
719 (return (setf (cdr last) new))
720 (setf (car subtree) (s (car subtree)))))
721 subtree))))
722 (s tree))))
724 (defun nsubst-if-not (new test tree &key key)
725 #!+sb-doc
726 "Substitute NEW for subtrees of TREE for which TEST is false."
727 (let ((test (%coerce-callable-to-fun test))
728 (key (and key (%coerce-callable-to-fun key))))
729 (labels ((s (subtree)
730 (cond ((not (funcall test (apply-key key subtree))) new)
731 ((atom subtree) subtree)
732 (t (do* ((last nil subtree)
733 (subtree subtree (cdr subtree)))
734 ((atom subtree)
735 (if (not (funcall test (apply-key key subtree)))
736 (setf (cdr last) new)))
737 (if (not (funcall test (apply-key key subtree)))
738 (return (setf (cdr last) new))
739 (setf (car subtree) (s (car subtree)))))
740 subtree))))
741 (s tree))))
743 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
744 #!+sb-doc
745 "Substitute from ALIST into TREE nondestructively."
746 (when (and testp notp)
747 (error ":TEST and :TEST-NOT were both supplied."))
748 (let ((key (and key (%coerce-callable-to-fun key)))
749 (test (if testp (%coerce-callable-to-fun test) test))
750 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
751 (declare (type function test test-not))
752 (declare (inline assoc))
753 (labels ((s (subtree)
754 (let* ((key-val (apply-key key subtree))
755 (assoc (if notp
756 (assoc key-val alist :test-not test-not)
757 (assoc key-val alist :test test))))
758 (cond (assoc (cdr assoc))
759 ((atom subtree) subtree)
760 (t (let ((car (s (car subtree)))
761 (cdr (s (cdr subtree))))
762 (if (and (eq car (car subtree))
763 (eq cdr (cdr subtree)))
764 subtree
765 (cons car cdr))))))))
766 (s tree))))
768 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
769 ;;; because it can be referenced in inline expansions.
770 (defmacro nsublis-macro ()
771 (let ((key-tmp (gensym)))
772 `(let ((,key-tmp (apply-key key subtree)))
773 (if notp
774 (assoc ,key-tmp alist :test-not test-not)
775 (assoc ,key-tmp alist :test test)))))
777 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
778 #!+sb-doc
779 "Substitute from ALIST into TRUE destructively."
780 (when (and testp notp)
781 (error ":TEST and :TEST-NOT were both supplied."))
782 (let ((key (and key (%coerce-callable-to-fun key)))
783 (test (if testp (%coerce-callable-to-fun test) test))
784 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
785 (declare (inline assoc))
786 (let (temp)
787 (labels ((s (subtree)
788 (cond ((setq temp (nsublis-macro))
789 (cdr temp))
790 ((atom subtree) subtree)
791 (t (do* ((last nil subtree)
792 (subtree subtree (cdr subtree)))
793 ((atom subtree)
794 (if (setq temp (nsublis-macro))
795 (setf (cdr last) (cdr temp))))
796 (if (setq temp (nsublis-macro))
797 (return (setf (cdr last) (cdr temp)))
798 (setf (car subtree) (s (car subtree)))))
799 subtree))))
800 (s tree)))))
802 ;;;; functions for using lists as sets
804 (defun member (item list &key key (test #'eql testp) (test-not #'eql notp))
805 #!+sb-doc
806 "Return the tail of LIST beginning with first element satisfying EQLity,
807 :TEST, or :TEST-NOT with the given ITEM."
808 (when (and testp notp)
809 (error ":TEST and :TEST-NOT were both supplied."))
810 (let ((key (and key (%coerce-callable-to-fun key)))
811 (test (if testp (%coerce-callable-to-fun test) test))
812 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
813 (declare (type function test test-not))
814 (do ((list list (cdr list)))
815 ((null list) nil)
816 (let ((car (car list)))
817 (when (satisfies-the-test item car)
818 (return list))))))
820 (defun member-if (test list &key key)
821 #!+sb-doc
822 "Return tail of LIST beginning with first element satisfying TEST."
823 (let ((test (%coerce-callable-to-fun test))
824 (key (and key (%coerce-callable-to-fun key))))
825 (do ((list list (cdr list)))
826 ((endp list) nil)
827 (if (funcall test (apply-key key (car list)))
828 (return list)))))
830 (defun member-if-not (test list &key key)
831 #!+sb-doc
832 "Return tail of LIST beginning with first element not satisfying TEST."
833 (let ((test (%coerce-callable-to-fun test))
834 (key (and key (%coerce-callable-to-fun key))))
835 (do ((list list (cdr list)))
836 ((endp list) ())
837 (if (not (funcall test (apply-key key (car list))))
838 (return list)))))
840 (defun tailp (object list)
841 #!+sb-doc
842 "Return true if OBJECT is the same as some tail of LIST, otherwise
843 returns false. LIST must be a proper list or a dotted list."
844 (do ((list list (cdr list)))
845 ((atom list) (eql list object))
846 (if (eql object list)
847 (return t))))
849 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
850 #!+sb-doc
851 "Add ITEM to LIST unless it is already a member"
852 (when (and testp notp)
853 (error ":TEST and :TEST-NOT were both supplied."))
854 (let ((key (and key (%coerce-callable-to-fun key))))
855 (if (let ((key-val (apply-key key item)))
856 (if notp
857 (member key-val list :test-not test-not :key key)
858 (member key-val list :test test :key key)))
859 list
860 (cons item list))))
862 (defconstant +list-based-union-limit+ 80)
864 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
865 #!+sb-doc
866 "Return the union of LIST1 and LIST2."
867 (declare (inline member))
868 (when (and testp notp)
869 (error ":TEST and :TEST-NOT were both supplied."))
870 ;; We have to possibilities here: for shortish lists we pick up the
871 ;; shorter one as the result, and add the other one to it. For long
872 ;; lists we use a hash-table when possible.
873 (let ((n1 (length list1))
874 (n2 (length list2))
875 (key (and key (%coerce-callable-to-fun key)))
876 (test (if notp
877 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
878 (lambda (x y) (not (funcall test-not-fun x y))))
879 (%coerce-callable-to-fun test))))
880 (multiple-value-bind (short long n-short)
881 (if (< n1 n2)
882 (values list1 list2 n1)
883 (values list2 list1 n2))
884 (if (or (< n-short +list-based-union-limit+)
885 (not (member test (list #'eq #'eql #'equal #'equalp))))
886 (let ((orig short))
887 (dolist (elt long)
888 (unless (member (apply-key key elt) orig :key key :test test)
889 (push elt short)))
890 short)
891 (let ((table (make-hash-table :test test :size (+ n1 n2)))
892 (union nil))
893 (dolist (elt long)
894 (setf (gethash (apply-key key elt) table) elt))
895 (dolist (elt short)
896 (setf (gethash (apply-key key elt) table) elt))
897 (maphash (lambda (k v)
898 (declare (ignore k))
899 (push v union))
900 table)
901 union)))))
903 ;;; Destination and source are SETF-able and many-evaluable. Set the
904 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
906 ;;; FIXME: needs a more mnemonic name
907 (defmacro steve-splice (source destination)
908 `(let ((temp ,source))
909 (setf ,source (cdr ,source)
910 (cdr temp) ,destination
911 ,destination temp)))
913 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
914 #!+sb-doc
915 "Destructively return the union of LIST1 and LIST2."
916 (declare (inline member))
917 (when (and testp notp)
918 (error ":TEST and :TEST-NOT were both supplied."))
919 ;; We have to possibilities here: for shortish lists we pick up the
920 ;; shorter one as the result, and add the other one to it. For long
921 ;; lists we use a hash-table when possible.
922 (let ((n1 (length list1))
923 (n2 (length list2))
924 (key (and key (%coerce-callable-to-fun key)))
925 (test (if notp
926 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
927 (lambda (x y) (not (funcall test-not-fun x y))))
928 (%coerce-callable-to-fun test))))
929 (multiple-value-bind (short long n-short)
930 (if (< n1 n2)
931 (values list1 list2 n1)
932 (values list2 list1 n2))
933 (if (or (< n-short +list-based-union-limit+)
934 (not (member test (list #'eq #'eql #'equal #'equalp))))
935 (let ((orig short))
936 (do ((elt (car long) (car long)))
937 ((endp long))
938 (if (not (member (apply-key key elt) orig :key key :test test))
939 (steve-splice long short)
940 (setf long (cdr long))))
941 short)
942 (let ((table (make-hash-table :test test :size (+ n1 n2))))
943 (dolist (elt long)
944 (setf (gethash (apply-key key elt) table) elt))
945 (dolist (elt short)
946 (setf (gethash (apply-key key elt) table) elt))
947 (let ((union long)
948 (head long))
949 (maphash (lambda (k v)
950 (declare (ignore k))
951 (if head
952 (setf (car head) v
953 head (cdr head))
954 (push v union)))
955 table)
956 union))))))
958 (defun intersection (list1 list2
959 &key key (test #'eql testp) (test-not nil notp))
960 #!+sb-doc
961 "Return the intersection of LIST1 and LIST2."
962 (declare (inline member))
963 (when (and testp notp)
964 (error ":TEST and :TEST-NOT were both supplied."))
965 (let ((key (and key (%coerce-callable-to-fun key))))
966 (let ((res nil))
967 (dolist (elt list1)
968 (if (with-set-keys (member (apply-key key elt) list2))
969 (push elt res)))
970 res)))
972 (defun nintersection (list1 list2
973 &key key (test #'eql testp) (test-not nil notp))
974 #!+sb-doc
975 "Destructively return the intersection of LIST1 and LIST2."
976 (declare (inline member))
977 (when (and testp notp)
978 (error ":TEST and :TEST-NOT were both supplied."))
979 (let ((key (and key (%coerce-callable-to-fun key))))
980 (let ((res nil)
981 (list1 list1))
982 (do () ((endp list1))
983 (if (with-set-keys (member (apply-key key (car list1)) list2))
984 (steve-splice list1 res)
985 (setq list1 (cdr list1))))
986 res)))
988 (defun set-difference (list1 list2
989 &key key (test #'eql testp) (test-not nil notp))
990 #!+sb-doc
991 "Return the elements of LIST1 which are not in 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 (if (null list2)
997 list1
998 (let ((res nil))
999 (dolist (elt list1)
1000 (if (not (with-set-keys (member (apply-key key elt) list2)))
1001 (push elt res)))
1002 res))))
1004 (defun nset-difference (list1 list2
1005 &key key (test #'eql testp) (test-not nil notp))
1006 #!+sb-doc
1007 "Destructively 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 (let ((res nil)
1013 (list1 list1))
1014 (do () ((endp list1))
1015 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
1016 (steve-splice list1 res)
1017 (setq list1 (cdr list1))))
1018 res)))
1020 (defun set-exclusive-or (list1 list2
1021 &key key (test #'eql testp) (test-not #'eql notp))
1022 #!+sb-doc
1023 "Return new list of elements appearing exactly once in LIST1 and LIST2."
1024 (declare (inline member))
1025 (when (and testp notp)
1026 (error ":TEST and :TEST-NOT were both supplied."))
1027 (let ((result nil)
1028 (key (and key (%coerce-callable-to-fun key)))
1029 (test (if testp (%coerce-callable-to-fun test) test))
1030 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1031 (declare (type function test test-not))
1032 (dolist (elt list1)
1033 (unless (with-set-keys (member (apply-key key elt) list2))
1034 (setq result (cons elt result))))
1035 (let ((test (if testp
1036 (lambda (x y) (funcall test y x))
1037 test))
1038 (test-not (if notp
1039 (lambda (x y) (funcall test-not y x))
1040 test-not)))
1041 (dolist (elt list2)
1042 (unless (with-set-keys (member (apply-key key elt) list1))
1043 (setq result (cons elt result)))))
1044 result))
1046 (defun nset-exclusive-or (list1 list2
1047 &key key (test #'eql testp) (test-not #'eql notp))
1048 #!+sb-doc
1049 "Destructively return a list with elements which appear but once in LIST1
1050 and LIST2."
1051 (when (and testp notp)
1052 (error ":TEST and :TEST-NOT were both supplied."))
1053 (let ((key (and key (%coerce-callable-to-fun key)))
1054 (test (if testp (%coerce-callable-to-fun test) test))
1055 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1056 (declare (type function test test-not))
1057 ;; The outer loop examines LIST1 while the inner loop examines
1058 ;; LIST2. If an element is found in LIST2 "equal" to the element
1059 ;; in LIST1, both are spliced out. When the end of LIST1 is
1060 ;; reached, what is left of LIST2 is tacked onto what is left of
1061 ;; LIST1. The splicing operation ensures that the correct
1062 ;; operation is performed depending on whether splice is at the
1063 ;; top of the list or not.
1064 (do ((list1 list1)
1065 (list2 list2)
1066 (x list1 (cdr x))
1067 (splicex ())
1068 (deleted-y ())
1069 ;; elements of LIST2, which are "equal" to some processed
1070 ;; earlier elements of LIST1
1072 ((endp x)
1073 (if (null splicex)
1074 (setq list1 list2)
1075 (rplacd splicex list2))
1076 list1)
1077 (let ((key-val-x (apply-key key (car x)))
1078 (found-duplicate nil))
1080 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1081 ;; to DELETED-Y.
1082 (do* ((y list2 next-y)
1083 (next-y (cdr y) (cdr y))
1084 (splicey ()))
1085 ((endp y))
1086 (cond ((let ((key-val-y (apply-key key (car y))))
1087 (if notp
1088 (not (funcall test-not key-val-x key-val-y))
1089 (funcall test key-val-x key-val-y)))
1090 (if (null splicey)
1091 (setq list2 (cdr y))
1092 (rplacd splicey (cdr y)))
1093 (setq deleted-y (rplacd y deleted-y))
1094 (setq found-duplicate t))
1095 (t (setq splicey y))))
1097 (unless found-duplicate
1098 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1100 (if found-duplicate
1101 (if (null splicex)
1102 (setq list1 (cdr x))
1103 (rplacd splicex (cdr x)))
1104 (setq splicex x))))))
1106 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1107 #!+sb-doc
1108 "Return T if every element in LIST1 is also in LIST2."
1109 (declare (inline member))
1110 (when (and testp notp)
1111 (error ":TEST and :TEST-NOT were both supplied."))
1112 (let ((key (and key (%coerce-callable-to-fun key))))
1113 (dolist (elt list1)
1114 (unless (with-set-keys (member (apply-key key elt) list2))
1115 (return-from subsetp nil)))
1118 ;;;; functions that operate on association lists
1120 (defun acons (key datum alist)
1121 #!+sb-doc
1122 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1123 (cons (cons key datum) alist))
1125 (defun pairlis (keys data &optional (alist '()))
1126 #!+sb-doc
1127 "Construct an association list from KEYS and DATA (adding to ALIST)."
1128 (do ((x keys (cdr x))
1129 (y data (cdr y)))
1130 ((and (endp x) (endp y)) alist)
1131 (if (or (endp x) (endp y))
1132 (error "The lists of keys and data are of unequal length."))
1133 (setq alist (acons (car x) (car y) alist))))
1135 ;;; This is defined in the run-time environment, not just the compile-time
1136 ;;; environment (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL)) because it
1137 ;;; can appear in inline expansions.
1138 (defmacro assoc-guts (test-expr)
1139 `(do ((alist alist (cdr alist)))
1140 ((endp alist))
1141 (when (and (car alist) ,test-expr)
1142 (return (car alist)))))
1144 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1145 #!+sb-doc
1146 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1147 the ITEM."
1148 (when (and testp notp)
1149 (error ":TEST and :TEST-NOT were both supplied."))
1150 (let ((key (and key (%coerce-callable-to-fun key)))
1151 (test (and testp (%coerce-callable-to-fun test)))
1152 (test-not (and notp (%coerce-callable-to-fun test-not))))
1153 (cond (test
1154 (if key
1155 (assoc-guts (funcall test item (funcall key (caar alist))))
1156 (assoc-guts (funcall test item (caar alist)))))
1157 (test-not
1158 (if key
1159 (assoc-guts (not (funcall test-not item
1160 (funcall key (caar alist)))))
1161 (assoc-guts (not (funcall test-not item (caar alist))))))
1163 (if key
1164 (assoc-guts (eql item (funcall key (caar alist))))
1165 (assoc-guts (eql item (caar alist))))))))
1167 (defun assoc-if (predicate alist &key key)
1168 #!+sb-doc
1169 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1170 KEY is supplied, apply it to the CAR of each cons before testing."
1171 (let ((predicate (%coerce-callable-to-fun predicate))
1172 (key (and key (%coerce-callable-to-fun key))))
1173 (if key
1174 (assoc-guts (funcall predicate (funcall key (caar alist))))
1175 (assoc-guts (funcall predicate (caar alist))))))
1177 (defun assoc-if-not (predicate alist &key key)
1178 #!+sb-doc
1179 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1180 If KEY is supplied, apply it to the CAR of each cons before testing."
1181 (let ((predicate (%coerce-callable-to-fun predicate))
1182 (key (and key (%coerce-callable-to-fun key))))
1183 (if key
1184 (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
1185 (assoc-guts (not (funcall predicate (caar alist)))))))
1187 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1188 (declare (list alist))
1189 #!+sb-doc
1190 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1191 the ITEM."
1192 (when (and testp notp)
1193 (error ":TEST and :TEST-NOT were both supplied."))
1194 (let ((key (and key (%coerce-callable-to-fun key)))
1195 (test (and testp (%coerce-callable-to-fun test)))
1196 (test-not (and notp (%coerce-callable-to-fun test-not))))
1197 (cond (test
1198 (if key
1199 (assoc-guts (funcall test item (funcall key (cdar alist))))
1200 (assoc-guts (funcall test item (cdar alist)))))
1201 (test-not
1202 (if key
1203 (assoc-guts (not (funcall test-not item
1204 (funcall key (cdar alist)))))
1205 (assoc-guts (not (funcall test-not item (cdar alist))))))
1207 (if key
1208 (assoc-guts (eql item (funcall key (cdar alist))))
1209 (assoc-guts (eql item (cdar alist))))))))
1211 (defun rassoc-if (predicate alist &key key)
1212 #!+sb-doc
1213 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1214 is supplied, apply it to the CDR of each cons before testing."
1215 (let ((predicate (%coerce-callable-to-fun predicate))
1216 (key (and key (%coerce-callable-to-fun key))))
1217 (if key
1218 (assoc-guts (funcall predicate (funcall key (cdar alist))))
1219 (assoc-guts (funcall predicate (cdar alist))))))
1221 (defun rassoc-if-not (predicate alist &key key)
1222 #!+sb-doc
1223 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1224 If KEY is supplied, apply it to the CDR of each cons before testing."
1225 (let ((predicate (%coerce-callable-to-fun predicate))
1226 (key (and key (%coerce-callable-to-fun key))))
1227 (if key
1228 (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
1229 (assoc-guts (not (funcall predicate (cdar alist)))))))
1231 ;;;; mapping functions
1233 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1234 ;;; MAPL, MAPLIST, and MAPCON
1236 ;;; Map the designated function over the arglists in the appropriate
1237 ;;; way. It is done when any of the arglists runs out. Until then, it
1238 ;;; CDRs down the arglists calling the function and accumulating
1239 ;;; results as desired.
1240 (defun map1 (fun-designator original-arglists accumulate take-car)
1241 (let ((fun (%coerce-callable-to-fun fun-designator)))
1242 (let* ((arglists (copy-list original-arglists))
1243 (ret-list (list nil))
1244 (temp ret-list))
1245 (do ((res nil)
1246 (args '() '()))
1247 ((dolist (x arglists nil) (if (null x) (return t)))
1248 (if accumulate
1249 (cdr ret-list)
1250 (car original-arglists)))
1251 (do ((l arglists (cdr l)))
1252 ((null l))
1253 (push (if take-car (caar l) (car l)) args)
1254 (setf (car l) (cdar l)))
1255 (setq res (apply fun (nreverse args)))
1256 (case accumulate
1257 (:nconc (setq temp (last (nconc temp res))))
1258 (:list (rplacd temp (list res))
1259 (setq temp (cdr temp))))))))
1261 (defun mapc (function list &rest more-lists)
1262 #!+sb-doc
1263 "Apply FUNCTION to successive elements of lists. Return the second argument."
1264 (map1 function (cons list more-lists) nil t))
1266 (defun mapcar (function list &rest more-lists)
1267 #!+sb-doc
1268 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1269 return values."
1270 (map1 function (cons list more-lists) :list t))
1272 (defun mapcan (function list &rest more-lists)
1273 #!+sb-doc
1274 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1275 results."
1276 (map1 function (cons list more-lists) :nconc t))
1278 (defun mapl (function list &rest more-lists)
1279 #!+sb-doc
1280 "Apply FUNCTION to successive CDRs of list. Return NIL."
1281 (map1 function (cons list more-lists) nil nil))
1283 (defun maplist (function list &rest more-lists)
1284 #!+sb-doc
1285 "Apply FUNCTION to successive CDRs of list. Return list of results."
1286 (map1 function (cons list more-lists) :list nil))
1288 (defun mapcon (function list &rest more-lists)
1289 #!+sb-doc
1290 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1291 (map1 function (cons list more-lists) :nconc nil))
1293 ;;;; Specialized versions
1295 ;;; %ADJOIN-*, %ASSOC-*, and %MEMBER-* functions. Deftransforms
1296 ;;; delegate to TRANSFORM-LIST-ITEM-SEEK which picks the appropriate
1297 ;;; version. These win because they have only positional arguments,
1298 ;;; the TEST, TEST-NOT & KEY functions are known to exist (or not),
1299 ;;; and are known to be functions instead of function designators. We
1300 ;;; are also able to transform many common cases to -EQ versions,
1301 ;;; which are substantially faster then EQL using ones.
1302 (macrolet
1303 ((def (funs form &optional variant)
1304 (flet ((%def (name)
1305 (let* ((body-loop
1306 `(do ((list list (cdr list)))
1307 ((null list) nil)
1308 (declare (list list))
1309 (let ((this (car list)))
1310 ,(ecase name
1311 (assoc
1312 (if funs
1313 `(when this
1314 (let ((target (car this)))
1315 (when ,form
1316 (return this))))
1317 ;; If there is no TEST/TEST-NOT or
1318 ;; KEY, do the EQ/EQL test first,
1319 ;; before checking for NIL.
1320 `(let ((target (car this)))
1321 (when (and ,form this)
1322 (return this)))))
1323 (member
1324 `(let ((target this))
1325 (when ,form
1326 (return list))))
1327 (adjoin
1328 `(let ((target this))
1329 (when ,form
1330 (return t))))))))
1331 (body (if (eq 'adjoin name)
1332 `(if (let ,(when (member 'key funs)
1333 `((item (funcall key item))))
1334 ,body-loop)
1335 list
1336 (cons item list))
1337 body-loop)))
1338 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1339 (item list ,@funs)
1340 (declare (optimize speed (sb!c::verify-arg-count 0)))
1341 ,@(when funs `((declare (function ,@funs))))
1342 ,body))))
1343 `(progn
1344 ,(%def 'adjoin)
1345 ,(%def 'assoc)
1346 ,(%def 'member)))))
1347 (def ()
1348 (eql item target))
1349 (def ()
1350 (eq item target)
1352 (def (key)
1353 (eql item (funcall key target)))
1354 (def (key)
1355 (eq item (funcall key target))
1357 (def (key test)
1358 (funcall test item (funcall key target)))
1359 (def (key test-not)
1360 (not (funcall test-not item (funcall key target))))
1361 (def (test)
1362 (funcall test item target))
1363 (def (test-not)
1364 (not (funcall test-not item target))))