Google
 

Trailing-Edge - PDP-10 Archives - clisp - clisp/upsala/list.clisp
There are no other files named list.clisp in the archive.
;;; This is a -*-Lisp-*- file.
;;;
;;; **********************************************************************
;;; This code was written as part of the Spice Lisp project at
;;; Carnegie-Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of Spice Lisp, please contact
;;; Scott Fahlman (FAHLMAN@CMUC). 
;;; **********************************************************************
;;;
;;; Functions to implement lists for Spice Lisp.
;;; Written by Joe Ginder and Carl Ebeling.
;;; Rewritten and currently maintained by Skef Wholey.
;;;
;;; The list functions are part of the standard Spice Lisp environment.
;;;
;;; **********************************************************************
;;;

(in-package 'lisp)

(export '(caar cadr cdar cddr caaar caadr cadar caddr cdaar cdadr cddar cdddr
	  caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr cdaaar cdaadr
	  cdadar cdaddr cddaar cddadr cdddar cddddr tree-equal endp list-length
	  nth first second third fourth fifth sixth seventh eighth ninth tenth
	  rest nthcdr last list list* make-list append copy-list copy-alist
	  copy-tree revappend nconc nreconc butlast nbutlast ldiff subst
	  subst-if subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis
	  member member-if member-if-not tailp adjoin union nunion intersection
	  nintersection set-difference nset-difference set-exclusive-or
	  nset-exclusive-or subsetp acons pairlis assoc assoc-if assoc-if-not
	  rassoc rassoc-if rassoc-if-not mapcar maplist mapc mapl mapcan mapcon
	  memq delq))

;;; These functions perform basic list operations:


(defun caar (x) (car (car x)))
(defun cadr (x) (car (cdr x)))
(defun cdar (x) (cdr (car x)))
(defun cddr (x) (cdr (cdr x)))
(defun caaar (x) (car (car (car x))))
(defun caadr (x) (car (car (cdr x))))
(defun cadar (x) (car (cdr (car x))))
(defun caddr (x) (car (cdr (cdr x))))
(defun cdaar (x) (cdr (car (car x))))
(defun cdadr (x) (cdr (car (cdr x))))
(defun cddar (x) (cdr (cdr (car x))))
(defun cdddr (x) (cdr (cdr (cdr x))))
(defun caaaar (x) (car (car (car (car x)))))
(defun caaadr (x) (car (car (car (cdr x)))))
(defun caadar (x) (car (car (cdr (car x)))))
(defun caaddr (x) (car (car (cdr (cdr x)))))
(defun cadaar (x) (car (cdr (car (car x)))))
(defun cadadr (x) (car (cdr (car (cdr x)))))
(defun caddar (x) (car (cdr (cdr (car x)))))
(defun cadddr (x) (car (cdr (cdr (cdr x)))))
(defun cdaaar (x) (cdr (car (car (car x)))))
(defun cdaadr (x) (cdr (car (car (cdr x)))))
(defun cdadar (x) (cdr (car (cdr (car x)))))
(defun cdaddr (x) (cdr (car (cdr (cdr x)))))
(defun cddaar (x) (cdr (cdr (car (car x)))))
(defun cddadr (x) (cdr (cdr (car (cdr x)))))
(defun cdddar (x) (cdr (cdr (cdr (car x)))))
(defun cddddr (x) (cdr (cdr (cdr (cdr x)))))


