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")
17 ;;;; KLUDGE: comment from CMU CL, what does it mean?
18 ;;;; NSUBLIS, things at the beginning broken.
21 (declaim (maybe-inline
22 tree-equal list-length nth %setnth nthcdr last make-list append
23 copy-list copy-alist copy-tree revappend nconc nreconc butlast
24 nbutlast ldiff member member-if member-if-not tailp adjoin union
25 nunion intersection nintersection set-difference nset-difference
26 set-exclusive-or nset-exclusive-or subsetp acons pairlis assoc
27 assoc-if assoc-if-not rassoc rassoc-if rassoc-if-not subst subst-if
28 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
30 ;;; These functions perform basic list operations:
31 (defun car (list) #!+sb-doc "Returns the 1st object in a list." (car list))
33 #!+sb-doc "Returns all but the first object in a list."
35 (defun cadr (list) #!+sb-doc "Returns the 2nd object in a list." (cadr list))
36 (defun cdar (list) #!+sb-doc "Returns the cdr of the 1st sublist." (cdar list))
37 (defun caar (list) #!+sb-doc "Returns the car of the 1st sublist." (caar list))
39 #!+sb-doc "Returns all but the 1st two objects of a list."
42 #!+sb-doc "Returns the 1st object in the cddr of a list."
45 #!+sb-doc "Returns the 1st object in the cadr of a list."
48 #!+sb-doc "Returns the 1st object in the caar of a list."
51 #!+sb-doc "Returns the cdr of the caar of a list."
54 #!+sb-doc "Returns the cdr of the cdar of a list."
57 #!+sb-doc "Returns the cdr of the cddr of a list."
60 #!+sb-doc "Returns the car of the cdar of a list."
63 #!+sb-doc "Returns the cdr of the cadr of a list."
66 #!+sb-doc "Returns the car of the caaar of a list."
69 #!+sb-doc "Returns the car of the caadr of a list."
72 #!+sb-doc "Returns the car of the caddr of a list."
75 #!+sb-doc "Returns the car of the cdddr of a list."
78 #!+sb-doc "Returns the cdr of the cdddr of a list."
81 #!+sb-doc "Returns the cdr of the caaar of a list."
84 #!+sb-doc "Returns the cdr of the cdaar of a list."
87 #!+sb-doc "Returns the cdr of the cddar of a list."
90 #!+sb-doc "Returns the car of the cadar of a list."
93 #!+sb-doc "Returns the car of the cdaar of a list."
96 #!+sb-doc "Returns the car of the cdadr of a list."
99 #!+sb-doc "Returns the car of the cddar of a list."
102 #!+sb-doc "Returns the cdr of the caadr of a list."
105 #!+sb-doc "Returns the cdr of the cadar of a list."
108 #!+sb-doc "Returns the cdr of the caddr of a list."
111 #!+sb-doc "Returns the cdr of the cdadr of a list."
113 (defun cons (se1 se2)
114 #!+sb-doc "Returns a list with se1 as the car and se2 as the cdr."
117 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
119 (defun tree-equal-test-not (x y test-not)
122 (tree-equal-test-not (car x) (car y) test-not)
123 (tree-equal-test-not (cdr x) (cdr y) test-not)))
125 ((not (funcall test-not x y)) t)
128 (defun tree-equal-test (x y test)
131 (tree-equal-test (car x) (car y) test)
132 (tree-equal-test (cdr x) (cdr y) test)))
134 ((funcall test x y) t)
137 (defun tree-equal (x y &key (test #'eql) test-not)
139 "Returns T if X and Y are isomorphic trees with identical leaves."
141 (tree-equal-test-not x y test-not)
142 (tree-equal-test x y test)))
146 "The recommended way to test for the end of a list. True if Object is nil,
147 false if Object is a cons, and an error for any other types of arguments."
150 (defun list-length (list)
152 "Returns the length of the given List, or Nil if the List is circular."
157 (declare (fixnum n) (list y z))
158 (when (endp y) (return n))
159 (when (endp (cdr y)) (return (+ n 1)))
160 (when (and (eq y z) (> n 0)) (return nil))))
164 "Returns the nth object in a list where the car is the zero-th element."
165 (car (nthcdr n list)))
169 "Returns the 1st object in a list or NIL if the list is empty."
172 "Returns the 2nd object in a list or NIL if there is no 2nd object."
176 "Returns the 3rd object in a list or NIL if there is no 3rd object."
180 "Returns the 4th object in a list or NIL if there is no 4th object."
184 "Returns the 5th object in a list or NIL if there is no 5th object."
188 "Returns the 6th object in a list or NIL if there is no 6th object."
189 (cadr (cddddr list)))
190 (defun seventh (list)
192 "Returns the 7th object in a list or NIL if there is no 7th object."
193 (caddr (cddddr list)))
196 "Returns the 8th object in a list or NIL if there is no 8th object."
197 (cadddr (cddddr list)))
200 "Returns the 9th object in a list or NIL if there is no 9th object."
201 (car (cddddr (cddddr list))))
204 "Returns the 10th object in a list or NIL if there is no 10th object."
205 (cadr (cddddr (cddddr list))))
208 "Means the same as the cdr of a list."
211 (defun nthcdr (n list)
212 (declare (type index n))
214 "Performs the cdr function n times on a list."
216 (result list (cdr result)))
217 ((not (plusp i)) result)
218 (declare (type index i))))
220 (defun last (list &optional (n 1))
222 "Returns the last N conses (not the last element!) of a list."
223 (declare (type index n))
224 (do ((checked-list list (cdr checked-list))
226 (index 0 (1+ index)))
227 ((atom checked-list) returned-list)
228 (declare (type index index))
230 (pop returned-list))))
232 (defun list (&rest args)
234 "Returns constructs and returns a list of its arguments."
237 ;;; List* is done the same as list, except that the last cons is made a
240 (defun list* (arg &rest others)
242 "Returns a list of the arguments with last cons a dotted pair"
243 (cond ((atom others) arg)
244 ((atom (cdr others)) (cons arg (car others)))
245 (t (do ((x others (cdr x)))
246 ((null (cddr x)) (rplacd x (cadr x))))
249 (defun make-list (size &key initial-element)
251 "Constructs a list with size elements each set to value"
252 (declare (type index size))
253 (do ((count size (1- count))
254 (result '() (cons initial-element result)))
255 ((zerop count) result)
256 (declare (type index count))))
258 ;;; The outer loop finds the first non-null list and the result is started.
259 ;;; The remaining lists in the arguments are tacked to the end of the result
260 ;;; using splice which cdr's down the end of the new list
262 (defun append (&rest lists)
264 "Construct a new list by concatenating the list arguments"
265 (do ((top lists (cdr top))) ;;Cdr to first non-null list.
267 (cond ((null (car top))) ; Nil -> Keep looping
268 ((not (consp (car top))) ; Non cons
270 (error "~S is not a list." (car top))
275 (car top) ;;Special case.
276 (let* ((result (cons (caar top) '()))
278 (do ((x (cdar top) (cdr x))) ;;Copy first list
281 (cdr (rplacd splice (cons (car x) ()) ))) )
282 (do ((y (cdr top) (cdr y))) ;;Copy rest of lists.
284 (setq splice (rplacd splice (car y)))
287 (do ((x (car y) (cdr x))) ;;Inner copy loop.
291 (cdr (rplacd splice (cons (car x) ())))))
292 (error "~S is not a list." (car y)))))))))))
294 ;;; list copying functions
296 ;;; The list is copied correctly even if the list is not terminated by ()
297 ;;; The new list is built by cdr'ing splice which is always at the tail
300 (defun copy-list (list)
302 "Returns a new list EQUAL but not EQ to list"
305 (let ((result (list (car list))))
306 (do ((x (cdr list) (cdr x))
308 (cdr (rplacd splice (cons (car x) '() ))) ))
314 (defun copy-alist (alist)
316 "Returns a new association list equal to alist, constructed in space"
320 (cons (if (atom (car alist))
322 (cons (caar alist) (cdar alist)) )
324 (do ((x (cdr alist) (cdr x))
330 (cons (caar x) (cdar x)))
332 ;; Non-null terminated alist done here.
338 (defun copy-tree (object)
340 "Recursively copy trees of conses."
342 (cons (copy-tree (car object)) (copy-tree (cdr object)))
345 ;;; more commonly-used list functions
347 (defun revappend (x y)
349 "Returns (append (reverse x) y)"
350 (do ((top x (cdr top))
351 (result y (cons (car top) result)))
352 ((endp top) result)))
354 ;;; NCONC finds the first non-null list, so it can make splice point to a cons.
355 ;;; After finding the first cons element, it holds it in a result variable
356 ;;; while running down successive elements tacking them together. While
357 ;;; tacking lists together, if we encounter a null list, we set the previous
358 ;;; list's last cdr to nil just in case it wasn't already nil, and it could
359 ;;; have been dotted while the null list was the last argument to NCONC. The
360 ;;; manipulation of splice (that is starting it out on a first cons, setting
361 ;;; LAST of splice, and setting splice to ele) inherently handles (nconc x x),
362 ;;; and it avoids running down the last argument to NCONC which allows the last
363 ;;; argument to be circular.
364 (defun nconc (&rest lists)
366 "Concatenates the lists given as arguments (by changing them)"
367 (do ((top lists (cdr top)))
369 (let ((top-of-top (car top)))
372 (let* ((result top-of-top)
374 (do ((elements (cdr top) (cdr elements)))
376 (let ((ele (car elements)))
378 (cons (rplacd (last splice) ele)
380 (null (rplacd (last splice) nil))
381 (atom (if (cdr elements)
382 (error "Argument is not a list -- ~S." ele)
383 (rplacd (last splice) ele)))
384 (t (error "Argument is not a list -- ~S." ele)))))
389 (error "Argument is not a list -- ~S." top-of-top)
390 (return top-of-top)))
391 (t (error "Argument is not a list -- ~S." top-of-top))))))
395 "Returns (nconc (nreverse x) y)"
396 (do ((1st (cdr x) (if (atom 1st) 1st (cdr 1st)))
397 (2nd x 1st) ;2nd follows first down the list.
398 (3rd y 2nd)) ;3rd follows 2nd down the list.
402 (defun butlast (list &optional (n 1))
404 "Return a new list the same as LIST without the last N conses.
405 List must not be circular."
406 (declare (list list) (type index n))
407 (let ((length (do ((list list (cdr list))
409 ((atom list) (1- i)))))
410 (declare (type index length))
412 (do* ((top (cdr list) (cdr top))
413 (result (list (car list)))
415 (count length (1- count)))
417 (declare (type index count))
418 (setq splice (cdr (rplacd splice (list (car top)))))))))
420 (defun nbutlast (list &optional (n 1))
422 "Modifies List to remove the last N conses. List must not be circular."
423 (declare (list list) (type index n))
424 (let ((length (do ((list list (cdr list))
426 ((atom list) (1- i)))))
427 (declare (type index length))
429 (do ((1st (cdr list) (cdr 1st))
431 (count length (1- count)))
435 (declare (type index count))))))
437 (defun ldiff (list object)
438 "Returns a new list, whose elements are those of List that appear before
439 Object. If Object is not a tail of List, a copy of List is returned.
440 List must be a proper list or a dotted list."
441 (do* ((list list (cdr list))
445 (if (eql list object)
447 (progn (rplacd splice list) (cdr result))))
448 (if (eql list object)
449 (return (cdr result))
450 (setq splice (cdr (rplacd splice (list (car list))))))))
452 ;;; Functions to alter list structure
456 "Changes the car of x to y and returns the new x."
461 "Changes the cdr of x to y and returns the new x."
464 ;;; The following are for use by SETF.
466 (defun %rplaca (x val) (rplaca x val) val)
468 (defun %rplacd (x val) (rplacd x val) val)
470 (defun %setnth (n list newval)
471 (declare (type index n))
473 "Sets the Nth element of List (zero based) to Newval."
474 (do ((count n (1- count))
475 (list list (cdr list)))
477 (error "~S is too large an index for SETF of NTH." n))
478 (declare (fixnum count))
483 ;;;; :KEY arg optimization to save funcall of IDENTITY
485 ;;; APPLY-KEY saves us a function call sometimes.
486 ;;; This is not wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
487 ;;; because this is used in seq.lisp and sort.lisp.
488 (defmacro apply-key (key element)
490 (funcall ,key ,element)
493 (defun identity (thing)
495 "Returns what was passed to it."
498 (defun complement (function)
500 "Builds a new function that returns T whenever FUNCTION returns NIL and
501 NIL whenever FUNCTION returns T."
502 #'(lambda (&optional (arg0 nil arg0-p) (arg1 nil arg1-p) (arg2 nil arg2-p)
504 (not (cond (more-args (apply function arg0 arg1 arg2 more-args))
505 (arg2-p (funcall function arg0 arg1 arg2))
506 (arg1-p (funcall function arg0 arg1))
507 (arg0-p (funcall function arg0))
508 (t (funcall function))))))
510 (defun constantly (value &optional (val1 nil val1-p) (val2 nil val2-p)
513 "Builds a function that always returns VALUE, and posisbly MORE-VALUES."
515 (let ((list (list* value val1 val2 more-values)))
517 (declare (optimize-interface (speed 3) (safety 0)))
518 (values-list list))))
521 (declare (optimize-interface (speed 3) (safety 0)))
522 (values value val1 val2)))
525 (declare (optimize-interface (speed 3) (safety 0)))
526 (values value val1)))
529 (declare (optimize-interface (speed 3) (safety 0)))
532 ;;;; macros for (&key (key #'identity) (test #'eql testp) (test-not nil notp)).
534 ;;; Use these with the following keyword args:
535 (defmacro with-set-keys (funcall)
536 `(cond ((and testp notp) (error "Test and test-not both supplied."))
537 (notp ,(append funcall '(:key key :test-not test-not)))
538 (t ,(append funcall '(:key key :test test)))))
540 (defmacro satisfies-the-test (item elt)
541 (let ((key-tmp (gensym)))
542 `(let ((,key-tmp (apply-key key ,elt)))
543 (cond (testp (funcall test ,item ,key-tmp))
544 (notp (not (funcall test-not ,item ,key-tmp)))
545 (t (funcall test ,item ,key-tmp))))))
547 ;;;; substitution of expressions
549 (defun subst (new old tree &key key (test #'eql testp) (test-not nil notp))
551 "Substitutes new for subtrees matching old."
552 (labels ((s (subtree)
553 (cond ((satisfies-the-test old 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 subst-if (new test tree &key key)
565 "Substitutes new for subtrees for which test is true."
566 (labels ((s (subtree)
567 (cond ((funcall test (apply-key key subtree)) new)
568 ((atom subtree) subtree)
569 (t (let ((car (s (car subtree)))
570 (cdr (s (cdr subtree))))
571 (if (and (eq car (car subtree))
572 (eq cdr (cdr subtree)))
577 (defun subst-if-not (new test tree &key key)
579 "Substitutes new for subtrees for which test is false."
580 (labels ((s (subtree)
581 (cond ((not (funcall test (apply-key key subtree))) new)
582 ((atom subtree) subtree)
583 (t (let ((car (s (car subtree)))
584 (cdr (s (cdr subtree))))
585 (if (and (eq car (car subtree))
586 (eq cdr (cdr subtree)))
591 (defun nsubst (new old tree &key key (test #'eql testp) (test-not nil notp))
593 "Substitutes new for subtrees matching old."
594 (labels ((s (subtree)
595 (cond ((satisfies-the-test old subtree) new)
596 ((atom subtree) subtree)
597 (t (do* ((last nil subtree)
598 (subtree subtree (Cdr subtree)))
600 (if (satisfies-the-test old subtree)
601 (setf (cdr last) new)))
602 (if (satisfies-the-test old subtree)
603 (return (setf (cdr last) new))
604 (setf (car subtree) (s (car subtree)))))
608 (defun nsubst-if (new test tree &key key)
610 "Substitutes new for subtrees of tree for which test is true."
611 (labels ((s (subtree)
612 (cond ((funcall test (apply-key key subtree)) new)
613 ((atom subtree) subtree)
614 (t (do* ((last nil subtree)
615 (subtree subtree (Cdr subtree)))
617 (if (funcall test (apply-key key subtree))
618 (setf (cdr last) new)))
619 (if (funcall test (apply-key key subtree))
620 (return (setf (cdr last) new))
621 (setf (car subtree) (s (car subtree)))))
625 (defun nsubst-if-not (new test tree &key key)
627 "Substitutes new for subtrees of tree for which test is false."
628 (labels ((s (subtree)
629 (cond ((not (funcall test (apply-key key subtree))) new)
630 ((atom subtree) subtree)
631 (t (do* ((last nil subtree)
632 (subtree subtree (Cdr subtree)))
634 (if (not (funcall test (apply-key key subtree)))
635 (setf (cdr last) new)))
636 (if (not (funcall test (apply-key key subtree)))
637 (return (setf (cdr last) new))
638 (setf (car subtree) (s (car subtree)))))
642 (defun sublis (alist tree &key key (test #'eql) (test-not nil notp))
644 "Substitutes from alist into tree nondestructively."
645 (declare (inline assoc))
646 (labels ((s (subtree)
647 (let* ((key-val (apply-key key subtree))
649 (assoc key-val alist :test-not test-not)
650 (assoc key-val alist :test test))))
651 (cond (assoc (cdr assoc))
652 ((atom subtree) subtree)
653 (t (let ((car (s (car subtree)))
654 (cdr (s (cdr subtree))))
655 (if (and (eq car (car subtreE))
656 (eq cdr (cdr subtree)))
658 (cons car cdr))))))))
661 ;;; These are in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
662 ;;; because they can be referenced in inline expansions.
663 (defmacro nsublis-macro ()
664 (let ((key-tmp (gensym)))
665 `(let ((,key-tmp (apply-key key subtree)))
667 (assoc ,key-tmp alist :test-not test-not)
668 (assoc ,key-tmp alist :test test)))))
670 (defun nsublis (alist tree &key key (test #'eql) (test-not nil notp))
672 "Substitutes new for subtrees matching old."
673 (declare (inline assoc))
675 (labels ((s (subtree)
676 (cond ((Setq temp (nsublis-macro))
678 ((atom subtree) subtree)
679 (t (do* ((last nil subtree)
680 (subtree subtree (Cdr subtree)))
682 (if (setq temp (nsublis-macro))
683 (setf (cdr last) (cdr temp))))
684 (if (setq temp (nsublis-macro))
685 (return (setf (Cdr last) (Cdr temp)))
686 (setf (car subtree) (s (car subtree)))))
690 ;;;; functions for using lists as sets
692 (defun member (item list &key key (test #'eql testp) (test-not nil notp))
694 "Returns tail of list beginning with first element satisfying EQLity,
695 :test, or :test-not with a given item."
696 (do ((list list (cdr list)))
698 (let ((car (car list)))
699 (if (satisfies-the-test item car)
702 (defun member-if (test list &key key)
704 "Returns tail of list beginning with first element satisfying test(element)"
705 (do ((list list (Cdr list)))
707 (if (funcall test (apply-key key (car list)))
710 (defun member-if-not (test list &key key)
712 "Returns tail of list beginning with first element not satisfying test(el)"
713 (do ((list list (cdr list)))
715 (if (not (funcall test (apply-key key (car list))))
718 (defun tailp (object list)
720 "Returns true if Object is the same as some tail of List, otherwise
721 returns false. List must be a proper list or a dotted list."
722 (do ((list list (cdr list)))
723 ((atom list) (eql list object))
724 (if (eql object list)
727 (defun adjoin (item list &key key (test #'eql) (test-not nil notp))
729 "Add item to list unless it is already a member"
730 (declare (inline member))
731 (if (let ((key-val (apply-key key item)))
733 (member key-val list :test-not test-not :key key)
734 (member key-val list :test test :key key)))
738 ;;; This function assumes list2 is the result, adding to it from list1 as
739 ;;; necessary. List2 must initialize the result value, so the call to MEMBER
740 ;;; will apply the test to the elements from list1 and list2 in the correct
742 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
744 "Returns the union of list1 and list2."
745 (declare (inline member))
746 (when (and testp notp) (error "Test and test-not both supplied."))
749 (unless (with-set-keys (member (apply-key key elt) list2))
753 ;;; Destination and source are setf-able and many-evaluable. Sets the source
754 ;;; to the cdr, and "conses" the 1st elt of source to destination.
756 ;;; FIXME: needs a more mnemonic name
757 (defmacro steve-splice (source destination)
758 `(let ((temp ,source))
759 (setf ,source (cdr ,source)
760 (cdr temp) ,destination
763 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
765 "Destructively returns the union list1 and list2."
766 (declare (inline member))
768 (error "Test and test-not both supplied."))
773 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
774 (steve-splice list1 res)
775 (setf list1 (cdr list1))))
778 (defun intersection (list1 list2 &key key
779 (test #'eql testp) (test-not nil notp))
781 "Returns the intersection of list1 and list2."
782 (declare (inline member))
784 (error "Test and test-not both supplied."))
787 (if (with-set-keys (member (apply-key key elt) list2))
791 (defun nintersection (list1 list2 &key key
792 (test #'eql testp) (test-not nil notp))
794 "Destructively returns the intersection of list1 and list2."
795 (declare (inline member))
797 (error "Test and test-not both supplied."))
800 (do () ((endp list1))
801 (if (with-set-keys (member (apply-key key (car list1)) list2))
802 (steve-splice list1 res)
803 (setq list1 (Cdr list1))))
806 (defun set-difference (list1 list2 &key key
807 (test #'eql testp) (test-not nil notp))
809 "Returns the elements of list1 which are not in list2."
810 (declare (inline member))
812 (error "Test and test-not both supplied."))
817 (if (not (with-set-keys (member (apply-key key elt) list2)))
821 (defun nset-difference (list1 list2 &key key
822 (test #'eql testp) (test-not nil notp))
824 "Destructively returns the elements of list1 which are not in list2."
825 (declare (inline member))
827 (error "Test and test-not both supplied."))
830 (do () ((endp list1))
831 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
832 (steve-splice list1 res)
833 (setq list1 (cdr list1))))
836 (defun set-exclusive-or (list1 list2 &key key
837 (test #'eql testp) (test-not nil notp))
839 "Returns new list of elements appearing exactly once in list1 and list2."
840 (declare (inline member))
843 (unless (with-set-keys (member (apply-key key elt) list2))
844 (setq result (cons elt result))))
846 (unless (with-set-keys (member (apply-key key elt) list1))
847 (setq result (cons elt result))))
850 ;;; The outer loop examines list1 while the inner loop examines list2. If an
851 ;;; element is found in list2 "equal" to the element in list1, both are
852 ;;; spliced out. When the end of list1 is reached, what is left of list2 is
853 ;;; tacked onto what is left of list1. The splicing operation ensures that
854 ;;; the correct operation is performed depending on whether splice is at the
855 ;;; top of the list or not
857 (defun nset-exclusive-or (list1 list2 &key (test #'eql) (test-not nil notp)
860 "Destructively return a list with elements which appear but once in list1
869 (rplacd splicex list2))
871 (do ((y list2 (cdr y))
873 ((endp y) (setq splicex x))
874 (cond ((let ((key-val-x (apply-key key (car x)))
875 (key-val-y (apply-key key (Car y))))
877 (not (funcall test-not key-val-x key-val-y))
878 (funcall test key-val-x key-val-y)))
881 (rplacd splicex (cdr x)))
884 (rplacd splicey (cdr y)))
885 (return ())) ; assume lists are really sets
886 (t (setq splicey y))))))
888 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
890 "Returns T if every element in list1 is also in list2."
891 (declare (inline member))
893 (unless (with-set-keys (member (apply-key key elt) list2))
894 (return-from subsetp nil)))
897 ;;; functions that operate on association lists
899 (defun acons (key datum alist)
901 "Construct a new alist by adding the pair (key . datum) to alist"
902 (cons (cons key datum) alist))
904 (defun pairlis (keys data &optional (alist '()))
906 "Construct an association list from keys and data (adding to alist)"
907 (do ((x keys (cdr x))
909 ((and (endp x) (endp y)) alist)
910 (if (or (endp x) (endp y))
911 (error "The lists of keys and data are of unequal length."))
912 (setq alist (acons (car x) (car y) alist))))
914 ;;; This is in the run-time environment (i.e. not wrapped in
915 ;;; EVAL-WHEN (COMPILE EVAL)) because these guys can be inline
917 (defmacro assoc-guts (test-guy)
918 `(do ((alist alist (cdr alist)))
920 ;; FIXME: would be clearer as (WHEN (AND ..) ..)
922 (if ,test-guy (return (car alist))))))
924 (defun assoc (item alist &key key test test-not)
926 "Returns the cons in ALIST whose car is equal (by a given test or EQL) to
928 ;; FIXME: Shouldn't there be a check for existence of both TEST and TEST-NOT?
931 (assoc-guts (funcall test item (funcall key (caar alist))))
932 (assoc-guts (funcall test item (caar alist)))))
935 (assoc-guts (not (funcall test-not item
936 (funcall key (caar alist)))))
937 (assoc-guts (not (funcall test-not item (caar alist))))))
940 (assoc-guts (eql item (funcall key (caar alist))))
941 (assoc-guts (eql item (caar alist)))))))
943 (defun assoc-if (predicate alist &key key)
945 "Returns the first cons in alist whose car satisfies the Predicate. If
946 key is supplied, apply it to the car of each cons before testing."
948 (assoc-guts (funcall predicate (funcall key (caar alist))))
949 (assoc-guts (funcall predicate (caar alist)))))
951 (defun assoc-if-not (predicate alist &key key)
953 "Returns the first cons in alist whose car does not satisfiy the Predicate.
954 If key is supplied, apply it to the car of each cons before testing."
956 (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
957 (assoc-guts (not (funcall predicate (caar alist))))))
959 (defun rassoc (item alist &key key test test-not)
960 (declare (list alist))
962 "Returns the cons in alist whose cdr is equal (by a given test or EQL) to
966 (assoc-guts (funcall test item (funcall key (cdar alist))))
967 (assoc-guts (funcall test item (cdar alist)))))
970 (assoc-guts (not (funcall test-not item
971 (funcall key (cdar alist)))))
972 (assoc-guts (not (funcall test-not item (cdar alist))))))
975 (assoc-guts (eql item (funcall key (cdar alist))))
976 (assoc-guts (eql item (cdar alist)))))))
978 (defun rassoc-if (predicate alist &key key)
980 "Returns the first cons in alist whose cdr satisfies the Predicate. If key
981 is supplied, apply it to the cdr of each cons before testing."
983 (assoc-guts (funcall predicate (funcall key (cdar alist))))
984 (assoc-guts (funcall predicate (cdar alist)))))
986 (defun rassoc-if-not (predicate alist &key key)
988 "Returns the first cons in alist whose cdr does not satisfy the Predicate.
989 If key is supplied, apply it to the cdr of each cons before testing."
991 (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
992 (assoc-guts (not (funcall predicate (cdar alist))))))
994 ;;;; mapping functions
996 (defun map1 (function original-arglists accumulate take-car)
998 "This function is called by mapc, mapcar, mapcan, mapl, maplist, and mapcon.
999 It Maps function over the arglists in the appropriate way. It is done when any
1000 of the arglists runs out. Until then, it CDRs down the arglists calling the
1001 function and accumulating results as desired."
1003 (let* ((arglists (copy-list original-arglists))
1004 (ret-list (list nil))
1008 ((dolist (x arglists nil) (if (null x) (return t)))
1011 (car original-arglists)))
1012 (do ((l arglists (cdr l)))
1014 (push (if take-car (caar l) (car l)) args)
1015 (setf (car l) (cdar l)))
1016 (setq res (apply function (nreverse args)))
1018 (:nconc (setq temp (last (nconc temp res))))
1019 (:list (rplacd temp (list res))
1020 (setq temp (cdr temp)))))))
1022 (defun mapc (function list &rest more-lists)
1024 "Applies fn to successive elements of lists, returns its second argument."
1025 (map1 function (cons list more-lists) nil t))
1027 (defun mapcar (function list &rest more-lists)
1029 "Applies fn to successive elements of list, returns list of results."
1030 (map1 function (cons list more-lists) :list t))
1032 (defun mapcan (function list &rest more-lists)
1034 "Applies fn to successive elements of list, returns NCONC of results."
1035 (map1 function (cons list more-lists) :nconc t))
1037 (defun mapl (function list &rest more-lists)
1039 "Applies fn to successive CDRs of list, returns ()."
1040 (map1 function (cons list more-lists) nil nil))
1042 (defun maplist (function list &rest more-lists)
1044 "Applies fn to successive CDRs of list, returns list of results."
1045 (map1 function (cons list more-lists) :list nil))
1047 (defun mapcon (function list &rest more-lists)
1049 "Applies fn to successive CDRs of lists, returns NCONC of results."
1050 (map1 function (cons list more-lists) :nconc nil))