1 ;;;; functions to implement lists
3 ;;;; This software is part of the SBCL system. See the README file for
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 ;;;; KLUDGE: comment from CMU CL, what does it mean?
15 ;;;; NSUBLIS, things at the beginning broken.
18 (declaim (maybe-inline
19 tree-equal nth %setnth nthcdr last make-list append
20 nconc member member-if member-if-not tailp adjoin union
21 nunion intersection nintersection set-difference nset-difference
22 set-exclusive-or nset-exclusive-or subsetp acons assoc
23 assoc-if assoc-if-not rassoc rassoc-if rassoc-if-not subst subst-if
24 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
26 ;;; These functions perform basic list operations.
27 (defun car (list) #!+sb-doc "Return the 1st object in a list." (car list))
29 #!+sb-doc "Return all but the first object in a list."
31 (defun cadr (list) #!+sb-doc "Return the 2nd object in a list." (cadr list))
32 (defun cdar (list) #!+sb-doc "Return the cdr of the 1st sublist." (cdar list))
33 (defun caar (list) #!+sb-doc "Return the car of the 1st sublist." (caar list))
35 #!+sb-doc "Return all but the 1st two objects of a list."
38 #!+sb-doc "Return the 1st object in the cddr of a list."
41 #!+sb-doc "Return the 1st object in the cadr of a list."
44 #!+sb-doc "Return the 1st object in the caar of a list."
47 #!+sb-doc "Return the cdr of the caar of a list."
50 #!+sb-doc "Return the cdr of the cdar of a list."
53 #!+sb-doc "Return the cdr of the cddr of a list."
56 #!+sb-doc "Return the car of the cdar of a list."
59 #!+sb-doc "Return the cdr of the cadr of a list."
62 #!+sb-doc "Return the car of the caaar of a list."
65 #!+sb-doc "Return the car of the caadr of a list."
68 #!+sb-doc "Return the car of the caddr of a list."
71 #!+sb-doc "Return the car of the cdddr of a list."
74 #!+sb-doc "Return the cdr of the cdddr of a list."
77 #!+sb-doc "Return the cdr of the caaar of a list."
80 #!+sb-doc "Return the cdr of the cdaar of a list."
83 #!+sb-doc "Return the cdr of the cddar of a list."
86 #!+sb-doc "Return the car of the cadar of a list."
89 #!+sb-doc "Return the car of the cdaar of a list."
92 #!+sb-doc "Return the car of the cdadr of a list."
95 #!+sb-doc "Return the car of the cddar of a list."
98 #!+sb-doc "Return the cdr of the caadr of a list."
101 #!+sb-doc "Return the cdr of the cadar of a list."
104 #!+sb-doc "Return the cdr of the caddr of a list."
107 #!+sb-doc "Return the cdr of the cdadr of a list."
109 (defun cons (se1 se2)
110 #!+sb-doc "Return a list with SE1 as the CAR and SE2 as the CDR."
113 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
115 (defun tree-equal-test-not (x y test-not)
116 (declare (type function test-not))
119 (tree-equal-test-not (car x) (car y) test-not)
120 (tree-equal-test-not (cdr x) (cdr y) test-not)))
122 ((not (funcall test-not x y)) t)
125 (defun tree-equal-test (x y test)
126 (declare (type function test))
129 (tree-equal-test (car x) (car y) test)
130 (tree-equal-test (cdr x) (cdr y) test)))
132 ((funcall test x y) t)
135 (defun tree-equal (x y &key (test #'eql testp) (test-not nil notp))
137 "Return T if X and Y are isomorphic trees with identical leaves."
138 (when (and testp notp)
139 (error ":TEST and :TEST-NOT were both supplied."))
141 (tree-equal-test-not x y (%coerce-callable-to-fun test-not))
142 (tree-equal-test x y (%coerce-callable-to-fun test))))
146 "This is the recommended way to test for the end of a proper list. It
147 returns true if OBJECT is NIL, false if OBJECT is a CONS, and an error
148 for any other type of OBJECT."
151 (defun list-length (list)
153 "Return the length of the given List, or Nil if the List is circular."
158 (declare (type fixnum n)
160 (when (endp y) (return n))
161 (when (endp (cdr y)) (return (+ n 1)))
162 (when (and (eq y z) (> n 0)) (return nil))))
166 "Return the nth object in a list where the car is the zero-th element."
167 (car (nthcdr n list)))
171 "Return the 1st object in a list or NIL if the list is empty."
174 "Return the 2nd object in a list or NIL if there is no 2nd object."
178 "Return the 3rd object in a list or NIL if there is no 3rd object."
182 "Return the 4th object in a list or NIL if there is no 4th object."
186 "Return the 5th object in a list or NIL if there is no 5th object."
190 "Return the 6th object in a list or NIL if there is no 6th object."
191 (cadr (cddddr list)))
192 (defun seventh (list)
194 "Return the 7th object in a list or NIL if there is no 7th object."
195 (caddr (cddddr list)))
198 "Return the 8th object in a list or NIL if there is no 8th object."
199 (cadddr (cddddr list)))
202 "Return the 9th object in a list or NIL if there is no 9th object."
203 (car (cddddr (cddddr list))))
206 "Return the 10th object in a list or NIL if there is no 10th object."
207 (cadr (cddddr (cddddr list))))
210 "Means the same as the cdr of a list."
213 (defun nthcdr (n list)
214 (declare (type index n))
216 "Performs the cdr function n times on a list."
218 (result list (cdr result)))
219 ((not (plusp i)) result)
220 (declare (type index i))))
222 (defun last (list &optional (n 1))
224 "Return the last N conses (not the last element!) of a list."
225 (declare (type index n))
226 (do ((checked-list list (cdr checked-list))
228 (index 0 (1+ index)))
229 ((atom checked-list) returned-list)
230 (declare (type index index))
232 (pop returned-list))))
234 (defun list (&rest args)
236 "Return constructs and returns a list of its arguments."
239 ;;; LIST* is done the same as LIST, except that the last cons is made
242 (defun list* (arg &rest others)
244 "Return a list of the arguments with last cons a dotted pair"
245 (cond ((atom others) arg)
246 ((atom (cdr others)) (cons arg (car others)))
247 (t (do ((x others (cdr x)))
248 ((null (cddr x)) (rplacd x (cadr x))))
251 (defun make-list (size &key initial-element)
253 "Constructs a list with size elements each set to value"
254 (declare (type index size))
255 (do ((count size (1- count))
256 (result '() (cons initial-element result)))
257 ((zerop count) result)
258 (declare (type index count))))
260 ;;; The outer loop finds the first non-null list and the result is
261 ;;; started. The remaining lists in the arguments are tacked to the
262 ;;; end of the result using splice which cdr's down the end of the new
264 (defun append (&rest lists)
266 "Construct a new list by concatenating the list arguments"
267 (flet ((fail (object)
270 :expected-type 'list)))
271 (do ((top lists (cdr top))) ; CDR to first non-null list.
273 (cond ((null (car top))) ; NIL -> Keep looping
274 ((not (consp (car top))) ; Non CONS
281 (car top) ; Special case.
282 (let* ((result (cons (caar top) '()))
284 (do ((x (cdar top) (cdr x))) ; Copy first list
287 (cdr (rplacd splice (cons (car x) ()) ))) )
288 (do ((y (cdr top) (cdr y))) ; Copy rest of lists.
290 (setq splice (rplacd splice (car y)))
293 (do ((x (car y) (cdr x))) ; Inner copy loop.
297 (cdr (rplacd splice (cons (car x) ())))))
298 (fail (car y))))))))))))
300 ;;;; list copying functions
302 (defun copy-list (list)
304 "Return a new list which is EQUAL to LIST."
305 ;; The list is copied correctly even if the list is not terminated
306 ;; by NIL. The new list is built by CDR'ing SPLICE which is always
307 ;; at the tail of the new list.
310 (let ((result (list (car list))))
311 (do ((x (cdr list) (cdr x))
313 (cdr (rplacd splice (cons (car x) '() ))) ))
319 (defun copy-alist (alist)
321 "Return a new association list which is EQUAL to ALIST."
325 (cons (if (atom (car alist))
327 (cons (caar alist) (cdar alist)) )
329 (do ((x (cdr alist) (cdr x))
335 (cons (caar x) (cdar x)))
337 ;; Non-null terminated alist done here.
343 (defun copy-tree (object)
345 "Recursively copy trees of conses."
347 (cons (copy-tree (car object)) (copy-tree (cdr object)))
350 ;;;; more commonly-used list functions
352 (defun revappend (x y)
354 "Return (append (reverse x) y)."
355 (do ((top x (cdr top))
356 (result y (cons (car top) result)))
357 ((endp top) result)))
359 ;;; NCONC finds the first non-null list, so it can make splice point
360 ;;; to a cons. After finding the first cons element, it holds it in a
361 ;;; result variable while running down successive elements tacking
362 ;;; them together. While tacking lists together, if we encounter a
363 ;;; null list, we set the previous list's last cdr to nil just in case
364 ;;; it wasn't already nil, and it could have been dotted while the
365 ;;; null list was the last argument to NCONC. The manipulation of
366 ;;; splice (that is starting it out on a first cons, setting LAST of
367 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
368 ;;; and it avoids running down the last argument to NCONC which allows
369 ;;; the last argument to be circular.
370 (defun nconc (&rest lists)
372 "Concatenates the lists given as arguments (by changing them)"
373 (flet ((fail (object)
376 :expected-type 'list)))
377 (do ((top lists (cdr top)))
379 (let ((top-of-top (car top)))
382 (let* ((result top-of-top)
384 (do ((elements (cdr top) (cdr elements)))
386 (let ((ele (car elements)))
388 (cons (rplacd (last splice) ele)
390 (null (rplacd (last splice) nil))
391 (atom (if (cdr elements)
393 (rplacd (last splice) ele)))
400 (return top-of-top)))
401 (t (fail top-of-top)))))))
405 "Return (NCONC (NREVERSE X) Y)."
406 (do ((1st (cdr x) (if (atom 1st) 1st (cdr 1st)))
407 (2nd x 1st) ;2nd follows first down the list.
408 (3rd y 2nd)) ;3rd follows 2nd down the list.
412 (flet (;; Return the number of conses at the head of the
413 ;; possibly-improper list LIST. (Or if LIST is circular, you
416 (do ((in-list list (cdr in-list))
417 (result 0 (1+ result)))
420 (declare (type index result)))))
421 (declare (ftype (function (t) index) count-conses))
422 (defun butlast (list &optional (n 1))
423 (let ((n-conses-in-list (count-conses list)))
425 ;; (We can't use SUBSEQ in this case because LIST isn't
426 ;; necessarily a proper list, but SUBSEQ expects a
427 ;; proper sequence. COPY-LIST isn't so fussy.)
429 ((>= n n-conses-in-list)
432 ;; (LIST isn't necessarily a proper list in this case
433 ;; either, and technically SUBSEQ wants a proper
434 ;; sequence, but no reasonable implementation of SUBSEQ
435 ;; will actually walk down to the end of the list to
436 ;; check, and since we're calling our own implementation
437 ;; we know it's reasonable, so it's OK.)
438 (subseq list 0 (- n-conses-in-list n))))))
439 (defun nbutlast (list &optional (n 1))
442 (let ((n-conses-in-list (count-conses list)))
443 (unless (<= n-conses-in-list n)
444 (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
448 (defun ldiff (list object)
449 "Return a new list, whose elements are those of LIST that appear before
450 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
451 LIST must be a proper list or a dotted list."
452 (do* ((list list (cdr list))
456 (if (eql list object)
458 (progn (rplacd splice list) (cdr result))))
459 (if (eql list object)
460 (return (cdr result))
461 (setq splice (cdr (rplacd splice (list (car list))))))))
463 ;;;; functions to alter list structure
467 "Change the CAR of X to Y and return the new X."
472 "Change the CDR of X to Y and return the new X."
475 ;;; The following are for use by SETF.
477 (defun %rplaca (x val) (rplaca x val) val)
479 (defun %rplacd (x val) (rplacd x val) val)
481 ;;; Set the Nth element of LIST to NEWVAL.
482 (defun %setnth (n list newval)
483 (declare (type index n))
484 (do ((count n (1- count))
485 (list list (cdr list)))
487 (error "~S is too large an index for SETF of NTH." n))
488 (declare (type fixnum count))
493 ;;;; :KEY arg optimization to save funcall of IDENTITY
495 ;;; APPLY-KEY saves us a function call sometimes.
496 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
497 ;;; because it's used in seq.lisp and sort.lisp.
498 (defmacro apply-key (key element)
500 (funcall ,key ,element)
503 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
505 ;;; Use these with the following &KEY args:
506 (defmacro with-set-keys (funcall)
508 ,(append funcall '(:key key :test-not test-not))
509 ,(append funcall '(:key key :test test))))
511 (defmacro satisfies-the-test (item elt)
512 (let ((key-tmp (gensym)))
513 `(let ((,key-tmp (apply-key key ,elt)))
514 (cond (testp (funcall test ,item ,key-tmp))
515 (notp (not (funcall test-not ,item ,key-tmp)))
516 (t (funcall test ,item ,key-tmp))))))
518 ;;;; substitution of expressions
520 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
522 "Substitutes new for subtrees matching old."
523 (when (and testp notp)
524 (error ":TEST and :TEST-NOT were both supplied."))
525 (let ((key (and key (%coerce-callable-to-fun key)))
526 (test (if testp (%coerce-callable-to-fun test) test))
527 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
528 (declare (type function test test-not))
529 (labels ((s (subtree)
530 (cond ((satisfies-the-test old subtree) new)
531 ((atom subtree) subtree)
532 (t (let ((car (s (car subtree)))
533 (cdr (s (cdr subtree))))
534 (if (and (eq car (car subtree))
535 (eq cdr (cdr subtree)))
540 (defun subst-if (new test tree &key key)
542 "Substitutes new for subtrees for which test is true."
543 (let ((test (%coerce-callable-to-fun test))
544 (key (and key (%coerce-callable-to-fun key))))
545 (labels ((s (subtree)
546 (cond ((funcall test (apply-key key subtree)) new)
547 ((atom subtree) subtree)
548 (t (let ((car (s (car subtree)))
549 (cdr (s (cdr subtree))))
550 (if (and (eq car (car subtree))
551 (eq cdr (cdr subtree)))
556 (defun subst-if-not (new test tree &key key)
558 "Substitutes new for subtrees for which test is false."
559 (let ((test (%coerce-callable-to-fun test))
560 (key (and key (%coerce-callable-to-fun key))))
561 (labels ((s (subtree)
562 (cond ((not (funcall test (apply-key key subtree))) new)
563 ((atom subtree) subtree)
564 (t (let ((car (s (car subtree)))
565 (cdr (s (cdr subtree))))
566 (if (and (eq car (car subtree))
567 (eq cdr (cdr subtree)))
572 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
574 "Substitute NEW for subtrees matching OLD."
575 (when (and testp notp)
576 (error ":TEST and :TEST-NOT were both supplied."))
577 (let ((key (and key (%coerce-callable-to-fun key)))
578 (test (if testp (%coerce-callable-to-fun test) test))
579 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
580 (declare (type function test test-not))
581 (labels ((s (subtree)
582 (cond ((satisfies-the-test old subtree) new)
583 ((atom subtree) subtree)
584 (t (do* ((last nil subtree)
585 (subtree subtree (Cdr subtree)))
587 (if (satisfies-the-test old subtree)
588 (setf (cdr last) new)))
589 (if (satisfies-the-test old subtree)
590 (return (setf (cdr last) new))
591 (setf (car subtree) (s (car subtree)))))
595 (defun nsubst-if (new test tree &key key)
597 "Substitute NEW for subtrees of TREE for which TEST is true."
598 (let ((test (%coerce-callable-to-fun test))
599 (key (and key (%coerce-callable-to-fun key))))
600 (labels ((s (subtree)
601 (cond ((funcall test (apply-key key subtree)) new)
602 ((atom subtree) subtree)
603 (t (do* ((last nil subtree)
604 (subtree subtree (Cdr subtree)))
606 (if (funcall test (apply-key key subtree))
607 (setf (cdr last) new)))
608 (if (funcall test (apply-key key subtree))
609 (return (setf (cdr last) new))
610 (setf (car subtree) (s (car subtree)))))
614 (defun nsubst-if-not (new test tree &key key)
616 "Substitute NEW for subtrees of TREE for which TEST is false."
617 (let ((test (%coerce-callable-to-fun test))
618 (key (and key (%coerce-callable-to-fun key))))
619 (labels ((s (subtree)
620 (cond ((not (funcall test (apply-key key subtree))) new)
621 ((atom subtree) subtree)
622 (t (do* ((last nil subtree)
623 (subtree subtree (Cdr subtree)))
625 (if (not (funcall test (apply-key key subtree)))
626 (setf (cdr last) new)))
627 (if (not (funcall test (apply-key key subtree)))
628 (return (setf (cdr last) new))
629 (setf (car subtree) (s (car subtree)))))
633 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
635 "Substitute from ALIST into TREE nondestructively."
636 (when (and testp notp)
637 (error ":TEST and :TEST-NOT were both supplied."))
638 (let ((key (and key (%coerce-callable-to-fun key)))
639 (test (if testp (%coerce-callable-to-fun test) test))
640 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
641 (declare (type function test test-not))
642 (declare (inline assoc))
643 (labels ((s (subtree)
644 (let* ((key-val (apply-key key subtree))
646 (assoc key-val alist :test-not test-not)
647 (assoc key-val alist :test test))))
648 (cond (assoc (cdr assoc))
649 ((atom subtree) subtree)
650 (t (let ((car (s (car subtree)))
651 (cdr (s (cdr subtree))))
652 (if (and (eq car (car subtreE))
653 (eq cdr (cdr subtree)))
655 (cons car cdr))))))))
658 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
659 ;;; because it can be referenced in inline expansions.
660 (defmacro nsublis-macro ()
661 (let ((key-tmp (gensym)))
662 `(let ((,key-tmp (apply-key key subtree)))
664 (assoc ,key-tmp alist :test-not test-not)
665 (assoc ,key-tmp alist :test test)))))
667 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
669 "Substitute from ALIST into TRUE destructively."
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 (inline assoc))
677 (labels ((s (subtree)
678 (cond ((Setq temp (nsublis-macro))
680 ((atom subtree) subtree)
681 (t (do* ((last nil subtree)
682 (subtree subtree (Cdr subtree)))
684 (if (setq temp (nsublis-macro))
685 (setf (cdr last) (cdr temp))))
686 (if (setq temp (nsublis-macro))
687 (return (setf (Cdr last) (Cdr temp)))
688 (setf (car subtree) (s (car subtree)))))
692 ;;;; functions for using lists as sets
694 (defun member (item list &key key (test #'eql testp) (test-not #'eql notp))
696 "Return the tail of LIST beginning with first element satisfying EQLity,
697 :TEST, or :TEST-NOT with the given ITEM."
698 (when (and testp notp)
699 (error ":TEST and :TEST-NOT were both supplied."))
700 (let ((key (and key (%coerce-callable-to-fun key)))
701 (test (if testp (%coerce-callable-to-fun test) test))
702 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
703 (declare (type function test test-not))
704 (do ((list list (cdr list)))
706 (let ((car (car list)))
707 (if (satisfies-the-test item car)
710 (defun member-if (test list &key key)
712 "Return tail of LIST beginning with first element satisfying TEST."
713 (let ((test (%coerce-callable-to-fun test))
714 (key (and key (%coerce-callable-to-fun key))))
715 (do ((list list (cdr list)))
717 (if (funcall test (apply-key key (car list)))
720 (defun member-if-not (test list &key key)
722 "Return tail of LIST beginning with first element not satisfying TEST."
723 (let ((test (%coerce-callable-to-fun test))
724 (key (and key (%coerce-callable-to-fun key))))
725 (do ((list list (cdr list)))
727 (if (not (funcall test (apply-key key (car list))))
730 (defun tailp (object list)
732 "Return true if OBJECT is the same as some tail of LIST, otherwise
733 returns false. LIST must be a proper list or a dotted list."
734 (do ((list list (cdr list)))
735 ((atom list) (eql list object))
736 (if (eql object list)
739 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
741 "Add ITEM to LIST unless it is already a member"
742 (when (and testp notp)
743 (error ":TEST and :TEST-NOT were both supplied."))
744 (let ((key (and key (%coerce-callable-to-fun key))))
745 (declare (inline member))
746 (if (let ((key-val (apply-key key item)))
748 (member key-val list :test-not test-not :key key)
749 (member key-val list :test test :key key)))
753 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
755 "Return the union of LIST1 and LIST2."
756 (declare (inline member))
757 (when (and testp notp)
758 (error ":TEST and :TEST-NOT were both supplied."))
759 ;; We assumes LIST2 is the result, adding to it from LIST1 as
760 ;; necessary. LIST2 must initialize the result value, so the call to
761 ;; MEMBER will apply the test to the elements from LIST1 and LIST2
762 ;; in the correct order.
763 (let ((key (and key (%coerce-callable-to-fun key))))
766 (unless (with-set-keys (member (apply-key key elt) list2))
770 ;;; Destination and source are SETF-able and many-evaluable. Set the
771 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
773 ;;; FIXME: needs a more mnemonic name
774 (defmacro steve-splice (source destination)
775 `(let ((temp ,source))
776 (setf ,source (cdr ,source)
777 (cdr temp) ,destination
780 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
782 "Destructively return the union of LIST1 and LIST2."
783 (declare (inline member))
784 (when (and testp notp)
785 (error ":TEST and :TEST-NOT were both supplied."))
786 (let ((key (and key (%coerce-callable-to-fun key))))
791 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
792 (steve-splice list1 res)
793 (setf list1 (cdr list1))))
796 (defun intersection (list1 list2
797 &key key (test #'eql testp) (test-not nil notp))
799 "Return the intersection of LIST1 and LIST2."
800 (declare (inline member))
801 (when (and testp notp)
802 (error ":TEST and :TEST-NOT were both supplied."))
803 (let ((key (and key (%coerce-callable-to-fun key))))
806 (if (with-set-keys (member (apply-key key elt) list2))
810 (defun nintersection (list1 list2
811 &key key (test #'eql testp) (test-not nil notp))
813 "Destructively return the intersection of LIST1 and LIST2."
814 (declare (inline member))
815 (when (and testp notp)
816 (error ":TEST and :TEST-NOT were both supplied."))
817 (let ((key (and key (%coerce-callable-to-fun key))))
820 (do () ((endp list1))
821 (if (with-set-keys (member (apply-key key (car list1)) list2))
822 (steve-splice list1 res)
823 (setq list1 (Cdr list1))))
826 (defun set-difference (list1 list2
827 &key key (test #'eql testp) (test-not nil notp))
829 "Return the elements of LIST1 which are not in LIST2."
830 (declare (inline member))
831 (when (and testp notp)
832 (error ":TEST and :TEST-NOT were both supplied."))
833 (let ((key (and key (%coerce-callable-to-fun key))))
838 (if (not (with-set-keys (member (apply-key key elt) list2)))
842 (defun nset-difference (list1 list2
843 &key key (test #'eql testp) (test-not nil notp))
845 "Destructively return the elements of LIST1 which are not in LIST2."
846 (declare (inline member))
847 (when (and testp notp)
848 (error ":TEST and :TEST-NOT were both supplied."))
849 (let ((key (and key (%coerce-callable-to-fun key))))
852 (do () ((endp list1))
853 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
854 (steve-splice list1 res)
855 (setq list1 (cdr list1))))
858 (defun set-exclusive-or (list1 list2
859 &key key (test #'eql testp) (test-not #'eql notp))
861 "Return new list of elements appearing exactly once in LIST1 and LIST2."
862 (declare (inline member))
863 (when (and testp notp)
864 (error ":TEST and :TEST-NOT were both supplied."))
866 (key (and key (%coerce-callable-to-fun key)))
867 (test (if testp (%coerce-callable-to-fun test) test))
868 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
869 (declare (type function test test-not))
871 (unless (with-set-keys (member (apply-key key elt) list2))
872 (setq result (cons elt result))))
873 (let ((test (if testp
874 (lambda (x y) (funcall test y x))
877 (lambda (x y) (funcall test-not y x))
880 (unless (with-set-keys (member (apply-key key elt) list1))
881 (setq result (cons elt result)))))
884 (defun nset-exclusive-or (list1 list2
885 &key key (test #'eql testp) (test-not #'eql notp))
887 "Destructively return a list with elements which appear but once in LIST1
889 (when (and testp notp)
890 (error ":TEST and :TEST-NOT were both supplied."))
891 (let ((key (and key (%coerce-callable-to-fun key)))
892 (test (if testp (%coerce-callable-to-fun test) test))
893 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
894 (declare (type function test test-not))
895 ;; The outer loop examines LIST1 while the inner loop examines
896 ;; LIST2. If an element is found in LIST2 "equal" to the element
897 ;; in LIST1, both are spliced out. When the end of LIST1 is
898 ;; reached, what is left of LIST2 is tacked onto what is left of
899 ;; LIST1. The splicing operation ensures that the correct
900 ;; operation is performed depending on whether splice is at the
901 ;; top of the list or not
909 (rplacd splicex list2))
911 (do ((y list2 (cdr y))
913 ((endp y) (setq splicex x))
914 (cond ((let ((key-val-x (apply-key key (car x)))
915 (key-val-y (apply-key key (Car y))))
917 (not (funcall test-not key-val-x key-val-y))
918 (funcall test key-val-x key-val-y)))
921 (rplacd splicex (cdr x)))
924 (rplacd splicey (cdr y)))
925 (return ())) ; assume lists are really sets
926 (t (setq splicey y)))))))
928 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
930 "Return T if every element in LIST1 is also in LIST2."
931 (declare (inline member))
932 (when (and testp notp)
933 (error ":TEST and :TEST-NOT were both supplied."))
934 (let ((key (and key (%coerce-callable-to-fun key))))
936 (unless (with-set-keys (member (apply-key key elt) list2))
937 (return-from subsetp nil)))
940 ;;;; functions that operate on association lists
942 (defun acons (key datum alist)
944 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
945 (cons (cons key datum) alist))
947 (defun pairlis (keys data &optional (alist '()))
949 "Construct an association list from KEYS and DATA (adding to ALIST)."
950 (do ((x keys (cdr x))
952 ((and (endp x) (endp y)) alist)
953 (if (or (endp x) (endp y))
954 (error "The lists of keys and data are of unequal length."))
955 (setq alist (acons (car x) (car y) alist))))
957 ;;; This is defined in the run-time environment, not just the compile-time
958 ;;; environment (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL)) because it
959 ;;; can appear in inline expansions.
960 (defmacro assoc-guts (test-expr)
961 `(do ((alist alist (cdr alist)))
963 (when (and (car alist) ,test-expr)
964 (return (car alist)))))
966 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
968 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
970 (when (and testp notp)
971 (error ":TEST and :TEST-NOT were both supplied."))
972 (let ((key (and key (%coerce-callable-to-fun key)))
973 (test (and testp (%coerce-callable-to-fun test)))
974 (test-not (and notp (%coerce-callable-to-fun test-not))))
977 (assoc-guts (funcall test item (funcall key (caar alist))))
978 (assoc-guts (funcall test item (caar alist)))))
981 (assoc-guts (not (funcall test-not item
982 (funcall key (caar alist)))))
983 (assoc-guts (not (funcall test-not item (caar alist))))))
986 (assoc-guts (eql item (funcall key (caar alist))))
987 (assoc-guts (eql item (caar alist))))))))
989 (defun assoc-if (predicate alist &key key)
991 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
992 KEY is supplied, apply it to the CAR of each cons before testing."
993 (let ((predicate (%coerce-callable-to-fun predicate))
994 (key (and key (%coerce-callable-to-fun key))))
996 (assoc-guts (funcall predicate (funcall key (caar alist))))
997 (assoc-guts (funcall predicate (caar alist))))))
999 (defun assoc-if-not (predicate alist &key key)
1001 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1002 If KEY is supplied, apply it to the CAR of each cons before testing."
1003 (let ((predicate (%coerce-callable-to-fun predicate))
1004 (key (and key (%coerce-callable-to-fun key))))
1006 (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
1007 (assoc-guts (not (funcall predicate (caar alist)))))))
1009 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1010 (declare (list alist))
1012 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1014 (when (and testp notp)
1015 (error ":TEST and :TEST-NOT were both supplied."))
1016 (let ((key (and key (%coerce-callable-to-fun key)))
1017 (test (and testp (%coerce-callable-to-fun test)))
1018 (test-not (and notp (%coerce-callable-to-fun test-not))))
1021 (assoc-guts (funcall test item (funcall key (cdar alist))))
1022 (assoc-guts (funcall test item (cdar alist)))))
1025 (assoc-guts (not (funcall test-not item
1026 (funcall key (cdar alist)))))
1027 (assoc-guts (not (funcall test-not item (cdar alist))))))
1030 (assoc-guts (eql item (funcall key (cdar alist))))
1031 (assoc-guts (eql item (cdar alist))))))))
1033 (defun rassoc-if (predicate alist &key key)
1035 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1036 is supplied, apply it to the CDR of each cons before testing."
1037 (let ((predicate (%coerce-callable-to-fun predicate))
1038 (key (and key (%coerce-callable-to-fun key))))
1040 (assoc-guts (funcall predicate (funcall key (cdar alist))))
1041 (assoc-guts (funcall predicate (cdar alist))))))
1043 (defun rassoc-if-not (predicate alist &key key)
1045 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1046 If KEY is supplied, apply it to the CDR of each cons before testing."
1047 (let ((predicate (%coerce-callable-to-fun predicate))
1048 (key (and key (%coerce-callable-to-fun key))))
1050 (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
1051 (assoc-guts (not (funcall predicate (cdar alist)))))))
1053 ;;;; mapping functions
1055 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1056 ;;; MAPL, MAPLIST, and MAPCON
1058 ;;; Map the designated function over the arglists in the appropriate
1059 ;;; way. It is done when any of the arglists runs out. Until then, it
1060 ;;; CDRs down the arglists calling the function and accumulating
1061 ;;; results as desired.
1062 (defun map1 (fun-designator original-arglists accumulate take-car)
1063 (let ((fun (%coerce-callable-to-fun fun-designator)))
1064 (let* ((arglists (copy-list original-arglists))
1065 (ret-list (list nil))
1069 ((dolist (x arglists nil) (if (null x) (return t)))
1072 (car original-arglists)))
1073 (do ((l arglists (cdr l)))
1075 (push (if take-car (caar l) (car l)) args)
1076 (setf (car l) (cdar l)))
1077 (setq res (apply fun (nreverse args)))
1079 (:nconc (setq temp (last (nconc temp res))))
1080 (:list (rplacd temp (list res))
1081 (setq temp (cdr temp))))))))
1083 (defun mapc (function list &rest more-lists)
1085 "Apply FUNCTION to successive elements of lists. Return the second argument."
1086 (map1 function (cons list more-lists) nil t))
1088 (defun mapcar (function list &rest more-lists)
1090 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1092 (map1 function (cons list more-lists) :list t))
1094 (defun mapcan (function list &rest more-lists)
1096 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1098 (map1 function (cons list more-lists) :nconc t))
1100 (defun mapl (function list &rest more-lists)
1102 "Apply FUNCTION to successive CDRs of list. Return NIL."
1103 (map1 function (cons list more-lists) nil nil))
1105 (defun maplist (function list &rest more-lists)
1107 "Apply FUNCTION to successive CDRs of list. Return list of results."
1108 (map1 function (cons list more-lists) :list nil))
1110 (defun mapcon (function list &rest more-lists)
1112 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1113 (map1 function (cons list more-lists) :nconc nil))