;20; removed CAR, CDR, CONS, which are in the kernel
(defun tree-equal (x y &key (test #'eql) test-not)
  "Returns T if X and Y are isomorphic trees with identical leaves."
  (if test-not
      (tree-equal-test-not x y test-not)
      (tree-equal-test x y test)))

(defun tree-equal-test-not (x y test-not)
  (cond ((not (funcall test-not x y)) t)
	((consp x)
	 (and (consp y)
	      (tree-equal-test-not (car x) (car y) test-not)
	      (tree-equal-test-not (cdr x) (cdr y) test-not)))
	(t ())))

(defun tree-equal-test (x y test)
  (cond ((funcall test x y) t)
	((consp x)
	 (and (consp y)
	      (tree-equal-test (car x) (car y) test)
	      (tree-equal-test (cdr x) (cdr y) test)))
	(t ())))

(defun endp (object)
  "The recommended way to test for the end of a list.  True if Object is nil,
   false if Object is a cons, and an error for any other types of arguments."
  (cond ((null object) t)
	((consp object) nil)
	(t (error "~S is not a list." object))))

(defun list-length (list)
  "Returns the length of the given List, or Nil if the List is circular."
  (do ((n 0 (+ n 2))
       (y list (cddr y))
       (z list (cdr z)))
      (())
    (when (endp y) (return n))
    (when (endp (cdr y)) (return (+ n 1)))
    (when (and (eq y z) (> n 0)) (return nil))))

(defun nth (n list)
  (declare (fixnum n))
  "Returns Nth (zero based) element of list."
  (if (< n 0)
      (error "~S is an illegal N for NTH." n)
      (do ((count n (1- count)))
	  ((zerop count) (car list))
	(declare (fixnum count))
	(if (atom (cdr list))
	    (return '())
	    (setq list (cdr list))))))

(defun first (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CAR."
  (car list))
(defun second (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CADR."
  (cadr list))
(defun third (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CADDR."
  (caddr list))
(defun fourth (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CADDDR."
  (cadddr list))
(defun fifth (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CAR of CDDDDR."
  (car (cddddr list)))
(defun sixth (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CADR of CDDDDR."
  (cadr (cddddr list)))
(defun seventh (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CADDR of CDDDDR."
  (caddr (cddddr list)))
(defun eighth (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CADDDR of CDDDDR."
  (cadddr (cddddr list)))
(defun ninth (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CAR of CDDDDR of CDDDDR."
  (car (cddddr (cddddr list))))
(defun tenth (list)
  "A convenient function for accessing particular elements of a list.  Same as
  CADR of CDDDDR of CDDDDR."
  (cadr (cddddr (cddddr list))))

(defun rest (list)
  "The same as CDR."
  (cdr list))

(defun nthcdr (n list)
  (declare (fixnum n))
  "Returns the Nth (zero based) cdr of list."
  (if (< n 0)
      (error "~S is an illegal N for NTHCDR." n)
      (do ((count n (1- count)))
	  ((zerop count) list)
	(declare (fixnum count))
	(if (atom list)
	    (return '())
	    (setq list (cdr list))))))

(defun last (list)
  "Returns the last CONS in list."
  (if (atom list) '()
      (do ((x list (cdr x)))
	  ((atom (cdr x)) x))))


(defun list (&rest args)
  "Returns a list of the arguments."
  args)

;;; List* is done the same as list, except that the last cons is made a
;;; dotted pair

(defun list* (arg &rest others)
  "Returns a list of the arguments with last cons a dotted pair"
  (cond ((atom others) arg)
	((atom (cdr others)) (cons arg (car others)))
	(t (do ((x others (cdr x)))
	       ((null (cddr x)) (rplacd x (cadr x))))
	   (cons arg others))))

(defun make-list (size &key initial-element)
  (declare (fixnum size))
  "Constructs a list with size elements each set to value"
  (if (< size 0) (error "~S is an illegal size for MAKE-LIST." size)
      (do ((count size (1- count))
	   (result '() (cons initial-element result)))
	  ((zerop count) result)
	(declare (fixnum count)))))
;;; The outer loop finds the first non-null list and the result is started.
;;; The remaining lists in the arguments are tacked to the end of the result
;;; using splice which cdr's down the end of the new list

(defun append (&rest lists)
  "Construct a new list by concatenating the list arguments"
  (do ((top lists (cdr top)))	 ;;Cdr to first non-null list.
      ((atom top) '())
    (cond ((null (car top)))				; Nil -> Keep looping
	  ((not (consp (car top)))			; Non cons
	   (if (cdr top)
	       (error "~S is not a list." (car top))
	       (return (car top))))
	  (t						; Start appending
	   (return
	     (if (atom (cdr top))
		 (car top)    ;;Special case.
		 (let* ((result (cons (caar top) '())) 
			(splice result))
		   (do ((x (cdar top) (cdr x)))  ;;Copy first list
		       ((atom x))
		     (setq splice
			   (cdr (rplacd splice (cons (car x) ()) ))) )
		   (do ((y (cdr top) (cdr y)))	 ;;Copy rest of lists.
		       ((atom (cdr y))
			(setq splice (rplacd splice (car y)))
			result)
		     (if (listp (car y))
			 (do ((x (car y) (cdr x)))   ;;Inner copy loop.
			     ((atom x))
			   (setq
			    splice
			    (cdr (rplacd splice (cons (car x) ())))))
			 (error "~S is not a list." (car y)))))))))))
  
;;; List Copying Functions


;;; The list is copied correctly even if the list is not terminated by ()
;;; The new list is built by cdr'ing splice which is always at the tail
;;; of the new list

(defun copy-list (list)
  "Returns a new list EQUAL but not EQ to list"
  (if (atom list)
      (if list
	  (error "~S is not a list." list))
      (let ((result (cons (car list) '()) ))
	(do ((x (cdr list) (cdr x))
	     (splice result
		     (cdr (rplacd splice (cons (car x) '() ))) ))
	    ((atom x) (unless (null x)
			      (rplacd splice x))
		      result)))))

(defun copy-alist (a-list)
  "Returns a new association list equal to a-list, constructed in space"
  (if (atom a-list)
      (if a-list
	  (error "~S is not a list." a-list))
      (let ((result
	     (cons (if (atom (car a-list))
		       (car a-list)
		       (cons (caar a-list) (cdar a-list)) )
		   '() )))	      
	(do ((x (cdr a-list) (cdr x))
	     (splice result
		     (cdr (rplacd splice
				  (cons
				   (if (atom (car x)) 
				       (car x)
				       (cons (caar x) (cdar x)))
				   '() ))) ))
;;; Non-null terminated a-list done here.
	    ((atom x) (unless (null x)
			      (rplacd splice x))
		      result)))))

(defun copy-tree (object)
  "Copy-Tree recursively copys trees of conses."
  (cond ((not (consp object)) object)
	(T (cons (copy-tree (car object)) (copy-tree (cdr object)))) ))
;;; More Commonly-used List Functions

(defun revappend (x y)
  "Returns (append (reverse x) y)"
  (do ((top x (cdr top))
       (result y (cons (car top) result)))
      ((atom top) result)))

;;; The outer loop finds the first non-null list.  Starting with this list
;;; the inner loop tacks on the remaining lists in the arguments

(defun nconc (&rest lists)
  "Concatenates the lists given as arguments (by changing them)"
  (do ((top lists (cdr top)))	 ;Find first non-null list in lists.
      ((null top) top)
    (unless (atom (car top))
	    (return (do ((x (cdr top) (cdr x))
			 (splice (car top)))
			((atom x) (car top))
		      (cond ((atom (car x))
			     (rplacd (last splice) (car x)))
			    (t
			     (rplacd (last splice) (car x))
			     (setq splice (car x))) ))) )))

(defun nreconc (x y)
  "Returns (nconc (nreverse x) y)"
  (do ((1st (cdr x) (if (atom 1st) 1st (cdr 1st)))
       (2nd x 1st)		;2nd follows first down the list.
       (3rd y 2nd))		;3rd follows 2nd down the list.
      ((atom 2nd) 3rd)
    (rplacd 2nd 3rd)))
(defun butlast (list &optional (n 1))
  (declare (fixnum n))
  "Returns a new list the same as List without the N last elements."
  (if (< n 0) (setq n 0))
  (let ((length (1- (length (the list list)))))
    (declare (fixnum length))
    (if (< length n) ()
	(do* ((top (cdr list) (cdr top))
	      (result (list (car list)))
	      (splice result)
	      (count length (1- count)))
	     ((= count n) result)
	  (setq splice (cdr (rplacd splice (list (car top)))))))))

(defun nbutlast (list &optional (n 1))
  (declare (fixnum n))
  "Modifies List to remove the last N elements."
  (if (< n 0) (setq n 0))
  (let ((length (1- (length (the list list)))))
    (declare (fixnum length))
    (if (< length n) ()
	(do ((1st (cdr list) (cdr 1st))
	     (2nd list 1st)
	     (count length (1- count)))
	    ((= count n)
	     (rplacd 2nd ())
	     list)))))

(defun ldiff (list sublist)
  "Returns a new list, whose elements are those of List that appear before
   Sublist.  If Sublist is not a tail of List, a copy of List is returned."
  (do* ((list list (cdr list))
	(result (list ()))
	(splice result))
       ((or (null list) (eq list sublist)) (cdr result))
    (setq splice (cdr (rplacd splice (list (car list)))))))
;;; Functions to alter list structure

;20; removed rplaca and rplacd, which are in the kernel

;;; The following are for use by SETF.

(defun %rplaca (x val) (rplaca x val) val)

(defun %rplacd (x val) (rplacd x val) val)

(defun %setnth (n list newval)
  (declare (fixnum n))
  "Sets the Nth element of List (zero based) to Newval."
  (if (< n 0)
      (error "~S is an illegal N for SETF of NTH." n)
      (do ((count n (1- count)))
	  ((zerop count) (rplaca list newval) newval)
	(declare (fixnum count))
	(if (atom (cdr list))
	    (error "~S is too large an index for SETF of NTH." n)
	    (setq list (cdr list))))))
;;;20; The use of labels is removed in our version since closures are not
;;; yet implemented in the compiler.  Also this is probably faster.

;;; Use this with the following keyword args:
;;;  (&key (key #'identity) (test #'eql testp) (test-not nil notp))

(defmacro with-set-keys (funcall)
  `(cond ((and testp notp) (error "Test and test-not both supplied."))
	 (notp ,(append funcall '(:key key :test-not test-not)))
	 (t ,(append funcall '(:key key :test test)))))

;;; Works with the above keylist.  We do three clauses so that if only test-not
;;; is supplied, then we don't test eql.  In each case, the args should be 
;;; multiply evaluable.

(Defmacro elements-match-p (elt1 elt2)
  `(or (and testp
	    (funcall test (funcall key ,elt1) (funcall key ,elt2)))
       (and notp
	    (not (funcall test-not (funcall key ,elt1) (funcall key ,elt2))))
       (eql (funcall key ,elt1) (funcall key ,elt2))))

(defmacro satisfies-the-test (item elt)
  `(or (and testp
	    (funcall test ,item (funcall key ,elt)))
       (and notp
	    (not (funcall test-not ,item (funcall key ,elt))))
       (funcall test ,item (funcall key ,elt))))


;;; Substitution of expressions

(defun subst (new old tree &key (key #'identity)
		  (test #'eql testp) (test-not nil notp))
  "Substitutes New for subtrees matching Old, and returns the modified copy
  of Tree."
  (subst* new old tree key test testp test-not notp))

(defun subst* (new old tree key test testp test-not notp)
  (cond ((satisfies-the-test old tree) new)
	((atom tree) tree)
	(t (let ((car (subst* new old (car tree) key test testp test-not notp))
		 (cdr (subst*
		       new old (cdr tree) key test testp test-not notp)))
	     (if (and (eq car (car tree))
		      (eq cdr (cdr tree)))
		 tree
		 (cons car cdr))))))

(defun subst-if (new test tree &key (key #'identity))
  "Substitutes New for subtrees for which test is true, and returns the
  modified copy of Tree."
  (subst-if* new test tree key))

(defun subst-if* (new test tree key)
  (cond ((funcall test (funcall key tree)) new)
	((atom tree) tree)
	(t (let ((car (subst-if* new test (car tree) key))
		 (cdr (subst-if* new test (cdr tree) key)))
	     (if (and (eq car (car tree))
		      (eq cdr (cdr tree)))
		 tree
		 (cons car cdr))))))

(defun subst-if-not (new test tree &key (key #'identity))
  "Substitutes New for subtrees for which test is false, and returns the
  modified copy of Tree."
  (subst-if-not* new test tree key))

(defun subst-if-not* (new test tree key)
  (cond ((not (funcall test (funcall key tree))) new)
	((atom tree) tree)
	(t (let ((car (subst-if-not* new test (car tree) key))
		 (cdr (subst-if-not* new test (cdr tree) key)))
	     (if (and (eq car (car tree))
		      (eq cdr (cdr tree)))
		 tree
		 (cons car cdr))))))

(defun nsubst (new old tree &key (key #'identity)
		  (test #'eql testp) (test-not nil notp))
  "Substitutes New for subtrees matching Old, and returns the destructively
  modified copy of Tree."
  (nsubst* new old tree key test testp test-not notp))

(defun nsubst* (new old tree key test testp test-not notp)
  (cond ((satisfies-the-test old tree) new)
	((atom tree) tree)
	(t (do* ((last nil tree)
		 (tree tree (cdr tree)))
		((atom tree)
		 (if (satisfies-the-test old tree)
		     (setf (cdr last) new)))
	     (if (satisfies-the-test old tree)
		 (return (setf (cdr last) new))
		 (setf (car tree)
		       (nsubst*
			new old (car tree) key test testp test-not notp))))
	   tree)))

(defun nsubst-if (new test tree &key (key #'identity))
  "Substitutes New for subtrees of Tree for which Test is true, and
  returns the destructively modified copy of Tree."
  (nsubst-if* new test tree key))

(defun nsubst-if* (new test tree key)
  (cond ((funcall test (funcall key tree)) new)
	((atom tree) tree)
	(t (do* ((last nil tree)
		 (tree tree (cdr tree)))
		((atom tree)
		 (if (funcall test (funcall key tree))
		     (setf (cdr last) new)))
	     (if (funcall test (funcall key tree))
		 (return (setf (cdr last) new))
		 (setf (car tree) (nsubst-if* new test (car tree) key))))
	   tree)))

(defun nsubst-if-not (new test tree &key (key #'identity))
  "Substitutes New for subtrees of tree for which test is false, and returns
  the destructively modified copy of Tree."
  (nsubst-if-not* new test tree key))

(defun nsubst-if-not* (new test tree key)
  (cond ((not (funcall test (funcall key tree))) new)
	((atom tree) tree)
	(t (do* ((last nil tree)
		 (tree tree (cdr tree)))
		((atom tree)
		 (if (not (funcall test (funcall key tree)))
		     (setf (cdr last) new)))
	     (if (not (funcall test (funcall key tree)))
		 (return (setf (cdr last) new))
		 (setf (car tree) (nsubst-if-not* new test (car tree) key))))
	   tree)))

(defun sublis (alist tree &key (key #'identity)
		     (test #'eql) (test-not nil notp))
  "Substitutes from Alist into Tree nondestructively."
  (sublis* alist tree key test test-not notp))

(defun sublis* (alist tree key test test-not notp)
  (let ((assoc
	 (if notp
	     (assoc (funcall key tree) alist :test-not test-not)
	     (assoc (funcall key tree) alist :test test))))
    (cond (assoc (cdr assoc))
	  ((atom tree) tree)
	  (t (let ((car (sublis* alist (car tree) key test test-not notp))
		   (cdr (sublis* alist (cdr tree) key test test-not notp)))
	       (if (and (eq car (car tree))
			(eq cdr (cdr tree)))
		   tree
		   (cons car cdr)))))))

(eval-when (compile eval)
  (defmacro nsublis-macro ()
    '(if notp
	 (assoc (funcall key tree) alist :test-not test-not)
	 (assoc (funcall key tree) alist :test test))))

(defun nsublis (alist tree &key (key #'identity)
		      (test #'eql) (test-not nil notp))
  "Substitutes from Alist into Tree destructively."
  (nsublis* alist tree key test test-not notp))

(defun nsublis* (alist tree key test test-not notp)
  (let (temp)
    (cond ((setq temp (nsublis-macro))
	   (cdr temp))
	  ((atom tree) tree)
	  (t (do* ((last nil tree)
		   (tree tree (cdr tree)))
		  ((atom tree)
		   (if (setq temp (nsublis-macro))
		       (setf (cdr last) (cdr temp))))
	       (if (setq temp (nsublis-macro))
		   (return (setf (cdr last) (cdr temp)))
		   (setf (car tree)
			 (nsublis* alist (car tree) key test test-not notp))))
	     tree))))
;;; Functions for using lists as sets

(defun member (item list &key (test #'eql) test-not (key #'identity))
  "Returns tail of list beginning with first element satisfying EQUALity,
   :test, or :test-not with a given item."
  (unless (listp list) (error "~S is not a list." list))
  (if test-not
      (do ((list list (cdr list)))
	  ((atom list) ())
	(if (not (funcall test-not item (funcall key (car list))))
	    (return list)))
      (do ((list list (cdr list)))
	  ((atom list) ())
	(if (funcall test item (funcall key (car list)))
	    (return list)))))

(defun member-if (test list &key (key #'identity))
  "Returns tail of list beginning with first element satisfying test(element)"
  (unless (listp list) (error "~S is not a list." list))
  (do ((list list (cdr list)))
      ((atom list) ())
    (if (funcall test (funcall key (car list)))
	(return list))))

(defun member-if-not (test list &key (key #'identity))
  "Returns tail of list beginning with first element not satisfying test(el)"
  (unless (listp list) (error "~S is not a list." list))
  (do ((list list (cdr list)))
      ((atom list) ())
    (if (not (funcall test (funcall key (car list))))
	(return list))))

(defun tailp (sublist list)
  "Returns T if sublist is one of the cons'es in list"
  (do ((x list (cdr x)))
      ((atom x) '())
    (if (eq x sublist) (return T))))

(defun adjoin (item list &key (key #'identity) (test #'eql)
		    (test-not nil notp))
  "Add item to list unless it is already a member."
  (if (if notp (member (funcall key item) list :test-not test-not :key key)
	  (member (funcall key item) list :test test :key key))
      list
      (cons item list)))

(defun union (list1 list2  &key (key #'identity) (test #'eql testp)
		    (test-not nil notp))
  "Returns the union of List1 and List2."
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res list1))
    (dolist (elt list2)
      (if (not (with-set-keys (member (funcall key elt) list1)))
	  (push elt res)))
    res))

;;; Destination and source are setf-able and many-evaluable.
;;; Sets the source to the cdr, and "conses" the 1st elt of source to
;;; destination.

(defmacro steve-splice (source destination)
  `(let ((temp ,source))
     (setf ,source (Cdr ,source)
	   (cdr temp) ,destination
	   ,destination temp)))

(Defun nunion (list1 list2 &key (key #'identity)
		     (test #'eql testp) (test-not nil notp))
  "Returns the union of List1 and List2, which may destroy both lists."
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res list1))
    (do () ((endp list2))
      (if (not (with-set-keys (member (funcall key (car list2)) list1)))
	  (steve-splice list2 res)
	  (Setq list2 (cdr list2))))
    res))
  

(defun intersection (list1 list2  &key (key #'identity)
			   (test #'eql testp) (test-not nil notp))
  "Returns the union of List1 and List2."
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res nil))
    (dolist (elt list1)
      (if (with-set-keys (member (funcall key elt) list2))
	  (push elt res)))
    res))

(Defun nintersection (list1 list2 &key (key #'identity)
			    (test #'eql testp) (test-not nil notp))
  "Returns the intersection of List1 and List2.  List1 may be destroyed."
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res nil))
    (do () ((endp list1))
      (if (with-set-keys (member (funcall key (car list1)) list2))
	  (steve-splice list1 res)
	  (setq list1 (Cdr list1))))
    res))

(defun set-difference (list1 list2 &key (key #'identity)
			     (test #'eql testp) (test-not nil notp))
  "Returns a list containing all the elements of List1 not in List2."
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res nil))
    (dolist (elt list1)
      (if (not (with-set-keys (member (funcall key elt) list2)))
	  (push elt res)))
    res))


(defun nset-difference (list1 list2 &key (key #'identity)
			      (test #'eql testp) (test-not nil notp))
  "Returns a list containing all the elements of List1 not in List2.  List1
  may be destroyed."
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res nil))
    (do () ((endp list1))
      (if (not (with-set-keys (member (funcall key (car list1)) list2)))
	  (steve-splice list1 res)
	  (setq list1 (cdr list1))))
    res))
(defun set-exclusive-or (list1 list2 &key (key #'identity)
			       (test #'eql testp) (test-not nil notp))
  "Returns new list of elements appearing exactly once in List1 and List2."
  (let ((result nil))
    (dolist (elt list1)
      (unless (with-set-keys (member (funcall key elt) list2))
	(setq result (cons elt result))))
    (dolist (elt list2)
      (unless (with-set-keys (member (funcall key elt) list1))
	(setq result (cons elt result))))
    result))


;;; The outer loop examines list1 while the inner loop examines list2. If an
;;; element is found in list2 "equal" to the element in list1, both are
;;; spliced out. When the end of list1 is reached, what is left of list2 is
;;; tacked onto what is left of list1.  The splicing operation ensures that
;;; the correct operation is performed depending on whether splice is at the
;;; top of the list or not

(defun nset-exclusive-or (list1 list2 &key (test #'eql) (test-not nil notp)
				(key #'identity))
  "Return a list with elements which appear but once in List1 and List2."
  (do ((x list1 (cdr x))
       (splicex ()))
      ((endp x)
       (if (null splicex)
	   (setq list1 list2)
	   (rplacd splicex list2))
       list1)
    (do ((y list2 (cdr y))
	 (splicey ()))
	((endp y) (setq splicex x))
      (cond ((if notp
		 (not (funcall test-not (funcall key (car x))
			       (funcall key (Car y))))
		 (funcall test (funcall key (car x)) (funcall key (Car y))))
	     (if (null splicex)
		 (setq list1 (cdr x))
		 (rplacd splicex (cdr x)))
	     (if (null splicey) 
		 (setq list2 (cdr y))
		 (rplacd splicey (cdr y)))
	     (return ()))			; assume lists are really sets
	    (t (setq splicey y)))))))

(defun subsetp (list1 list2 &key (key #'identity)
		      (test #'eql testp) (test-not nil notp))
  "Returns T if every element of List1 appears in ('matches' some element of)
  List2, else returns NIL."
  (dolist (elt list1)
    (unless (with-set-keys (member (funcall key elt) list2))
      (return-from subsetp nil)))
  T)
;;; Functions that operate on association lists

(defun acons (key datum a-list)
  "Construct a new a-list by adding the pair (key . datum) to a-list"
  (cons (cons key datum) a-list))

(defun pairlis (keys data &optional (a-list '()))
  "Construct an association list from keys and data (adding to a-list)"
  (do ((x keys (cdr x))
       (y data (cdr y)))
      ((and (atom x) (atom y)) a-list)
    (if (or (atom x) (atom y)) 
	(error "The lists of keys and data are of unequal length."))
    (setq a-list (acons (car x) (car y) a-list))))

(eval-when (eval)

(defmacro assoc-guts (test)
  `(do ((a-list a-list (cdr a-list)))
       ((atom a-list))
     (if (car a-list)
	 (if ,test (return (car a-list))))))

)

(defun assoc (item a-list &key test test-not key)
  "Returns the cons in A-List whose car is equal (by a given test or EQL) to
   the Item."
  (if key
      (cond (test
	     (assoc-guts (funcall test item (funcall key (car a-list)))))
	    (test-not
	     (assoc-guts (not (funcall test item (funcall key (car a-list))))))
	    (t
	     (assoc-guts (eql item (funcall key (car a-list))))))
      (cond (test
	     (assoc-guts (funcall test item (caar a-list))))
	    (test-not
	     (assoc-guts (not (funcall test item (caar a-list)))))
	    (t
	     (assoc-guts (eql item (caar a-list)))))))
  
(defun assoc-if (predicate a-list)
  "Returns the first cons in A-List whose car satisfies the Predicate."
  (assoc-guts (funcall predicate (caar a-list))))

(defun assoc-if-not (predicate a-list)
  "Returns the first cons in A-List whose car does not satisfy the Predicate."
  (assoc-guts (not (funcall predicate (caar a-list)))))

;;;[Victor] :key keyword added like for assoc.
(defun rassoc (item a-list &key test test-not key)
  (declare (list a-list))
  "Returns the cons in A-List whose cdr is equal (by a given test or EQL) to
   the Item."
  (if key
      (cond (test
	     (assoc-guts (funcall test item (funcall key (car a-list)))))
	    (test-not
	     (assoc-guts (not (funcall test item (funcall key (car a-list))))))
	    (t
	     (assoc-guts (eql item (funcall key (car a-list))))))
      (cond (test
	     (assoc-guts (funcall test item (cdar a-list))))
	    (test-not
	     (assoc-guts (not (funcall test item (cdar a-list)))))
	    (t
	     (assoc-guts (eql item (cdar a-list)))))))

(defun rassoc-if (predicate a-list)
  "Returns the first cons in A-List whose cdr satisfies the Predicate."
  (assoc-guts (funcall predicate (cdar a-list))))

(defun rassoc-if-not (predicate a-list)
  "Returns the first cons in A-List whose cdr does not satisfy the Predicate."
  (assoc-guts (not (funcall predicate (cdar a-list)))))
;;; Mapc and friends

(defun map1 (function original-arglists accumulate take-car)
  "This function is called by mapc, mapcar, mapcan, mapl, maplist, and
  mapcon.  It Maps function over the arglists in the appropriate way.
  It is done when any of the arglists runs out.  Until then, it CDRs
  down the arglists calling the function and accumulating results as
  desired."
  (let* ((arglists (copy-list original-arglists))
	 (ret-list (list nil)) 
	 (temp ret-list))
    (do ((res nil)
	 (args '() '()))
	((dolist (x arglists nil) (if (null x) (return t)))
	 (if accumulate
	     (cdr ret-list)
	     (car original-arglists)))
      (do ((l arglists (cdr l)))
	  ((null l))
	(push (if take-car (caar l) (car l)) args)
	(setf (car l) (cdar l)))
      (setq res (apply function (nreverse args)))
      (case accumulate
	(:nconc (setq temp (last (nconc temp res))))
	(:list (rplacd temp (list res))
	       (setq temp (cdr temp)))))))


(defun mapc (function list &rest more-lists)
  "Applies fn to successive elements of lists, returns the second argument."
  (map1 function (cons list more-lists) nil t))

(defun mapcar (function list &rest more-lists)
  "Applies fn to successive elements of list, returns list of results."
  (map1 function (cons list more-lists) :list t))

(defun mapcan (function list &rest more-lists)
  "Applies fn to successive elements of list, returns NCONC of results."
  (map1 function (cons list more-lists) :nconc t))

(defun mapl (function list &rest more-lists)
  "Applies fn to successive CDRs of list, returns the second argument."
  (map1 function (cons list more-lists) nil nil))

(defun maplist (function list &rest more-lists)
  "Applies fn to successive CDRs of list, returns list of results."
  (map1 function (cons list more-lists) :list nil))

(defun mapcon (function list &rest more-lists)
  "Applies fn to successive CDRs of lists, returns NCONC of results."
  (map1 function (cons list more-lists) :nconc nil))
;;; Functions for compatibility sake:

(defun memq (item list)
  "Returns tail of list beginning with first element eq to item"
  (do ((x list (cdr x)))
      ((atom x) '())
    (if (eq (car x) item) (return x))))

;20; ASSQ is in the kernel

(defun delq (item list &optional (n 0 np))
  (declare (fixnum n))
  "Returns list with all (up to n) elements with all elements EQ to ITEM
   deleted"
  (do ((x list (cdr x))
       (splice '()))
      ((or (atom x)
	   (and np (zerop n))) list)
    (cond ((eq item (car x))
	   (setq n (1- n))
	   (if (null splice) 
	       (setq list (cdr x))
	       (rplacd splice (cdr x))))
	  (T (setq splice x)))))	; move splice along to include element