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 "Returns the 1st object in a list." (car list))
29 #!+sb-doc "Returns all but the first object in a list."
31 (defun cadr (list) #!+sb-doc "Returns the 2nd object in a list." (cadr list))
32 (defun cdar (list) #!+sb-doc "Returns the cdr of the 1st sublist." (cdar list))
33 (defun caar (list) #!+sb-doc "Returns the car of the 1st sublist." (caar list))
35 #!+sb-doc "Returns all but the 1st two objects of a list."
38 #!+sb-doc "Returns the 1st object in the cddr of a list."
41 #!+sb-doc "Returns the 1st object in the cadr of a list."
44 #!+sb-doc "Returns the 1st object in the caar of a list."
47 #!+sb-doc "Returns the cdr of the caar of a list."
50 #!+sb-doc "Returns the cdr of the cdar of a list."
53 #!+sb-doc "Returns the cdr of the cddr of a list."
56 #!+sb-doc "Returns the car of the cdar of a list."
59 #!+sb-doc "Returns the cdr of the cadr of a list."
62 #!+sb-doc "Returns the car of the caaar of a list."
65 #!+sb-doc "Returns the car of the caadr of a list."
68 #!+sb-doc "Returns the car of the caddr of a list."
71 #!+sb-doc "Returns the car of the cdddr of a list."
74 #!+sb-doc "Returns the cdr of the cdddr of a list."
77 #!+sb-doc "Returns the cdr of the caaar of a list."
80 #!+sb-doc "Returns the cdr of the cdaar of a list."
83 #!+sb-doc "Returns the cdr of the cddar of a list."
86 #!+sb-doc "Returns the car of the cadar of a list."
89 #!+sb-doc "Returns the car of the cdaar of a list."
92 #!+sb-doc "Returns the car of the cdadr of a list."
95 #!+sb-doc "Returns the car of the cddar of a list."
98 #!+sb-doc "Returns the cdr of the caadr of a list."
101 #!+sb-doc "Returns the cdr of the cadar of a list."
104 #!+sb-doc "Returns the cdr of the caddr of a list."
107 #!+sb-doc "Returns the cdr of the cdadr of a list."
109 (defun cons (se1 se2)
110 #!+sb-doc "Returns 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)
118 (tree-equal-test-not (car x) (car y) test-not)
119 (tree-equal-test-not (cdr x) (cdr y) test-not)))
121 ((not (funcall test-not x y)) t)
124 (defun tree-equal-test (x y test)
127 (tree-equal-test (car x) (car y) test)
128 (tree-equal-test (cdr x) (cdr y) test)))
130 ((funcall test x y) t)
133 (defun tree-equal (x y &key (test #'eql) test-not)
135 "Returns T if X and Y are isomorphic trees with identical leaves."
137 (tree-equal-test-not x y test-not)
138 (tree-equal-test x y test)))
142 "The recommended way to test for the end of a list. True if Object is nil,
143 false if Object is a cons, and an error for any other types of arguments."
146 (defun list-length (list)
148 "Returns the length of the given List, or Nil if the List is circular."
153 (declare (fixnum n) (list y z))
154 (when (endp y) (return n))
155 (when (endp (cdr y)) (return (+ n 1)))
156 (when (and (eq y z) (> n 0)) (return nil))))
160 "Returns the nth object in a list where the car is the zero-th element."
161 (car (nthcdr n list)))
165 "Returns the 1st object in a list or NIL if the list is empty."
168 "Returns the 2nd object in a list or NIL if there is no 2nd object."
172 "Returns the 3rd object in a list or NIL if there is no 3rd object."
176 "Returns the 4th object in a list or NIL if there is no 4th object."
180 "Returns the 5th object in a list or NIL if there is no 5th object."
184 "Returns the 6th object in a list or NIL if there is no 6th object."
185 (cadr (cddddr list)))
186 (defun seventh (list)
188 "Returns the 7th object in a list or NIL if there is no 7th object."
189 (caddr (cddddr list)))
192 "Returns the 8th object in a list or NIL if there is no 8th object."
193 (cadddr (cddddr list)))
196 "Returns the 9th object in a list or NIL if there is no 9th object."
197 (car (cddddr (cddddr list))))
200 "Returns the 10th object in a list or NIL if there is no 10th object."
201 (cadr (cddddr (cddddr list))))
204 "Means the same as the cdr of a list."
207 (defun nthcdr (n list)
208 (declare (type index n))
210 "Performs the cdr function n times on a list."
212 (result list (cdr result)))
213 ((not (plusp i)) result)
214 (declare (type index i))))
216 (defun last (list &optional (n 1))
218 "Returns the last N conses (not the last element!) of a list."
219 (declare (type index n))
220 (do ((checked-list list (cdr checked-list))
222 (index 0 (1+ index)))
223 ((atom checked-list) returned-list)
224 (declare (type index index))
226 (pop returned-list))))
228 (defun list (&rest args)
230 "Returns constructs and returns a list of its arguments."
233 ;;; List* is done the same as list, except that the last cons is made a
236 (defun list* (arg &rest others)
238 "Returns a list of the arguments with last cons a dotted pair"
239 (cond ((atom others) arg)
240 ((atom (cdr others)) (cons arg (car others)))
241 (t (do ((x others (cdr x)))
242 ((null (cddr x)) (rplacd x (cadr x))))
245 (defun make-list (size &key initial-element)
247 "Constructs a list with size elements each set to value"
248 (declare (type index size))
249 (do ((count size (1- count))
250 (result '() (cons initial-element result)))
251 ((zerop count) result)
252 (declare (type index count))))
254 ;;; The outer loop finds the first non-null list and the result is started.
255 ;;; The remaining lists in the arguments are tacked to the end of the result
256 ;;; using splice which cdr's down the end of the new list
258 (defun append (&rest lists)
260 "Construct a new list by concatenating the list arguments"
261 (do ((top lists (cdr top))) ;;Cdr to first non-null list.
263 (cond ((null (car top))) ; Nil -> Keep looping
264 ((not (consp (car top))) ; Non cons
266 (error "~S is not a list." (car top))
271 (car top) ;;Special case.
272 (let* ((result (cons (caar top) '()))
274 (do ((x (cdar top) (cdr x))) ;;Copy first list
277 (cdr (rplacd splice (cons (car x) ()) ))) )
278 (do ((y (cdr top) (cdr y))) ;;Copy rest of lists.
280 (setq splice (rplacd splice (car y)))
283 (do ((x (car y) (cdr x))) ;;Inner copy loop.
287 (cdr (rplacd splice (cons (car x) ())))))
288 (error "~S is not a list." (car y)))))))))))
290 ;;; list copying functions
292 (defun copy-list (list)
294 "Returns a new list which is EQUAL to LIST."
295 ;; The list is copied correctly even if the list is not terminated
296 ;; by NIL. The new list is built by CDR'ing SPLICE which is always
297 ;; at the tail of the new list.
300 (let ((result (list (car list))))
301 (do ((x (cdr list) (cdr x))
303 (cdr (rplacd splice (cons (car x) '() ))) ))
309 (defun copy-alist (alist)
311 "Returns a new association list which is EQUAL to ALIST."
315 (cons (if (atom (car alist))
317 (cons (caar alist) (cdar alist)) )
319 (do ((x (cdr alist) (cdr x))
325 (cons (caar x) (cdar x)))
327 ;; Non-null terminated alist done here.
333 (defun copy-tree (object)
335 "Recursively copy trees of conses."
337 (cons (copy-tree (car object)) (copy-tree (cdr object)))
340 ;;; more commonly-used list functions
342 (defun revappend (x y)
344 "Returns (append (reverse x) y)"
345 (do ((top x (cdr top))
346 (result y (cons (car top) result)))
347 ((endp top) result)))
349 ;;; NCONC finds the first non-null list, so it can make splice point
350 ;;; to a cons. After finding the first cons element, it holds it in a
351 ;;; result variable while running down successive elements tacking
352 ;;; them together. While tacking lists together, if we encounter a
353 ;;; null list, we set the previous list's last cdr to nil just in case
354 ;;; it wasn't already nil, and it could have been dotted while the
355 ;;; null list was the last argument to NCONC. The manipulation of
356 ;;; splice (that is starting it out on a first cons, setting LAST of
357 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
358 ;;; and it avoids running down the last argument to NCONC which allows
359 ;;; the last argument to be circular.
360 (defun nconc (&rest lists)
362 "Concatenates the lists given as arguments (by changing them)"
363 (do ((top lists (cdr top)))
365 (let ((top-of-top (car top)))
368 (let* ((result top-of-top)
370 (do ((elements (cdr top) (cdr elements)))
372 (let ((ele (car elements)))
374 (cons (rplacd (last splice) ele)
376 (null (rplacd (last splice) nil))
377 (atom (if (cdr elements)
378 (error "Argument is not a list -- ~S." ele)
379 (rplacd (last splice) ele)))
380 (t (error "Argument is not a list -- ~S." ele)))))
385 (error "Argument is not a list -- ~S." top-of-top)
386 (return top-of-top)))
387 (t (error "Argument is not a list -- ~S." top-of-top))))))
391 "Returns (nconc (nreverse x) y)"
392 (do ((1st (cdr x) (if (atom 1st) 1st (cdr 1st)))
393 (2nd x 1st) ;2nd follows first down the list.
394 (3rd y 2nd)) ;3rd follows 2nd down the list.
398 (flet (;; Return the number of conses at the head of the
399 ;; possibly-improper list LIST. (Or if LIST is circular, you
402 (do ((in-list list (cdr in-list))
403 (result 0 (1+ result)))
406 (declare (type index result)))))
407 (declare (ftype (function (t) index) count-conses))
408 (defun butlast (list &optional (n 1))
409 (let* ((n-conses-in-list (count-conses list))
410 (n-remaining-to-copy (- n-conses-in-list n)))
411 (declare (type fixnum n-remaining-to-copy))
412 (when (plusp n-remaining-to-copy)
413 (do* ((result (list (first list)))
414 (rest (rest list) (rest rest))
416 ((zerop (decf n-remaining-to-copy))
420 (list (first rest))))))))
421 (defun nbutlast (list &optional (n 1))
422 (let ((n-conses-in-list (count-conses list)))
423 (unless (< n-conses-in-list n)
424 (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
428 (defun ldiff (list object)
429 "Return a new list, whose elements are those of LIST that appear before
430 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
431 LIST must be a proper list or a dotted list."
432 (do* ((list list (cdr list))
436 (if (eql list object)
438 (progn (rplacd splice list) (cdr result))))
439 (if (eql list object)
440 (return (cdr result))
441 (setq splice (cdr (rplacd splice (list (car list))))))))
443 ;;;; functions to alter list structure
447 "Change the CAR of X to Y and return the new X."
452 "Change the CDR of X to Y and return the new X."
455 ;;; The following are for use by SETF.
457 (defun %rplaca (x val) (rplaca x val) val)
459 (defun %rplacd (x val) (rplacd x val) val)
461 ;;; Set the Nth element of LIST to NEWVAL.
462 (defun %setnth (n list newval)
463 (declare (type index n))
464 (do ((count n (1- count))
465 (list list (cdr list)))
467 (error "~S is too large an index for SETF of NTH." n))
468 (declare (fixnum count))
473 ;;;; :KEY arg optimization to save funcall of IDENTITY
475 ;;; APPLY-KEY saves us a function call sometimes.
476 ;;; This is not wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
477 ;;; because this is used in seq.lisp and sort.lisp.
478 (defmacro apply-key (key element)
480 (funcall ,key ,element)
483 (defun identity (thing)
485 "This function simply returns what was passed to it."
488 (defun complement (function)
490 "Return a new function that returns T whenever FUNCTION returns NIL and
491 NIL whenever FUNCTION returns non-NIL."
492 (lambda (&optional (arg0 nil arg0-p) (arg1 nil arg1-p) (arg2 nil arg2-p)
494 (not (cond (more-args (apply function arg0 arg1 arg2 more-args))
495 (arg2-p (funcall function arg0 arg1 arg2))
496 (arg1-p (funcall function arg0 arg1))
497 (arg0-p (funcall function arg0))
498 (t (funcall function))))))
500 (defun constantly (value)
502 "Return a function that always returns VALUE."
504 ;; KLUDGE: This declaration is a hack to make the closure ignore
505 ;; all its arguments without consing a &REST list or anything.
506 ;; Perhaps once DYNAMIC-EXTENT is implemented we won't need to
507 ;; screw around with this kind of thing. -- WHN 2001-04-06
508 (declare (optimize (speed 3) (safety 0)))
511 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
513 ;;; Use these with the following &KEY args:
514 (defmacro with-set-keys (funcall)
515 `(cond ((and testp notp) (error ":TEST and :TEST-NOT were both supplied."))
516 (notp ,(append funcall '(:key key :test-not test-not)))
517 (t ,(append funcall '(:key key :test test)))))
519 (defmacro satisfies-the-test (item elt)
520 (let ((key-tmp (gensym)))
521 `(let ((,key-tmp (apply-key key ,elt)))
522 (cond (testp (funcall test ,item ,key-tmp))
523 (notp (not (funcall test-not ,item ,key-tmp)))
524 (t (funcall test ,item ,key-tmp))))))
526 ;;;; substitution of expressions
528 (defun subst (new old tree &key key (test #'eql testp) (test-not nil notp))
530 "Substitutes new for subtrees matching old."
531 (labels ((s (subtree)
532 (cond ((satisfies-the-test old subtree) new)
533 ((atom subtree) subtree)
534 (t (let ((car (s (car subtree)))
535 (cdr (s (cdr subtree))))
536 (if (and (eq car (car subtree))
537 (eq cdr (cdr subtree)))
542 (defun subst-if (new test tree &key key)
544 "Substitutes new for subtrees for which test is true."
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 (labels ((s (subtree)
560 (cond ((not (funcall test (apply-key key subtree))) new)
561 ((atom subtree) subtree)
562 (t (let ((car (s (car subtree)))
563 (cdr (s (cdr subtree))))
564 (if (and (eq car (car subtree))
565 (eq cdr (cdr subtree)))
570 (defun nsubst (new old tree &key key (test #'eql testp) (test-not nil notp))
572 "Substitutes new for subtrees matching old."
573 (labels ((s (subtree)
574 (cond ((satisfies-the-test old subtree) new)
575 ((atom subtree) subtree)
576 (t (do* ((last nil subtree)
577 (subtree subtree (Cdr subtree)))
579 (if (satisfies-the-test old subtree)
580 (setf (cdr last) new)))
581 (if (satisfies-the-test old subtree)
582 (return (setf (cdr last) new))
583 (setf (car subtree) (s (car subtree)))))
587 (defun nsubst-if (new test tree &key key)
589 "Substitutes new for subtrees of tree for which test is true."
590 (labels ((s (subtree)
591 (cond ((funcall test (apply-key key subtree)) new)
592 ((atom subtree) subtree)
593 (t (do* ((last nil subtree)
594 (subtree subtree (Cdr subtree)))
596 (if (funcall test (apply-key key subtree))
597 (setf (cdr last) new)))
598 (if (funcall test (apply-key key subtree))
599 (return (setf (cdr last) new))
600 (setf (car subtree) (s (car subtree)))))
604 (defun nsubst-if-not (new test tree &key key)
606 "Substitutes new for subtrees of tree for which test is false."
607 (labels ((s (subtree)
608 (cond ((not (funcall test (apply-key key subtree))) new)
609 ((atom subtree) subtree)
610 (t (do* ((last nil subtree)
611 (subtree subtree (Cdr subtree)))
613 (if (not (funcall test (apply-key key subtree)))
614 (setf (cdr last) new)))
615 (if (not (funcall test (apply-key key subtree)))
616 (return (setf (cdr last) new))
617 (setf (car subtree) (s (car subtree)))))
621 (defun sublis (alist tree &key key (test #'eql) (test-not nil notp))
623 "Substitutes from alist into tree nondestructively."
624 (declare (inline assoc))
625 (labels ((s (subtree)
626 (let* ((key-val (apply-key key subtree))
628 (assoc key-val alist :test-not test-not)
629 (assoc key-val alist :test test))))
630 (cond (assoc (cdr assoc))
631 ((atom subtree) subtree)
632 (t (let ((car (s (car subtree)))
633 (cdr (s (cdr subtree))))
634 (if (and (eq car (car subtreE))
635 (eq cdr (cdr subtree)))
637 (cons car cdr))))))))
640 ;;; These are in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
641 ;;; because they can be referenced in inline expansions.
642 (defmacro nsublis-macro ()
643 (let ((key-tmp (gensym)))
644 `(let ((,key-tmp (apply-key key subtree)))
646 (assoc ,key-tmp alist :test-not test-not)
647 (assoc ,key-tmp alist :test test)))))
649 (defun nsublis (alist tree &key key (test #'eql) (test-not nil notp))
651 "Substitutes new for subtrees matching old."
652 (declare (inline assoc))
654 (labels ((s (subtree)
655 (cond ((Setq temp (nsublis-macro))
657 ((atom subtree) subtree)
658 (t (do* ((last nil subtree)
659 (subtree subtree (Cdr subtree)))
661 (if (setq temp (nsublis-macro))
662 (setf (cdr last) (cdr temp))))
663 (if (setq temp (nsublis-macro))
664 (return (setf (Cdr last) (Cdr temp)))
665 (setf (car subtree) (s (car subtree)))))
669 ;;;; functions for using lists as sets
671 (defun member (item list &key key (test #'eql testp) (test-not nil notp))
673 "Returns tail of list beginning with first element satisfying EQLity,
674 :TEST, or :TEST-NOT with a given item."
675 (do ((list list (cdr list)))
677 (let ((car (car list)))
678 (if (satisfies-the-test item car)
681 (defun member-if (test list &key key)
683 "Return tail of LIST beginning with first element satisfying TEST."
684 (do ((list list (Cdr list)))
686 (if (funcall test (apply-key key (car list)))
689 (defun member-if-not (test list &key key)
691 "Return tail of LIST beginning with first element not satisfying TEST."
692 (do ((list list (cdr list)))
694 (if (not (funcall test (apply-key key (car list))))
697 (defun tailp (object list)
699 "Return true if OBJECT is the same as some tail of LIST, otherwise
700 returns false. LIST must be a proper list or a dotted list."
701 (do ((list list (cdr list)))
702 ((atom list) (eql list object))
703 (if (eql object list)
706 (defun adjoin (item list &key key (test #'eql) (test-not nil notp))
708 "Add ITEM to LIST unless it is already a member"
709 (declare (inline member))
710 (if (let ((key-val (apply-key key item)))
712 (member key-val list :test-not test-not :key key)
713 (member key-val list :test test :key key)))
717 ;;; This function assumes list2 is the result, adding to it from list1 as
718 ;;; necessary. List2 must initialize the result value, so the call to MEMBER
719 ;;; will apply the test to the elements from list1 and list2 in the correct
721 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
723 "Return the union of LIST1 and LIST2."
724 (declare (inline member))
725 (when (and testp notp) (error "Test and test-not both supplied."))
728 (unless (with-set-keys (member (apply-key key elt) list2))
732 ;;; Destination and source are SETF-able and many-evaluable. Set the
733 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
735 ;;; FIXME: needs a more mnemonic name
736 (defmacro steve-splice (source destination)
737 `(let ((temp ,source))
738 (setf ,source (cdr ,source)
739 (cdr temp) ,destination
742 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
744 "Destructively return the union of LIST1 and LIST2."
745 (declare (inline member))
747 (error ":TEST and :TEST-NOT were both supplied."))
752 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
753 (steve-splice list1 res)
754 (setf list1 (cdr list1))))
757 (defun intersection (list1 list2 &key key
758 (test #'eql testp) (test-not nil notp))
760 "Return the intersection of LIST1 and LIST2."
761 (declare (inline member))
763 (error "Test and test-not both supplied."))
766 (if (with-set-keys (member (apply-key key elt) list2))
770 (defun nintersection (list1 list2 &key key
771 (test #'eql testp) (test-not nil notp))
773 "Destructively return the intersection of LIST1 and LIST2."
774 (declare (inline member))
776 (error "Test and test-not both supplied."))
779 (do () ((endp list1))
780 (if (with-set-keys (member (apply-key key (car list1)) list2))
781 (steve-splice list1 res)
782 (setq list1 (Cdr list1))))
785 (defun set-difference (list1 list2 &key key
786 (test #'eql testp) (test-not nil notp))
788 "Return the elements of LIST1 which are not in LIST2."
789 (declare (inline member))
791 (error "Test and test-not both supplied."))
796 (if (not (with-set-keys (member (apply-key key elt) list2)))
800 (defun nset-difference (list1 list2 &key key
801 (test #'eql testp) (test-not nil notp))
803 "Destructively return the elements of LIST1 which are not in LIST2."
804 (declare (inline member))
806 (error "Test and test-not both supplied."))
809 (do () ((endp list1))
810 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
811 (steve-splice list1 res)
812 (setq list1 (cdr list1))))
815 (defun set-exclusive-or (list1 list2 &key key
816 (test #'eql testp) (test-not nil notp))
818 "Return new list of elements appearing exactly once in LIST1 and LIST2."
819 (declare (inline member))
822 (unless (with-set-keys (member (apply-key key elt) list2))
823 (setq result (cons elt result))))
825 (unless (with-set-keys (member (apply-key key elt) list1))
826 (setq result (cons elt result))))
829 ;;; The outer loop examines list1 while the inner loop examines list2.
830 ;;; If an element is found in list2 "equal" to the element in list1,
831 ;;; both are spliced out. When the end of list1 is reached, what is
832 ;;; left of list2 is tacked onto what is left of list1. The splicing
833 ;;; operation ensures that the correct operation is performed
834 ;;; depending on whether splice is at the top of the list or not
835 (defun nset-exclusive-or (list1 list2 &key (test #'eql) (test-not nil notp)
838 "Destructively return a list with elements which appear but once in LIST1
847 (rplacd splicex list2))
849 (do ((y list2 (cdr y))
851 ((endp y) (setq splicex x))
852 (cond ((let ((key-val-x (apply-key key (car x)))
853 (key-val-y (apply-key key (Car y))))
855 (not (funcall test-not key-val-x key-val-y))
856 (funcall test key-val-x key-val-y)))
859 (rplacd splicex (cdr x)))
862 (rplacd splicey (cdr y)))
863 (return ())) ; assume lists are really sets
864 (t (setq splicey y))))))
866 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
868 "Return T if every element in LIST1 is also in LIST2."
869 (declare (inline member))
871 (unless (with-set-keys (member (apply-key key elt) list2))
872 (return-from subsetp nil)))
875 ;;; functions that operate on association lists
877 (defun acons (key datum alist)
879 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
880 (cons (cons key datum) alist))
882 (defun pairlis (keys data &optional (alist '()))
884 "Construct an association list from KEYS and DATA (adding to ALIST)."
885 (do ((x keys (cdr x))
887 ((and (endp x) (endp y)) alist)
888 (if (or (endp x) (endp y))
889 (error "The lists of keys and data are of unequal length."))
890 (setq alist (acons (car x) (car y) alist))))
892 ;;; This is in the run-time environment (i.e. not wrapped in
893 ;;; EVAL-WHEN (COMPILE EVAL)) because these guys can be inline
895 (defmacro assoc-guts (test-guy)
896 `(do ((alist alist (cdr alist)))
898 ;; FIXME: would be clearer as (WHEN (AND ..) ..)
900 (if ,test-guy (return (car alist))))))
902 (defun assoc (item alist &key key test test-not)
904 "Returns the cons in ALIST whose car is equal (by a given test or EQL) to
906 ;; FIXME: Shouldn't there be a check for existence of both TEST and TEST-NOT?
909 (assoc-guts (funcall test item (funcall key (caar alist))))
910 (assoc-guts (funcall test item (caar alist)))))
913 (assoc-guts (not (funcall test-not item
914 (funcall key (caar alist)))))
915 (assoc-guts (not (funcall test-not item (caar alist))))))
918 (assoc-guts (eql item (funcall key (caar alist))))
919 (assoc-guts (eql item (caar alist)))))))
921 (defun assoc-if (predicate alist &key key)
923 "Returns the first cons in alist whose car satisfies the Predicate. If
924 key is supplied, apply it to the car of each cons before testing."
926 (assoc-guts (funcall predicate (funcall key (caar alist))))
927 (assoc-guts (funcall predicate (caar alist)))))
929 (defun assoc-if-not (predicate alist &key key)
931 "Returns the first cons in ALIST whose car does not satisfy the PREDICATE.
932 If KEY is supplied, apply it to the car of each cons before testing."
934 (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
935 (assoc-guts (not (funcall predicate (caar alist))))))
937 (defun rassoc (item alist &key key test test-not)
938 (declare (list alist))
940 "Returns the cons in ALIST whose cdr is equal (by a given test or EQL) to
944 (assoc-guts (funcall test item (funcall key (cdar alist))))
945 (assoc-guts (funcall test item (cdar alist)))))
948 (assoc-guts (not (funcall test-not item
949 (funcall key (cdar alist)))))
950 (assoc-guts (not (funcall test-not item (cdar alist))))))
953 (assoc-guts (eql item (funcall key (cdar alist))))
954 (assoc-guts (eql item (cdar alist)))))))
956 (defun rassoc-if (predicate alist &key key)
958 "Returns the first cons in alist whose cdr satisfies the Predicate. If key
959 is supplied, apply it to the cdr of each cons before testing."
961 (assoc-guts (funcall predicate (funcall key (cdar alist))))
962 (assoc-guts (funcall predicate (cdar alist)))))
964 (defun rassoc-if-not (predicate alist &key key)
966 "Returns the first cons in alist whose cdr does not satisfy the Predicate.
967 If key is supplied, apply it to the cdr of each cons before testing."
969 (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
970 (assoc-guts (not (funcall predicate (cdar alist))))))
972 ;;;; mapping functions
974 (defun map1 (function original-arglists accumulate take-car)
976 "This function is called by mapc, mapcar, mapcan, mapl, maplist, and mapcon.
977 It Maps function over the arglists in the appropriate way. It is done when any
978 of the arglists runs out. Until then, it CDRs down the arglists calling the
979 function and accumulating results as desired."
981 (let* ((arglists (copy-list original-arglists))
982 (ret-list (list nil))
986 ((dolist (x arglists nil) (if (null x) (return t)))
989 (car original-arglists)))
990 (do ((l arglists (cdr l)))
992 (push (if take-car (caar l) (car l)) args)
993 (setf (car l) (cdar l)))
994 (setq res (apply function (nreverse args)))
996 (:nconc (setq temp (last (nconc temp res))))
997 (:list (rplacd temp (list res))
998 (setq temp (cdr temp)))))))
1000 (defun mapc (function list &rest more-lists)
1002 "Applies fn to successive elements of lists, returns its second argument."
1003 (map1 function (cons list more-lists) nil t))
1005 (defun mapcar (function list &rest more-lists)
1007 "Applies fn to successive elements of list, returns list of results."
1008 (map1 function (cons list more-lists) :list t))
1010 (defun mapcan (function list &rest more-lists)
1012 "Applies fn to successive elements of list, returns NCONC of results."
1013 (map1 function (cons list more-lists) :nconc t))
1015 (defun mapl (function list &rest more-lists)
1017 "Applies fn to successive CDRs of list, returns ()."
1018 (map1 function (cons list more-lists) nil nil))
1020 (defun maplist (function list &rest more-lists)
1022 "Applies fn to successive CDRs of list, returns list of results."
1023 (map1 function (cons list more-lists) :list nil))
1025 (defun mapcon (function list &rest more-lists)
1027 "Applies fn to successive CDRs of lists, returns NCONC of results."
1028 (map1 function (cons list more-lists) :nconc nil))