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 (defun append (&rest lists)
262 "Construct a new list by concatenating the list arguments"
263 (labels ((fail (object)
266 :expected-type 'list))
267 (append-into (last-cons current rest)
268 "Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST)."
269 (declare (cons last-cons rest))
270 (cond ((consp current)
271 (append-into (setf (cdr last-cons) (list (car current)))
274 ((not (null current)) (fail current))
275 ((null (cdr rest)) (setf (cdr last-cons) (car rest)))
276 (t (append-into last-cons (car rest) (cdr rest)))))
278 (let ((current (car lists))
280 (cond ((null rest) current)
282 (let ((result (truly-the cons (list (car current)))))
287 ((null current) (append1 rest))
288 (t (fail current))))))
291 ;;;; list copying functions
293 (defun copy-list (list)
295 "Return a new list which is EQUAL to LIST."
296 ;; The list is copied correctly even if the list is not terminated
297 ;; by NIL. The new list is built by CDR'ing SPLICE which is always
298 ;; at the tail of the new list.
301 (let ((result (list (car list))))
302 (do ((x (cdr list) (cdr x))
304 (cdr (rplacd splice (cons (car x) '() ))) ))
310 (defun copy-alist (alist)
312 "Return a new association list which is EQUAL to ALIST."
316 (cons (if (atom (car alist))
318 (cons (caar alist) (cdar alist)) )
320 (do ((x (cdr alist) (cdr x))
326 (cons (caar x) (cdar x)))
328 ;; Non-null terminated alist done here.
334 (defun copy-tree (object)
336 "Recursively copy trees of conses."
338 (cons (copy-tree (car object)) (copy-tree (cdr object)))
341 ;;;; more commonly-used list functions
343 (defun revappend (x y)
345 "Return (append (reverse x) y)."
346 (do ((top x (cdr top))
347 (result y (cons (car top) result)))
348 ((endp top) result)))
350 ;;; NCONC finds the first non-null list, so it can make splice point
351 ;;; to a cons. After finding the first cons element, it holds it in a
352 ;;; result variable while running down successive elements tacking
353 ;;; them together. While tacking lists together, if we encounter a
354 ;;; null list, we set the previous list's last cdr to nil just in case
355 ;;; it wasn't already nil, and it could have been dotted while the
356 ;;; null list was the last argument to NCONC. The manipulation of
357 ;;; splice (that is starting it out on a first cons, setting LAST of
358 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
359 ;;; and it avoids running down the last argument to NCONC which allows
360 ;;; the last argument to be circular.
361 (defun nconc (&rest lists)
363 "Concatenates the lists given as arguments (by changing them)"
364 (flet ((fail (object)
367 :expected-type 'list)))
368 (do ((top lists (cdr top)))
370 (let ((top-of-top (car top)))
373 (let* ((result top-of-top)
375 (do ((elements (cdr top) (cdr elements)))
377 (let ((ele (car elements)))
379 (cons (rplacd (last splice) ele)
381 (null (rplacd (last splice) nil))
382 (atom (if (cdr elements)
384 (rplacd (last splice) ele)))
391 (return top-of-top)))
392 (t (fail top-of-top)))))))
396 "Return (NCONC (NREVERSE X) Y)."
397 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
398 (2nd x 1st) ;2nd follows first down the list.
399 (3rd y 2nd)) ;3rd follows 2nd down the list.
403 (flet (;; Return the number of conses at the head of the
404 ;; possibly-improper list LIST. (Or if LIST is circular, you
407 (do ((in-list list (cdr in-list))
408 (result 0 (1+ result)))
411 (declare (type index result)))))
412 (declare (ftype (function (t) index) count-conses))
413 (defun butlast (list &optional (n 1))
414 (let ((n-conses-in-list (count-conses list)))
416 ;; (We can't use SUBSEQ in this case because LIST isn't
417 ;; necessarily a proper list, but SUBSEQ expects a
418 ;; proper sequence. COPY-LIST isn't so fussy.)
420 ((>= n n-conses-in-list)
423 ;; (LIST isn't necessarily a proper list in this case
424 ;; either, and technically SUBSEQ wants a proper
425 ;; sequence, but no reasonable implementation of SUBSEQ
426 ;; will actually walk down to the end of the list to
427 ;; check, and since we're calling our own implementation
428 ;; we know it's reasonable, so it's OK.)
429 (subseq list 0 (- n-conses-in-list n))))))
430 (defun nbutlast (list &optional (n 1))
433 (let ((n-conses-in-list (count-conses list)))
434 (unless (<= n-conses-in-list n)
435 (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
439 (defun ldiff (list object)
440 "Return a new list, whose elements are those of LIST that appear before
441 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
442 LIST must be a proper list or a dotted list."
443 (do* ((list list (cdr list))
447 (if (eql list object)
449 (progn (rplacd splice list) (cdr result))))
450 (if (eql list object)
451 (return (cdr result))
452 (setq splice (cdr (rplacd splice (list (car list))))))))
454 ;;;; functions to alter list structure
458 "Change the CAR of X to Y and return the new X."
463 "Change the CDR of X to Y and return the new X."
466 ;;; The following are for use by SETF.
468 (defun %rplaca (x val) (rplaca x val) val)
470 (defun %rplacd (x val) (rplacd x val) val)
472 ;;; Set the Nth element of LIST to NEWVAL.
473 (defun %setnth (n list newval)
474 (declare (type index n))
475 (do ((count n (1- count))
476 (list list (cdr list)))
478 (error "~S is too large an index for SETF of NTH." n))
479 (declare (type fixnum count))
484 ;;;; :KEY arg optimization to save funcall of IDENTITY
486 ;;; APPLY-KEY saves us a function call sometimes.
487 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
488 ;;; because it's used in seq.lisp and sort.lisp.
489 (defmacro apply-key (key element)
491 (funcall ,key ,element)
494 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
496 ;;; Use these with the following &KEY args:
497 (defmacro with-set-keys (funcall)
499 ,(append funcall '(:key key :test-not test-not))
500 ,(append funcall '(:key key :test test))))
502 (defmacro satisfies-the-test (item elt)
503 (let ((key-tmp (gensym)))
504 `(let ((,key-tmp (apply-key key ,elt)))
505 (cond (testp (funcall test ,item ,key-tmp))
506 (notp (not (funcall test-not ,item ,key-tmp)))
507 (t (funcall test ,item ,key-tmp))))))
509 ;;;; substitution of expressions
511 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
513 "Substitutes new for subtrees matching old."
514 (when (and testp notp)
515 (error ":TEST and :TEST-NOT were both supplied."))
516 (let ((key (and key (%coerce-callable-to-fun key)))
517 (test (if testp (%coerce-callable-to-fun test) test))
518 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
519 (declare (type function test test-not))
520 (labels ((s (subtree)
521 (cond ((satisfies-the-test old subtree) new)
522 ((atom subtree) subtree)
523 (t (let ((car (s (car subtree)))
524 (cdr (s (cdr subtree))))
525 (if (and (eq car (car subtree))
526 (eq cdr (cdr subtree)))
531 (defun subst-if (new test tree &key key)
533 "Substitutes new for subtrees for which test is true."
534 (let ((test (%coerce-callable-to-fun test))
535 (key (and key (%coerce-callable-to-fun key))))
536 (labels ((s (subtree)
537 (cond ((funcall test (apply-key key subtree)) new)
538 ((atom subtree) subtree)
539 (t (let ((car (s (car subtree)))
540 (cdr (s (cdr subtree))))
541 (if (and (eq car (car subtree))
542 (eq cdr (cdr subtree)))
547 (defun subst-if-not (new test tree &key key)
549 "Substitutes new for subtrees for which test is false."
550 (let ((test (%coerce-callable-to-fun test))
551 (key (and key (%coerce-callable-to-fun key))))
552 (labels ((s (subtree)
553 (cond ((not (funcall test (apply-key key subtree))) new)
554 ((atom subtree) subtree)
555 (t (let ((car (s (car subtree)))
556 (cdr (s (cdr subtree))))
557 (if (and (eq car (car subtree))
558 (eq cdr (cdr subtree)))
563 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
565 "Substitute NEW for subtrees matching OLD."
566 (when (and testp notp)
567 (error ":TEST and :TEST-NOT were both supplied."))
568 (let ((key (and key (%coerce-callable-to-fun key)))
569 (test (if testp (%coerce-callable-to-fun test) test))
570 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
571 (declare (type function test test-not))
572 (labels ((s (subtree)
573 (cond ((satisfies-the-test old subtree) new)
574 ((atom subtree) subtree)
575 (t (do* ((last nil subtree)
576 (subtree subtree (Cdr subtree)))
578 (if (satisfies-the-test old subtree)
579 (setf (cdr last) new)))
580 (if (satisfies-the-test old subtree)
581 (return (setf (cdr last) new))
582 (setf (car subtree) (s (car subtree)))))
586 (defun nsubst-if (new test tree &key key)
588 "Substitute NEW for subtrees of TREE for which TEST is true."
589 (let ((test (%coerce-callable-to-fun test))
590 (key (and key (%coerce-callable-to-fun key))))
591 (labels ((s (subtree)
592 (cond ((funcall test (apply-key key subtree)) new)
593 ((atom subtree) subtree)
594 (t (do* ((last nil subtree)
595 (subtree subtree (Cdr subtree)))
597 (if (funcall test (apply-key key subtree))
598 (setf (cdr last) new)))
599 (if (funcall test (apply-key key subtree))
600 (return (setf (cdr last) new))
601 (setf (car subtree) (s (car subtree)))))
605 (defun nsubst-if-not (new test tree &key key)
607 "Substitute NEW for subtrees of TREE for which TEST is false."
608 (let ((test (%coerce-callable-to-fun test))
609 (key (and key (%coerce-callable-to-fun key))))
610 (labels ((s (subtree)
611 (cond ((not (funcall test (apply-key key subtree))) new)
612 ((atom subtree) subtree)
613 (t (do* ((last nil subtree)
614 (subtree subtree (Cdr subtree)))
616 (if (not (funcall test (apply-key key subtree)))
617 (setf (cdr last) new)))
618 (if (not (funcall test (apply-key key subtree)))
619 (return (setf (cdr last) new))
620 (setf (car subtree) (s (car subtree)))))
624 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
626 "Substitute from ALIST into TREE nondestructively."
627 (when (and testp notp)
628 (error ":TEST and :TEST-NOT were both supplied."))
629 (let ((key (and key (%coerce-callable-to-fun key)))
630 (test (if testp (%coerce-callable-to-fun test) test))
631 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
632 (declare (type function test test-not))
633 (declare (inline assoc))
634 (labels ((s (subtree)
635 (let* ((key-val (apply-key key subtree))
637 (assoc key-val alist :test-not test-not)
638 (assoc key-val alist :test test))))
639 (cond (assoc (cdr assoc))
640 ((atom subtree) subtree)
641 (t (let ((car (s (car subtree)))
642 (cdr (s (cdr subtree))))
643 (if (and (eq car (car subtreE))
644 (eq cdr (cdr subtree)))
646 (cons car cdr))))))))
649 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
650 ;;; because it can be referenced in inline expansions.
651 (defmacro nsublis-macro ()
652 (let ((key-tmp (gensym)))
653 `(let ((,key-tmp (apply-key key subtree)))
655 (assoc ,key-tmp alist :test-not test-not)
656 (assoc ,key-tmp alist :test test)))))
658 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
660 "Substitute from ALIST into TRUE destructively."
661 (when (and testp notp)
662 (error ":TEST and :TEST-NOT were both supplied."))
663 (let ((key (and key (%coerce-callable-to-fun key)))
664 (test (if testp (%coerce-callable-to-fun test) test))
665 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
666 (declare (inline assoc))
668 (labels ((s (subtree)
669 (cond ((Setq temp (nsublis-macro))
671 ((atom subtree) subtree)
672 (t (do* ((last nil subtree)
673 (subtree subtree (Cdr subtree)))
675 (if (setq temp (nsublis-macro))
676 (setf (cdr last) (cdr temp))))
677 (if (setq temp (nsublis-macro))
678 (return (setf (Cdr last) (Cdr temp)))
679 (setf (car subtree) (s (car subtree)))))
683 ;;;; functions for using lists as sets
685 (defun member (item list &key key (test #'eql testp) (test-not #'eql notp))
687 "Return the tail of LIST beginning with first element satisfying EQLity,
688 :TEST, or :TEST-NOT with the given ITEM."
689 (when (and testp notp)
690 (error ":TEST and :TEST-NOT were both supplied."))
691 (let ((key (and key (%coerce-callable-to-fun key)))
692 (test (if testp (%coerce-callable-to-fun test) test))
693 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
694 (declare (type function test test-not))
695 (do ((list list (cdr list)))
697 (let ((car (car list)))
698 (if (satisfies-the-test item car)
701 (defun member-if (test list &key key)
703 "Return tail of LIST beginning with first element satisfying TEST."
704 (let ((test (%coerce-callable-to-fun test))
705 (key (and key (%coerce-callable-to-fun key))))
706 (do ((list list (cdr list)))
708 (if (funcall test (apply-key key (car list)))
711 (defun member-if-not (test list &key key)
713 "Return tail of LIST beginning with first element not satisfying TEST."
714 (let ((test (%coerce-callable-to-fun test))
715 (key (and key (%coerce-callable-to-fun key))))
716 (do ((list list (cdr list)))
718 (if (not (funcall test (apply-key key (car list))))
721 (defun tailp (object list)
723 "Return true if OBJECT is the same as some tail of LIST, otherwise
724 returns false. LIST must be a proper list or a dotted list."
725 (do ((list list (cdr list)))
726 ((atom list) (eql list object))
727 (if (eql object list)
730 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
732 "Add ITEM to LIST unless it is already a member"
733 (when (and testp notp)
734 (error ":TEST and :TEST-NOT were both supplied."))
735 (let ((key (and key (%coerce-callable-to-fun key))))
736 (declare (inline member))
737 (if (let ((key-val (apply-key key item)))
739 (member key-val list :test-not test-not :key key)
740 (member key-val list :test test :key key)))
744 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
746 "Return the union of LIST1 and LIST2."
747 (declare (inline member))
748 (when (and testp notp)
749 (error ":TEST and :TEST-NOT were both supplied."))
750 ;; We assumes LIST2 is the result, adding to it from LIST1 as
751 ;; necessary. LIST2 must initialize the result value, so the call to
752 ;; MEMBER will apply the test to the elements from LIST1 and LIST2
753 ;; in the correct order.
754 (let ((key (and key (%coerce-callable-to-fun key))))
757 (unless (with-set-keys (member (apply-key key elt) list2))
761 ;;; Destination and source are SETF-able and many-evaluable. Set the
762 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
764 ;;; FIXME: needs a more mnemonic name
765 (defmacro steve-splice (source destination)
766 `(let ((temp ,source))
767 (setf ,source (cdr ,source)
768 (cdr temp) ,destination
771 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
773 "Destructively return the union of LIST1 and LIST2."
774 (declare (inline member))
775 (when (and testp notp)
776 (error ":TEST and :TEST-NOT were both supplied."))
777 (let ((key (and key (%coerce-callable-to-fun key))))
782 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
783 (steve-splice list1 res)
784 (setf list1 (cdr list1))))
787 (defun intersection (list1 list2
788 &key key (test #'eql testp) (test-not nil notp))
790 "Return the intersection of LIST1 and LIST2."
791 (declare (inline member))
792 (when (and testp notp)
793 (error ":TEST and :TEST-NOT were both supplied."))
794 (let ((key (and key (%coerce-callable-to-fun key))))
797 (if (with-set-keys (member (apply-key key elt) list2))
801 (defun nintersection (list1 list2
802 &key key (test #'eql testp) (test-not nil notp))
804 "Destructively return the intersection of LIST1 and LIST2."
805 (declare (inline member))
806 (when (and testp notp)
807 (error ":TEST and :TEST-NOT were both supplied."))
808 (let ((key (and key (%coerce-callable-to-fun key))))
811 (do () ((endp list1))
812 (if (with-set-keys (member (apply-key key (car list1)) list2))
813 (steve-splice list1 res)
814 (setq list1 (Cdr list1))))
817 (defun set-difference (list1 list2
818 &key key (test #'eql testp) (test-not nil notp))
820 "Return the elements of LIST1 which are not in LIST2."
821 (declare (inline member))
822 (when (and testp notp)
823 (error ":TEST and :TEST-NOT were both supplied."))
824 (let ((key (and key (%coerce-callable-to-fun key))))
829 (if (not (with-set-keys (member (apply-key key elt) list2)))
833 (defun nset-difference (list1 list2
834 &key key (test #'eql testp) (test-not nil notp))
836 "Destructively return the elements of LIST1 which are not in LIST2."
837 (declare (inline member))
838 (when (and testp notp)
839 (error ":TEST and :TEST-NOT were both supplied."))
840 (let ((key (and key (%coerce-callable-to-fun key))))
843 (do () ((endp list1))
844 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
845 (steve-splice list1 res)
846 (setq list1 (cdr list1))))
849 (defun set-exclusive-or (list1 list2
850 &key key (test #'eql testp) (test-not #'eql notp))
852 "Return new list of elements appearing exactly once in LIST1 and LIST2."
853 (declare (inline member))
854 (when (and testp notp)
855 (error ":TEST and :TEST-NOT were both supplied."))
857 (key (and key (%coerce-callable-to-fun key)))
858 (test (if testp (%coerce-callable-to-fun test) test))
859 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
860 (declare (type function test test-not))
862 (unless (with-set-keys (member (apply-key key elt) list2))
863 (setq result (cons elt result))))
864 (let ((test (if testp
865 (lambda (x y) (funcall test y x))
868 (lambda (x y) (funcall test-not y x))
871 (unless (with-set-keys (member (apply-key key elt) list1))
872 (setq result (cons elt result)))))
875 (defun nset-exclusive-or (list1 list2
876 &key key (test #'eql testp) (test-not #'eql notp))
878 "Destructively return a list with elements which appear but once in LIST1
880 (when (and testp notp)
881 (error ":TEST and :TEST-NOT were both supplied."))
882 (let ((key (and key (%coerce-callable-to-fun key)))
883 (test (if testp (%coerce-callable-to-fun test) test))
884 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
885 (declare (type function test test-not))
886 ;; The outer loop examines LIST1 while the inner loop examines
887 ;; LIST2. If an element is found in LIST2 "equal" to the element
888 ;; in LIST1, both are spliced out. When the end of LIST1 is
889 ;; reached, what is left of LIST2 is tacked onto what is left of
890 ;; LIST1. The splicing operation ensures that the correct
891 ;; operation is performed depending on whether splice is at the
892 ;; top of the list or not
900 (rplacd splicex list2))
902 (do ((y list2 (cdr y))
904 ((endp y) (setq splicex x))
905 (cond ((let ((key-val-x (apply-key key (car x)))
906 (key-val-y (apply-key key (Car y))))
908 (not (funcall test-not key-val-x key-val-y))
909 (funcall test key-val-x key-val-y)))
912 (rplacd splicex (cdr x)))
915 (rplacd splicey (cdr y)))
916 (return ())) ; assume lists are really sets
917 (t (setq splicey y)))))))
919 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
921 "Return T if every element in LIST1 is also in LIST2."
922 (declare (inline member))
923 (when (and testp notp)
924 (error ":TEST and :TEST-NOT were both supplied."))
925 (let ((key (and key (%coerce-callable-to-fun key))))
927 (unless (with-set-keys (member (apply-key key elt) list2))
928 (return-from subsetp nil)))
931 ;;;; functions that operate on association lists
933 (defun acons (key datum alist)
935 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
936 (cons (cons key datum) alist))
938 (defun pairlis (keys data &optional (alist '()))
940 "Construct an association list from KEYS and DATA (adding to ALIST)."
941 (do ((x keys (cdr x))
943 ((and (endp x) (endp y)) alist)
944 (if (or (endp x) (endp y))
945 (error "The lists of keys and data are of unequal length."))
946 (setq alist (acons (car x) (car y) alist))))
948 ;;; This is defined in the run-time environment, not just the compile-time
949 ;;; environment (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL)) because it
950 ;;; can appear in inline expansions.
951 (defmacro assoc-guts (test-expr)
952 `(do ((alist alist (cdr alist)))
954 (when (and (car alist) ,test-expr)
955 (return (car alist)))))
957 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
959 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
961 (when (and testp notp)
962 (error ":TEST and :TEST-NOT were both supplied."))
963 (let ((key (and key (%coerce-callable-to-fun key)))
964 (test (and testp (%coerce-callable-to-fun test)))
965 (test-not (and notp (%coerce-callable-to-fun test-not))))
968 (assoc-guts (funcall test item (funcall key (caar alist))))
969 (assoc-guts (funcall test item (caar alist)))))
972 (assoc-guts (not (funcall test-not item
973 (funcall key (caar alist)))))
974 (assoc-guts (not (funcall test-not item (caar alist))))))
977 (assoc-guts (eql item (funcall key (caar alist))))
978 (assoc-guts (eql item (caar alist))))))))
980 (defun assoc-if (predicate alist &key key)
982 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
983 KEY is supplied, apply it to the CAR of each cons before testing."
984 (let ((predicate (%coerce-callable-to-fun predicate))
985 (key (and key (%coerce-callable-to-fun key))))
987 (assoc-guts (funcall predicate (funcall key (caar alist))))
988 (assoc-guts (funcall predicate (caar alist))))))
990 (defun assoc-if-not (predicate alist &key key)
992 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
993 If KEY is supplied, apply it to the CAR of each cons before testing."
994 (let ((predicate (%coerce-callable-to-fun predicate))
995 (key (and key (%coerce-callable-to-fun key))))
997 (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
998 (assoc-guts (not (funcall predicate (caar alist)))))))
1000 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1001 (declare (list alist))
1003 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1005 (when (and testp notp)
1006 (error ":TEST and :TEST-NOT were both supplied."))
1007 (let ((key (and key (%coerce-callable-to-fun key)))
1008 (test (and testp (%coerce-callable-to-fun test)))
1009 (test-not (and notp (%coerce-callable-to-fun test-not))))
1012 (assoc-guts (funcall test item (funcall key (cdar alist))))
1013 (assoc-guts (funcall test item (cdar alist)))))
1016 (assoc-guts (not (funcall test-not item
1017 (funcall key (cdar alist)))))
1018 (assoc-guts (not (funcall test-not item (cdar alist))))))
1021 (assoc-guts (eql item (funcall key (cdar alist))))
1022 (assoc-guts (eql item (cdar alist))))))))
1024 (defun rassoc-if (predicate alist &key key)
1026 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1027 is supplied, apply it to the CDR of each cons before testing."
1028 (let ((predicate (%coerce-callable-to-fun predicate))
1029 (key (and key (%coerce-callable-to-fun key))))
1031 (assoc-guts (funcall predicate (funcall key (cdar alist))))
1032 (assoc-guts (funcall predicate (cdar alist))))))
1034 (defun rassoc-if-not (predicate alist &key key)
1036 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1037 If KEY is supplied, apply it to the CDR of each cons before testing."
1038 (let ((predicate (%coerce-callable-to-fun predicate))
1039 (key (and key (%coerce-callable-to-fun key))))
1041 (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
1042 (assoc-guts (not (funcall predicate (cdar alist)))))))
1044 ;;;; mapping functions
1046 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1047 ;;; MAPL, MAPLIST, and MAPCON
1049 ;;; Map the designated function over the arglists in the appropriate
1050 ;;; way. It is done when any of the arglists runs out. Until then, it
1051 ;;; CDRs down the arglists calling the function and accumulating
1052 ;;; results as desired.
1053 (defun map1 (fun-designator original-arglists accumulate take-car)
1054 (let ((fun (%coerce-callable-to-fun fun-designator)))
1055 (let* ((arglists (copy-list original-arglists))
1056 (ret-list (list nil))
1060 ((dolist (x arglists nil) (if (null x) (return t)))
1063 (car original-arglists)))
1064 (do ((l arglists (cdr l)))
1066 (push (if take-car (caar l) (car l)) args)
1067 (setf (car l) (cdar l)))
1068 (setq res (apply fun (nreverse args)))
1070 (:nconc (setq temp (last (nconc temp res))))
1071 (:list (rplacd temp (list res))
1072 (setq temp (cdr temp))))))))
1074 (defun mapc (function list &rest more-lists)
1076 "Apply FUNCTION to successive elements of lists. Return the second argument."
1077 (map1 function (cons list more-lists) nil t))
1079 (defun mapcar (function list &rest more-lists)
1081 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1083 (map1 function (cons list more-lists) :list t))
1085 (defun mapcan (function list &rest more-lists)
1087 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1089 (map1 function (cons list more-lists) :nconc t))
1091 (defun mapl (function list &rest more-lists)
1093 "Apply FUNCTION to successive CDRs of list. Return NIL."
1094 (map1 function (cons list more-lists) nil nil))
1096 (defun maplist (function list &rest more-lists)
1098 "Apply FUNCTION to successive CDRs of list. Return list of results."
1099 (map1 function (cons list more-lists) :list nil))
1101 (defun mapcon (function list &rest more-lists)
1103 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1104 (map1 function (cons list more-lists) :nconc nil))