1.0.15.11: one more slice of ASSOC micro-optimization
[sbcl.git] / src / code / list.lisp
1 ;;;; functions to implement lists
2
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.
11
12 (in-package "SB!IMPL")
13
14 ;;; Limitation: no list might have more than INDEX conses.
15
16 ;;;; KLUDGE: comment from CMU CL, what does it mean?
17 ;;;;   NSUBLIS, things at the beginning broken.
18 ;;;; -- WHN 20000127
19
20 (declaim (maybe-inline
21           tree-equal nth %setnth nthcdr last last1 make-list append
22           nconc nconc2 member-if member-if-not tailp adjoin 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))
27
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))
114 \f
115 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
116
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 ())))
126
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 ())))
136
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))))
145
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))
152
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))))
165
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)))
170
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))
214
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)))))))
232
233 (defun last1 (list)
234   #!+sb-doc
235   "Return the last cons (not the last element) of a list"
236   (let ((rest list)
237         (list list))
238     (loop (unless (consp rest) (return list))
239           (shiftf list rest (cdr rest)))))
240
241 (defun last (list &optional (n 1))
242   #!+sb-doc
243   "Return the last N conses (not the last element!) of a list."
244   (if (eql n 1)
245       (last1 list)
246     (if (typep n 'index)
247         (do ((checked-list list (cdr checked-list))
248              (returned-list list)
249              (index 0 (1+ index)))
250             ((atom checked-list) returned-list)
251           (declare (type index index))
252           (if (>= index n)
253               (pop returned-list)))
254       list)))
255
256 (defun list (&rest args)
257   #!+sb-doc
258   "Return constructs and returns a list of its arguments."
259   args)
260
261 ;;; LIST* is done the same as LIST, except that the last cons is made
262 ;;; a dotted pair.
263
264 (defun list* (arg &rest others)
265   #!+sb-doc
266   "Return a list of the arguments with last cons a dotted pair"
267   (cond ((atom others) arg)
268         ((atom (cdr others)) (cons arg (car others)))
269         (t (do ((x others (cdr x)))
270                ((null (cddr x)) (rplacd x (cadr x))))
271            (cons arg others))))
272
273 (defun make-list (size &key initial-element)
274   #!+sb-doc
275   "Constructs a list with size elements each set to value"
276   (declare (type index size))
277   (do ((count size (1- count))
278        (result '() (cons initial-element result)))
279       ((<= count 0) result)
280     (declare (type index count))))
281 \f
282 (defun append (&rest lists)
283   #!+sb-doc
284   "Construct a new list by concatenating the list arguments"
285   (labels ((fail (object)
286              (error 'type-error
287                     :datum object
288                     :expected-type 'list))
289            (append-into (last-cons current rest)
290              "Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST)."
291              (declare (cons last-cons rest))
292              (cond ((consp current)
293                     (append-into (setf (cdr last-cons) (list (car current)))
294                                 (cdr current)
295                                 rest))
296                    ((not (null current)) (fail current))
297                    ((null (cdr rest)) (setf (cdr last-cons) (car rest)))
298                    (t (append-into last-cons (car rest) (cdr rest)))))
299            (append1 (lists)
300              (let ((current (car lists))
301                    (rest (cdr lists)))
302                (cond ((null rest) current)
303                      ((consp current)
304                       (let ((result (truly-the cons (list (car current)))))
305                         (append-into result
306                                     (cdr current)
307                                     rest)
308                         result))
309                      ((null current) (append1 rest))
310                      (t (fail current))))))
311     (append1 lists)))
312 \f
313 ;;;; list copying functions
314
315 (eval-when (:compile-toplevel :load-toplevel :execute)
316   (sb!xc:defmacro !copy-list-macro (list &key check-proper-list)
317     ;; Unless CHECK-PROPER-LIST is true, the list is copied correctly
318     ;; even if the list is not terminated by NIL. The new list is built
319     ;; by CDR'ing SPLICE which is always at the tail of the new list.
320     `(when ,list
321        (let ((copy (list (car ,list))))
322          (do ((orig (cdr ,list) (cdr orig))
323               (splice copy (cdr (rplacd splice (cons (car orig) nil)))))
324              (,@(if check-proper-list
325                     '((endp orig))
326                     '((atom orig)
327                       (unless (null orig)
328                         (rplacd splice orig))))
329               copy))))))
330
331 (defun copy-list (list)
332   #!+sb-doc
333   "Return a new list which is EQUAL to LIST. LIST may be improper."
334   (!copy-list-macro list))
335
336 (defun copy-alist (alist)
337   #!+sb-doc
338   "Return a new association list which is EQUAL to ALIST."
339   (if (endp alist)
340       alist
341       (let ((result
342              (cons (if (atom (car alist))
343                        (car alist)
344                        (cons (caar alist) (cdar alist)))
345                    nil)))
346         (do ((x (cdr alist) (cdr x))
347              (splice result
348                      (cdr (rplacd splice
349                                   (cons
350                                    (if (atom (car x))
351                                        (car x)
352                                        (cons (caar x) (cdar x)))
353                                    nil)))))
354             ((endp x)))
355         result)))
356
357 (defun copy-tree (object)
358   #!+sb-doc
359   "Recursively copy trees of conses."
360   (if (consp object)
361       (cons (copy-tree (car object)) (copy-tree (cdr object)))
362       object))
363 \f
364 ;;;; more commonly-used list functions
365
366 (defun revappend (x y)
367   #!+sb-doc
368   "Return (append (reverse x) y)."
369   (do ((top x (cdr top))
370        (result y (cons (car top) result)))
371       ((endp top) result)))
372
373 ;;; NCONC finds the first non-null list, so it can make splice point
374 ;;; to a cons. After finding the first cons element, it holds it in a
375 ;;; result variable while running down successive elements tacking
376 ;;; them together. While tacking lists together, if we encounter a
377 ;;; null list, we set the previous list's last cdr to nil just in case
378 ;;; it wasn't already nil, and it could have been dotted while the
379 ;;; null list was the last argument to NCONC. The manipulation of
380 ;;; splice (that is starting it out on a first cons, setting LAST of
381 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
382 ;;; and it avoids running down the last argument to NCONC which allows
383 ;;; the last argument to be circular.
384 (defun nconc (&rest lists)
385   #!+sb-doc
386   "Concatenates the lists given as arguments (by changing them)"
387   (flet ((fail (object)
388            (error 'type-error
389                   :datum object
390                   :expected-type 'list)))
391     (do ((top lists (cdr top)))
392         ((null top) nil)
393       (let ((top-of-top (car top)))
394         (typecase top-of-top
395           (cons
396            (let* ((result top-of-top)
397                   (splice result))
398              (do ((elements (cdr top) (cdr elements)))
399                  ((endp elements))
400                (let ((ele (car elements)))
401                  (typecase ele
402                    (cons (rplacd (last splice) ele)
403                          (setf splice ele))
404                    (null (rplacd (last splice) nil))
405                    (atom (if (cdr elements)
406                              (fail ele)
407                              (rplacd (last splice) ele)))
408                    (t (fail ele)))))
409              (return result)))
410           (null)
411           (atom
412            (if (cdr top)
413                (fail top-of-top)
414                (return top-of-top)))
415           (t (fail top-of-top)))))))
416
417 (defun nconc2 (x y)
418   (if (null x) y
419     (let ((z x)
420           (rest (cdr x)))
421       (loop
422        (unless (consp rest)
423          (rplacd z y)
424          (return x))
425        (shiftf z rest (cdr rest))))))
426
427 (defun nreconc (x y)
428   #!+sb-doc
429   "Return (NCONC (NREVERSE X) Y)."
430   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
431        (2nd x 1st)              ;2nd follows first down the list.
432        (3rd y 2nd))             ;3rd follows 2nd down the list.
433       ((atom 2nd) 3rd)
434     (rplacd 2nd 3rd)))
435 \f
436 (flet (;; Return the number of conses at the head of the
437        ;; possibly-improper list LIST. (Or if LIST is circular, you
438        ;; lose.)
439        (count-conses (list)
440          (do ((in-list list (cdr in-list))
441               (result 0 (1+ result)))
442              ((atom in-list)
443               result)
444            (declare (type index result)))))
445   (declare (ftype (function (t) index) count-conses))
446   (defun butlast (list &optional (n 1))
447     (if (typep n 'index)
448         (let ((n-conses-in-list (count-conses list)))
449           (cond ((zerop n)
450                  ;; (We can't use SUBSEQ in this case because LIST isn't
451                  ;; necessarily a proper list, but SUBSEQ expects a
452                  ;; proper sequence. COPY-LIST isn't so fussy.)
453                  (copy-list list))
454                 ((>= n n-conses-in-list)
455                  nil)
456                 (t
457                  ;; (LIST isn't necessarily a proper list in this case
458                  ;; either, and technically SUBSEQ wants a proper
459                  ;; sequence, but no reasonable implementation of SUBSEQ
460                  ;; will actually walk down to the end of the list to
461                  ;; check, and since we're calling our own implementation
462                  ;; we know it's reasonable, so it's OK.)
463                  (subseq list 0 (- n-conses-in-list n)))))
464         nil))
465   (defun nbutlast (list &optional (n 1))
466     (cond ((zerop n)
467            list)
468           ((not (typep n 'index))
469            nil)
470           (t (let ((n-conses-in-list (count-conses list)))
471                (unless (<= n-conses-in-list n)
472                  (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
473                        nil)
474                  list))))))
475
476 (defun ldiff (list object)
477   "Return a new list, whose elements are those of LIST that appear before
478    OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
479    LIST must be a proper list or a dotted list."
480   (do* ((list list (cdr list))
481         (result (list ()))
482         (splice result))
483        ((atom list)
484         (if (eql list object)
485             (cdr result)
486             (progn (rplacd splice list) (cdr result))))
487     (if (eql list object)
488         (return (cdr result))
489         (setq splice (cdr (rplacd splice (list (car list))))))))
490 \f
491 ;;;; functions to alter list structure
492
493 (defun rplaca (x y)
494   #!+sb-doc
495   "Change the CAR of X to Y and return the new X."
496   (rplaca x y))
497
498 (defun rplacd (x y)
499   #!+sb-doc
500   "Change the CDR of X to Y and return the new X."
501   (rplacd x y))
502
503 ;;; The following are for use by SETF.
504
505 (defun %rplaca (x val) (rplaca x val) val)
506
507 (defun %rplacd (x val) (rplacd x val) val)
508
509 ;;; Set the Nth element of LIST to NEWVAL.
510 (defun %setnth (n list newval)
511   (typecase n
512     (index
513      (do ((count n (1- count))
514           (list list (cdr list)))
515          ((endp list)
516           (error "~S is too large an index for SETF of NTH." n))
517        (declare (type fixnum count))
518        (when (<= count 0)
519          (rplaca list newval)
520          (return newval))))
521     (t (let ((cons (nthcdr n list)))
522          (when (endp cons)
523            (error "~S is too large an index for SETF of NTH." n))
524          (rplaca cons newval)
525          newval))))
526 \f
527 ;;;; :KEY arg optimization to save funcall of IDENTITY
528
529 ;;; APPLY-KEY saves us a function call sometimes.
530 ;;;    This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
531 ;;;    because it's used in seq.lisp and sort.lisp.
532 (defmacro apply-key (key element)
533   `(if ,key
534        (funcall ,key ,element)
535        ,element))
536 \f
537 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
538
539 ;;; Use these with the following &KEY args:
540 (defmacro with-set-keys (funcall)
541   `(if notp
542        ,(append funcall '(:key key :test-not test-not))
543        ,(append funcall '(:key key :test test))))
544
545 (defmacro satisfies-the-test (item elt)
546   (let ((key-tmp (gensym)))
547     `(let ((,key-tmp (apply-key key ,elt)))
548       (cond (testp (funcall test ,item ,key-tmp))
549             (notp (not (funcall test-not ,item ,key-tmp)))
550             (t (funcall test ,item ,key-tmp))))))
551 \f
552 ;;;; substitution of expressions
553
554 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
555   #!+sb-doc
556   "Substitutes new for subtrees matching old."
557   (when (and testp notp)
558     (error ":TEST and :TEST-NOT were both supplied."))
559   (let ((key (and key (%coerce-callable-to-fun key)))
560         (test (if testp (%coerce-callable-to-fun test) test))
561         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
562     (declare (type function test test-not))
563     (labels ((s (subtree)
564                (cond ((satisfies-the-test old subtree) new)
565                      ((atom subtree) subtree)
566                      (t (let ((car (s (car subtree)))
567                               (cdr (s (cdr subtree))))
568                           (if (and (eq car (car subtree))
569                                    (eq cdr (cdr subtree)))
570                               subtree
571                               (cons car cdr)))))))
572       (s tree))))
573
574 (defun subst-if (new test tree &key key)
575   #!+sb-doc
576   "Substitutes new for subtrees for which test is true."
577   (let ((test (%coerce-callable-to-fun test))
578         (key (and key (%coerce-callable-to-fun key))))
579     (labels ((s (subtree)
580                (cond ((funcall test (apply-key key subtree)) new)
581                      ((atom subtree) subtree)
582                      (t (let ((car (s (car subtree)))
583                               (cdr (s (cdr subtree))))
584                           (if (and (eq car (car subtree))
585                                    (eq cdr (cdr subtree)))
586                               subtree
587                               (cons car cdr)))))))
588       (s tree))))
589
590 (defun subst-if-not (new test tree &key key)
591   #!+sb-doc
592   "Substitutes new for subtrees for which test is false."
593   (let ((test (%coerce-callable-to-fun test))
594         (key (and key (%coerce-callable-to-fun key))))
595     (labels ((s (subtree)
596                (cond ((not (funcall test (apply-key key subtree))) new)
597                      ((atom subtree) subtree)
598                      (t (let ((car (s (car subtree)))
599                               (cdr (s (cdr subtree))))
600                           (if (and (eq car (car subtree))
601                                    (eq cdr (cdr subtree)))
602                               subtree
603                               (cons car cdr)))))))
604       (s tree))))
605
606 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
607   #!+sb-doc
608   "Substitute NEW for subtrees matching OLD."
609   (when (and testp notp)
610     (error ":TEST and :TEST-NOT were both supplied."))
611   (let ((key (and key (%coerce-callable-to-fun key)))
612         (test (if testp (%coerce-callable-to-fun test) test))
613         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
614     (declare (type function test test-not))
615     (labels ((s (subtree)
616                (cond ((satisfies-the-test old subtree) new)
617                      ((atom subtree) subtree)
618                      (t (do* ((last nil subtree)
619                               (subtree subtree (cdr subtree)))
620                              ((atom subtree)
621                               (if (satisfies-the-test old subtree)
622                                   (setf (cdr last) new)))
623                           (if (satisfies-the-test old subtree)
624                               (return (setf (cdr last) new))
625                               (setf (car subtree) (s (car subtree)))))
626                         subtree))))
627       (s tree))))
628
629 (defun nsubst-if (new test tree &key key)
630   #!+sb-doc
631   "Substitute NEW for subtrees of TREE for which TEST is true."
632   (let ((test (%coerce-callable-to-fun test))
633         (key (and key (%coerce-callable-to-fun key))))
634     (labels ((s (subtree)
635                (cond ((funcall test (apply-key key subtree)) new)
636                      ((atom subtree) subtree)
637                      (t (do* ((last nil subtree)
638                               (subtree subtree (cdr subtree)))
639                              ((atom subtree)
640                               (if (funcall test (apply-key key subtree))
641                                   (setf (cdr last) new)))
642                           (if (funcall test (apply-key key subtree))
643                               (return (setf (cdr last) new))
644                               (setf (car subtree) (s (car subtree)))))
645                         subtree))))
646       (s tree))))
647
648 (defun nsubst-if-not (new test tree &key key)
649   #!+sb-doc
650   "Substitute NEW for subtrees of TREE for which TEST is false."
651   (let ((test (%coerce-callable-to-fun test))
652         (key (and key (%coerce-callable-to-fun key))))
653     (labels ((s (subtree)
654                (cond ((not (funcall test (apply-key key subtree))) new)
655                      ((atom subtree) subtree)
656                      (t (do* ((last nil subtree)
657                               (subtree subtree (cdr subtree)))
658                              ((atom subtree)
659                               (if (not (funcall test (apply-key key subtree)))
660                                   (setf (cdr last) new)))
661                           (if (not (funcall test (apply-key key subtree)))
662                               (return (setf (cdr last) new))
663                               (setf (car subtree) (s (car subtree)))))
664                         subtree))))
665       (s tree))))
666 \f
667 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
668   #!+sb-doc
669   "Substitute from ALIST into TREE nondestructively."
670   (when (and testp notp)
671     (error ":TEST and :TEST-NOT were both supplied."))
672   (let ((key (and key (%coerce-callable-to-fun key)))
673         (test (if testp (%coerce-callable-to-fun test) test))
674         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
675     (declare (type function test test-not))
676     (declare (inline assoc))
677     (labels ((s (subtree)
678                (let* ((key-val (apply-key key subtree))
679                       (assoc (if notp
680                                  (assoc key-val alist :test-not test-not)
681                                  (assoc key-val alist :test test))))
682                  (cond (assoc (cdr assoc))
683                        ((atom subtree) subtree)
684                        (t (let ((car (s (car subtree)))
685                                 (cdr (s (cdr subtree))))
686                             (if (and (eq car (car subtree))
687                                      (eq cdr (cdr subtree)))
688                                 subtree
689                                 (cons car cdr))))))))
690       (s tree))))
691
692 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
693 ;;; because it can be referenced in inline expansions.
694 (defmacro nsublis-macro ()
695   (let ((key-tmp (gensym)))
696     `(let ((,key-tmp (apply-key key subtree)))
697       (if notp
698           (assoc ,key-tmp alist :test-not test-not)
699           (assoc ,key-tmp alist :test test)))))
700
701 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
702   #!+sb-doc
703   "Substitute from ALIST into TRUE destructively."
704   (when (and testp notp)
705     (error ":TEST and :TEST-NOT were both supplied."))
706   (let ((key (and key (%coerce-callable-to-fun key)))
707         (test (if testp (%coerce-callable-to-fun test) test))
708         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
709     (declare (inline assoc))
710     (let (temp)
711       (labels ((s (subtree)
712                  (cond ((setq temp (nsublis-macro))
713                         (cdr temp))
714                        ((atom subtree) subtree)
715                        (t (do* ((last nil subtree)
716                                 (subtree subtree (cdr subtree)))
717                                ((atom subtree)
718                                 (if (setq temp (nsublis-macro))
719                                     (setf (cdr last) (cdr temp))))
720                             (if (setq temp (nsublis-macro))
721                                 (return (setf (cdr last) (cdr temp)))
722                                 (setf (car subtree) (s (car subtree)))))
723                           subtree))))
724         (s tree)))))
725 \f
726 ;;;; functions for using lists as sets
727
728 (defun member (item list &key key (test #'eql testp) (test-not #'eql notp))
729   #!+sb-doc
730   "Return the tail of LIST beginning with first element satisfying EQLity,
731    :TEST, or :TEST-NOT with the given ITEM."
732   (when (and testp notp)
733     (error ":TEST and :TEST-NOT were both supplied."))
734   (let ((key (and key (%coerce-callable-to-fun key)))
735         (test (if testp (%coerce-callable-to-fun test) test))
736         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
737     (declare (type function test test-not))
738     (do ((list list (cdr list)))
739         ((null list) nil)
740       (let ((car (car list)))
741         (when (satisfies-the-test item car)
742           (return list))))))
743
744 (defun member-if (test list &key key)
745   #!+sb-doc
746   "Return tail of LIST beginning with first element satisfying TEST."
747   (let ((test (%coerce-callable-to-fun test))
748         (key (and key (%coerce-callable-to-fun key))))
749     (do ((list list (cdr list)))
750         ((endp list) nil)
751       (if (funcall test (apply-key key (car list)))
752           (return list)))))
753
754 (defun member-if-not (test list &key key)
755   #!+sb-doc
756   "Return tail of LIST beginning with first element not satisfying TEST."
757   (let ((test (%coerce-callable-to-fun test))
758         (key (and key (%coerce-callable-to-fun key))))
759     (do ((list list (cdr list)))
760         ((endp list) ())
761       (if (not (funcall test (apply-key key (car list))))
762           (return list)))))
763
764 (defun tailp (object list)
765   #!+sb-doc
766   "Return true if OBJECT is the same as some tail of LIST, otherwise
767    returns false. LIST must be a proper list or a dotted list."
768   (do ((list list (cdr list)))
769       ((atom list) (eql list object))
770     (if (eql object list)
771         (return t))))
772
773 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
774   #!+sb-doc
775   "Add ITEM to LIST unless it is already a member"
776   (when (and testp notp)
777     (error ":TEST and :TEST-NOT were both supplied."))
778   (let ((key (and key (%coerce-callable-to-fun key))))
779     (declare (inline member))
780     (if (let ((key-val (apply-key key item)))
781           (if notp
782               (member key-val list :test-not test-not :key key)
783               (member key-val list :test test :key key)))
784         list
785         (cons item list))))
786
787 (defconstant +list-based-union-limit+ 80)
788
789 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
790   #!+sb-doc
791   "Return the union of LIST1 and LIST2."
792   (declare (inline member))
793   (when (and testp notp)
794     (error ":TEST and :TEST-NOT were both supplied."))
795   ;; We have to possibilities here: for shortish lists we pick up the
796   ;; shorter one as the result, and add the other one to it. For long
797   ;; lists we use a hash-table when possible.
798   (let ((n1 (length list1))
799         (n2 (length list2))
800         (key (and key (%coerce-callable-to-fun key)))
801         (test (if notp
802                   (let ((test-not-fun (%coerce-callable-to-fun test-not)))
803                     (lambda (x) (not (funcall test-not-fun x))))
804                   (%coerce-callable-to-fun test))))
805     (multiple-value-bind (short long n-short)
806         (if (< n1 n2)
807             (values list1 list2 n1)
808             (values list2 list1 n2))
809       (if (or (< n-short +list-based-union-limit+)
810               (not (member test (list #'eq #'eql #'equal #'equalp))))
811           (let ((orig short))
812             (dolist (elt long)
813               (unless (member (apply-key key elt) orig :key key :test test)
814                 (push elt short)))
815             short)
816           (let ((table (make-hash-table :test test :size (+ n1 n2)))
817                 (union nil))
818             (dolist (elt long)
819               (setf (gethash (apply-key key elt) table) elt))
820             (dolist (elt short)
821               (setf (gethash (apply-key key elt) table) elt))
822             (maphash (lambda (k v)
823                        (declare (ignore k))
824                        (push v union))
825                      table)
826             union)))))
827
828 ;;; Destination and source are SETF-able and many-evaluable. Set the
829 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
830 ;;;
831 ;;; FIXME: needs a more mnemonic name
832 (defmacro steve-splice (source destination)
833   `(let ((temp ,source))
834      (setf ,source (cdr ,source)
835            (cdr temp) ,destination
836            ,destination temp)))
837
838 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
839   #!+sb-doc
840   "Destructively return the union of LIST1 and LIST2."
841   (declare (inline member))
842   (when (and testp notp)
843     (error ":TEST and :TEST-NOT were both supplied."))
844   ;; We have to possibilities here: for shortish lists we pick up the
845   ;; shorter one as the result, and add the other one to it. For long
846   ;; lists we use a hash-table when possible.
847   (let ((n1 (length list1))
848         (n2 (length list2))
849         (key (and key (%coerce-callable-to-fun key)))
850         (test (if notp
851                   (let ((test-not-fun (%coerce-callable-to-fun test-not)))
852                     (lambda (x) (not (funcall test-not-fun x))))
853                   (%coerce-callable-to-fun test))))
854     (multiple-value-bind (short long n-short)
855         (if (< n1 n2)
856             (values list1 list2 n1)
857             (values list2 list1 n2))
858       (if (or (< n-short +list-based-union-limit+)
859               (not (member test (list #'eq #'eql #'equal #'equalp))))
860           (let ((orig short))
861             (do ((elt (car long) (car long)))
862                 ((endp long))
863               (if (not (member (apply-key key elt) orig :key key :test test))
864                   (steve-splice long short)
865                   (setf long (cdr long))))
866             short)
867           (let ((table (make-hash-table :test test :size (+ n1 n2))))
868             (dolist (elt long)
869               (setf (gethash (apply-key key elt) table) elt))
870             (dolist (elt short)
871               (setf (gethash (apply-key key elt) table) elt))
872             (let ((union long)
873                   (head long))
874               (maphash (lambda (k v)
875                          (declare (ignore k))
876                          (if head
877                              (setf (car head) v
878                                    head (cdr head))
879                              (push v union)))
880                       table)
881               union))))))
882
883 (defun intersection (list1 list2
884                      &key key (test #'eql testp) (test-not nil notp))
885   #!+sb-doc
886   "Return the intersection of LIST1 and LIST2."
887   (declare (inline member))
888   (when (and testp notp)
889     (error ":TEST and :TEST-NOT were both supplied."))
890   (let ((key (and key (%coerce-callable-to-fun key))))
891     (let ((res nil))
892       (dolist (elt list1)
893         (if (with-set-keys (member (apply-key key elt) list2))
894             (push elt res)))
895       res)))
896
897 (defun nintersection (list1 list2
898                       &key key (test #'eql testp) (test-not nil notp))
899   #!+sb-doc
900   "Destructively return the intersection of LIST1 and LIST2."
901   (declare (inline member))
902   (when (and testp notp)
903     (error ":TEST and :TEST-NOT were both supplied."))
904   (let ((key (and key (%coerce-callable-to-fun key))))
905     (let ((res nil)
906           (list1 list1))
907       (do () ((endp list1))
908         (if (with-set-keys (member (apply-key key (car list1)) list2))
909             (steve-splice list1 res)
910             (setq list1 (cdr list1))))
911       res)))
912
913 (defun set-difference (list1 list2
914                        &key key (test #'eql testp) (test-not nil notp))
915   #!+sb-doc
916   "Return the elements of LIST1 which are not in LIST2."
917   (declare (inline member))
918   (when (and testp notp)
919     (error ":TEST and :TEST-NOT were both supplied."))
920   (let ((key (and key (%coerce-callable-to-fun key))))
921     (if (null list2)
922         list1
923         (let ((res nil))
924           (dolist (elt list1)
925             (if (not (with-set-keys (member (apply-key key elt) list2)))
926                 (push elt res)))
927           res))))
928
929 (defun nset-difference (list1 list2
930                         &key key (test #'eql testp) (test-not nil notp))
931   #!+sb-doc
932   "Destructively return the elements of LIST1 which are not in LIST2."
933   (declare (inline member))
934   (when (and testp notp)
935     (error ":TEST and :TEST-NOT were both supplied."))
936   (let ((key (and key (%coerce-callable-to-fun key))))
937     (let ((res nil)
938           (list1 list1))
939       (do () ((endp list1))
940         (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
941             (steve-splice list1 res)
942             (setq list1 (cdr list1))))
943       res)))
944
945 (defun set-exclusive-or (list1 list2
946                          &key key (test #'eql testp) (test-not #'eql notp))
947   #!+sb-doc
948   "Return new list of elements appearing exactly once in LIST1 and LIST2."
949   (declare (inline member))
950   (when (and testp notp)
951     (error ":TEST and :TEST-NOT were both supplied."))
952   (let ((result nil)
953         (key (and key (%coerce-callable-to-fun key)))
954         (test (if testp (%coerce-callable-to-fun test) test))
955         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
956     (declare (type function test test-not))
957     (dolist (elt list1)
958       (unless (with-set-keys (member (apply-key key elt) list2))
959         (setq result (cons elt result))))
960     (let ((test (if testp
961                     (lambda (x y) (funcall test y x))
962                     test))
963           (test-not (if notp
964                         (lambda (x y) (funcall test-not y x))
965                         test-not)))
966       (dolist (elt list2)
967         (unless (with-set-keys (member (apply-key key elt) list1))
968           (setq result (cons elt result)))))
969     result))
970
971 (defun nset-exclusive-or (list1 list2
972                           &key key (test #'eql testp) (test-not #'eql notp))
973   #!+sb-doc
974   "Destructively return a list with elements which appear but once in LIST1
975    and LIST2."
976   (when (and testp notp)
977     (error ":TEST and :TEST-NOT were both supplied."))
978   (let ((key (and key (%coerce-callable-to-fun key)))
979         (test (if testp (%coerce-callable-to-fun test) test))
980         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
981     (declare (type function test test-not))
982     ;; The outer loop examines LIST1 while the inner loop examines
983     ;; LIST2. If an element is found in LIST2 "equal" to the element
984     ;; in LIST1, both are spliced out. When the end of LIST1 is
985     ;; reached, what is left of LIST2 is tacked onto what is left of
986     ;; LIST1. The splicing operation ensures that the correct
987     ;; operation is performed depending on whether splice is at the
988     ;; top of the list or not.
989     (do ((list1 list1)
990          (list2 list2)
991          (x list1 (cdr x))
992          (splicex ())
993          (deleted-y ())
994          ;; elements of LIST2, which are "equal" to some processed
995          ;; earlier elements of LIST1
996          )
997         ((endp x)
998          (if (null splicex)
999              (setq list1 list2)
1000              (rplacd splicex list2))
1001          list1)
1002       (let ((key-val-x (apply-key key (car x)))
1003             (found-duplicate nil))
1004
1005         ;; Move all elements from LIST2, which are "equal" to (CAR X),
1006         ;; to DELETED-Y.
1007         (do* ((y list2 next-y)
1008               (next-y (cdr y) (cdr y))
1009               (splicey ()))
1010              ((endp y))
1011           (cond ((let ((key-val-y (apply-key key (car y))))
1012                    (if notp
1013                        (not (funcall test-not key-val-x key-val-y))
1014                        (funcall test key-val-x key-val-y)))
1015                  (if (null splicey)
1016                      (setq list2 (cdr y))
1017                      (rplacd splicey (cdr y)))
1018                  (setq deleted-y (rplacd y deleted-y))
1019                  (setq found-duplicate t))
1020                 (t (setq splicey y))))
1021
1022         (unless found-duplicate
1023           (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1024
1025         (if found-duplicate
1026             (if (null splicex)
1027                 (setq list1 (cdr x))
1028                 (rplacd splicex (cdr x)))
1029             (setq splicex x))))))
1030
1031 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1032   #!+sb-doc
1033   "Return T if every element in LIST1 is also in LIST2."
1034   (declare (inline member))
1035   (when (and testp notp)
1036     (error ":TEST and :TEST-NOT were both supplied."))
1037   (let ((key (and key (%coerce-callable-to-fun key))))
1038     (dolist (elt list1)
1039       (unless (with-set-keys (member (apply-key key elt) list2))
1040         (return-from subsetp nil)))
1041     t))
1042 \f
1043 ;;;; functions that operate on association lists
1044
1045 (defun acons (key datum alist)
1046   #!+sb-doc
1047   "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1048   (cons (cons key datum) alist))
1049
1050 (defun pairlis (keys data &optional (alist '()))
1051   #!+sb-doc
1052   "Construct an association list from KEYS and DATA (adding to ALIST)."
1053   (do ((x keys (cdr x))
1054        (y data (cdr y)))
1055       ((and (endp x) (endp y)) alist)
1056     (if (or (endp x) (endp y))
1057         (error "The lists of keys and data are of unequal length."))
1058     (setq alist (acons (car x) (car y) alist))))
1059
1060 ;;; This is defined in the run-time environment, not just the compile-time
1061 ;;; environment (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL)) because it
1062 ;;; can appear in inline expansions.
1063 (defmacro assoc-guts (test-expr)
1064   `(do ((alist alist (cdr alist)))
1065        ((endp alist))
1066     (when (and (car alist) ,test-expr)
1067       (return (car alist)))))
1068
1069 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1070   #!+sb-doc
1071   "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1072    the ITEM."
1073   (when (and testp notp)
1074     (error ":TEST and :TEST-NOT were both supplied."))
1075   (let ((key (and key (%coerce-callable-to-fun key)))
1076         (test (and testp (%coerce-callable-to-fun test)))
1077         (test-not (and notp (%coerce-callable-to-fun test-not))))
1078     (cond (test
1079            (if key
1080                (assoc-guts (funcall test item (funcall key (caar alist))))
1081                (assoc-guts (funcall test item (caar alist)))))
1082           (test-not
1083            (if key
1084                (assoc-guts (not (funcall test-not item
1085                                          (funcall key (caar alist)))))
1086                (assoc-guts (not (funcall test-not item (caar alist))))))
1087           (t
1088            (if key
1089                (assoc-guts (eql item (funcall key (caar alist))))
1090                (assoc-guts (eql item (caar alist))))))))
1091
1092 (defun assoc-if (predicate alist &key key)
1093   #!+sb-doc
1094   "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1095    KEY is supplied, apply it to the CAR of each cons before testing."
1096   (let ((predicate (%coerce-callable-to-fun predicate))
1097         (key (and key (%coerce-callable-to-fun key))))
1098     (if key
1099         (assoc-guts (funcall predicate (funcall key (caar alist))))
1100         (assoc-guts (funcall predicate (caar alist))))))
1101
1102 (defun assoc-if-not (predicate alist &key key)
1103   #!+sb-doc
1104   "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1105   If KEY is supplied, apply it to the CAR of each cons before testing."
1106   (let ((predicate (%coerce-callable-to-fun predicate))
1107         (key (and key (%coerce-callable-to-fun key))))
1108     (if key
1109         (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
1110         (assoc-guts (not (funcall predicate (caar alist)))))))
1111
1112 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1113   (declare (list alist))
1114   #!+sb-doc
1115   "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1116    the ITEM."
1117   (when (and testp notp)
1118     (error ":TEST and :TEST-NOT were both supplied."))
1119   (let ((key (and key (%coerce-callable-to-fun key)))
1120         (test (and testp (%coerce-callable-to-fun test)))
1121         (test-not (and notp (%coerce-callable-to-fun test-not))))
1122     (cond (test
1123            (if key
1124                (assoc-guts (funcall test item (funcall key (cdar alist))))
1125                (assoc-guts (funcall test item (cdar alist)))))
1126           (test-not
1127            (if key
1128                (assoc-guts (not (funcall test-not item
1129                                          (funcall key (cdar alist)))))
1130                (assoc-guts (not (funcall test-not item (cdar alist))))))
1131           (t
1132            (if key
1133                (assoc-guts (eql item (funcall key (cdar alist))))
1134                (assoc-guts (eql item (cdar alist))))))))
1135
1136 (defun rassoc-if (predicate alist &key key)
1137   #!+sb-doc
1138   "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1139   is supplied, apply it to the CDR of each cons before testing."
1140   (let ((predicate (%coerce-callable-to-fun predicate))
1141         (key (and key (%coerce-callable-to-fun key))))
1142     (if key
1143         (assoc-guts (funcall predicate (funcall key (cdar alist))))
1144         (assoc-guts (funcall predicate (cdar alist))))))
1145
1146 (defun rassoc-if-not (predicate alist &key key)
1147   #!+sb-doc
1148   "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1149   If KEY is supplied, apply it to the CDR of each cons before testing."
1150   (let ((predicate (%coerce-callable-to-fun predicate))
1151         (key (and key (%coerce-callable-to-fun key))))
1152     (if key
1153         (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
1154         (assoc-guts (not (funcall predicate (cdar alist)))))))
1155 \f
1156 ;;;; mapping functions
1157
1158 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1159 ;;; MAPL, MAPLIST, and MAPCON
1160 ;;;
1161 ;;; Map the designated function over the arglists in the appropriate
1162 ;;; way. It is done when any of the arglists runs out. Until then, it
1163 ;;; CDRs down the arglists calling the function and accumulating
1164 ;;; results as desired.
1165 (defun map1 (fun-designator original-arglists accumulate take-car)
1166   (let ((fun (%coerce-callable-to-fun fun-designator)))
1167     (let* ((arglists (copy-list original-arglists))
1168            (ret-list (list nil))
1169            (temp ret-list))
1170       (do ((res nil)
1171            (args '() '()))
1172           ((dolist (x arglists nil) (if (null x) (return t)))
1173            (if accumulate
1174                (cdr ret-list)
1175                (car original-arglists)))
1176         (do ((l arglists (cdr l)))
1177             ((null l))
1178           (push (if take-car (caar l) (car l)) args)
1179           (setf (car l) (cdar l)))
1180         (setq res (apply fun (nreverse args)))
1181         (case accumulate
1182           (:nconc (setq temp (last (nconc temp res))))
1183           (:list (rplacd temp (list res))
1184                  (setq temp (cdr temp))))))))
1185
1186 (defun mapc (function list &rest more-lists)
1187   #!+sb-doc
1188   "Apply FUNCTION to successive elements of lists. Return the second argument."
1189   (map1 function (cons list more-lists) nil t))
1190
1191 (defun mapcar (function list &rest more-lists)
1192   #!+sb-doc
1193   "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1194    return values."
1195   (map1 function (cons list more-lists) :list t))
1196
1197 (defun mapcan (function list &rest more-lists)
1198   #!+sb-doc
1199   "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1200    results."
1201   (map1 function (cons list more-lists) :nconc t))
1202
1203 (defun mapl (function list &rest more-lists)
1204   #!+sb-doc
1205   "Apply FUNCTION to successive CDRs of list. Return NIL."
1206   (map1 function (cons list more-lists) nil nil))
1207
1208 (defun maplist (function list &rest more-lists)
1209   #!+sb-doc
1210   "Apply FUNCTION to successive CDRs of list. Return list of results."
1211   (map1 function (cons list more-lists) :list nil))
1212
1213 (defun mapcon (function list &rest more-lists)
1214   #!+sb-doc
1215   "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1216   (map1 function (cons list more-lists) :nconc nil))
1217
1218 ;;;; Specialized versions
1219
1220 ;;; %MEMBER-* and %ASSOC-* functions. The transforms for MEMBER and
1221 ;;; ASSOC pick the appropriate version. These win because they have
1222 ;;; only positional arguments, the TEST, TEST-NOT & KEY functions are
1223 ;;; known to exist (or not), and are known to be functions instead of
1224 ;;; function designators. We are also able to transform many common
1225 ;;; cases to -EQ versions, which are substantially faster then EQL
1226 ;;; using ones.
1227 (macrolet
1228     ((def (funs form &optional variant)
1229        (flet ((%def (name)
1230                 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1231                      (item list ,@funs)
1232                    (declare (optimize speed))
1233                    ,@(when funs `((declare (function ,@funs))))
1234                    (do ((list list (cdr list)))
1235                        ((null list) nil)
1236                      (declare (list list))
1237                      (let ((this (car list)))
1238                        ,(ecase name
1239                                (assoc
1240                                 (if funs
1241                                     `(when this
1242                                        (let ((target (car this)))
1243                                          (when ,form
1244                                            (return this))))
1245                                     ;; If there is no TEST/TEST-NOT or
1246                                     ;; KEY, do the EQ/EQL test first,
1247                                     ;; before checking for NIL.
1248                                     `(let ((target (car this)))
1249                                        (when (and ,form this)
1250                                          (return this)))))
1251                                (member
1252                                 `(let ((target this))
1253                                    (when ,form
1254                                      (return list))))))))))
1255          `(progn
1256             ,(%def 'member)
1257             ,(%def 'assoc)))))
1258   (def ()
1259       (eql item target))
1260   (def ()
1261       (eq item target)
1262     eq)
1263   (def (key)
1264       (eql item (funcall key target)))
1265   (def (key)
1266       (eq item (funcall key target))
1267     eq)
1268   (def (key test)
1269       (funcall test item (funcall key target)))
1270   (def (key test-not)
1271       (not (funcall test-not item (funcall key target))))
1272   (def (test)
1273       (funcall test item target))
1274   (def (test-not)
1275     (not (funcall test-not item target))))