;;; 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.
;;; Spice Lisp is currently incomplete and under active development.
;;; 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.
;;; 
;;; Nsublis, things at the beginning broken.
;;;
;;; The list functions are part of the standard Spice Lisp environment.
;;;
;;; **********************************************************************
;;;

;;; These functions perform basic list operations:

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


(defun tree-equal (x y &rest keywords)
  "Returns T if X and Y are isomorphic trees with identical leaves."
  (with-keywords keywords
      ((:test test #'eql)
       (:test test-not))
    (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)
  (car list))
(defun second (list)
  (cadr list))
(defun third (list)
  (caddr list))
(defun fourth (list)
  (cadddr list))
(defun fifth (list)
  (car (cddddr list)))
(defun sixth (list)
  (cadr (cddddr list)))
(defun seventh (list)
  (caddr (cddddr list)))
(defun eighth (list)
  (cadddr (cddddr list)))
(defun ninth (list)
  (car (cddddr (cddddr list))))
(defun tenth (list)
  (cadr (cddddr (cddddr list))))
(defun rest (list)
  (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 &rest keywords)
  (declare (fixnum size))
  "Constructs a list with size elements each set to value"
  (with-keywords keywords
      ((:initial-element initial-element ()))
    (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 (alist)
  "Returns a new association list equal to alist, constructed in space"
  (if (atom alist)
      (if alist
	  (error "~S is not a list." alist))
      (let ((result
	     (cons (if (atom (car alist))
		       (car alist)
		       (cons (caar alist) (cdar alist)) )
		   '() )))	      
	(do ((x (cdr alist) (cdr x))
	     (splice result
		     (cdr (rplacd splice
				  (cons
				   (if (atom (car x)) 
				       (car x)
				       (cons (caar x) (cdar x)))
				   '() ))) ))
;;; Non-null terminated alist 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

(defun rplaca (x y) (rplaca x y))

(defun rplacd (x y) (rplacd x y))

;;; 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))))))




;;; 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."
  (labels ((s (subtree)
	      (cond ((satisfies-the-test old subtree) new)
		    ((atom subtree) subtree)
		    (t (let ((car (s (car subtree)))
			     (cdr (s (cdr subtree))))
			 (if (and (eq car (car subtree))
				  (eq cdr (cdr subtree)))
			     subtree
			     (cons car cdr)))))))
    (s tree)))

(defun subst-if (new test tree &key (key #'identity))
  "Substitutes new for subtrees for which test is true."
  (labels ((s (subtree)
	      (cond ((funcall test subtree) new)
		    ((atom subtree) subtree)
		    (t (let ((car (s (car subtree)))
			     (cdr (s (cdr subtree))))
			 (if (and (eq car (car subtree))
				  (eq cdr (cdr subtree)))
			     subtree
			     (cons car cdr)))))))
    (s tree)))

(defun subst-if-not (new test tree &key (key #'identity))
  "Substitutes new for subtrees for which test is false."
  (labels ((s (subtree)
	      (cond ((not (funcall test subtree)) new)
		    ((atom subtree) subtree)
		    (t (let ((car (s (car subtree)))
			     (cdr (s (cdr subtree))))
			 (if (and (eq car (car subtree))
				  (eq cdr (cdr subtree)))
			     subtree
			     (cons car cdr)))))))
    (s tree)))

(defun nsubst (new old tree &key (key #'identity)
		  (test #'eql testp) (test-not nil notp))
  "Substitutes new for subtrees matching old."
  (labels ((s (subtree)
	      (cond ((satisfies-the-test old subtree) new)
		    ((atom subtree) subtree)
		    (t (do* ((last nil subtree)
			     (subtree subtree (Cdr subtree)))
			    ((atom subtree)
			     (if (satisfies-the-test old subtree)
				 (setf (cdr last) new)))
			 (if (satisfies-the-test old subtree)
			     (return (setf (cdr last) new))
			     (setf (car subtree) (s (car subtree)))))
		       subtree))))
    (s tree)))

(defun nsubst-if (new test tree &key (key #'identity))
  "Substitutes new for subtrees of tree for which test is true."
  (labels ((s (subtree)
	      (cond ((funcall test (funcall key subtree)) new)
		    ((atom subtree) subtree)
		    (t (do* ((last nil subtree)
			     (subtree subtree (Cdr subtree)))
			    ((atom subtree)
			     (if (funcall test (funcall key subtree))
				 (setf (cdr last) new)))
			 (if (funcall test (funcall key subtree))
			     (return (setf (cdr last) new))
			     (setf (car subtree) (s (car subtree)))))
		       subtree))))
    (s tree)))

(defun nsubst-if-not (new test tree &key (key #'identity))
  "Substitutes new for subtrees of tree for which test is false."
  (labels ((s (subtree)
	      (cond ((not (funcall test (funcall key subtree))) new)
		    ((atom subtree) subtree)
		    (t (do* ((last nil subtree)
			     (subtree subtree (Cdr subtree)))
			    ((atom subtree)
			     (if (not (funcall test (funcall key subtree)))
				 (setf (cdr last) new)))
			 (if (not (funcall test (funcall key subtree)))
			     (return (setf (cdr last) new))
			     (setf (car subtree) (s (car subtree)))))
		       subtree))))
    (s tree)))




(defun sublis (alist tree &key (key #'identity)
		     (test #'eql testp) (test-not nil notp))
  "Substitutes from alist into tree nondestructively."
  (labels ((s (subtree)
	      (let ((assoc
		     (if notp
			 (assoc (funcall key subtree) alist :test-not test-not)
			 (assoc (funcall key subtree) alist :test test))))
		(cond (assoc (cdr assoc))
		      ((atom subtree) subtree)
		      (t (let ((car (s (car subtree)))
			       (cdr (s (cdr subtree))))
			   (if (and (eq car (car subtreE))
				    (eq cdr (cdr subtree)))
			       subtree
			       (cons car cdr))))))))
    (s tree)))

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

(defun nsublis (alist tree &key (key #'identity)
		  (test #'eql testp) (test-not nil notp))
  "Substitutes new for subtrees matching old."
  (let (temp)
    (labels ((s (subtree)
		(cond ((Setq temp (nsublis-macro))
		       (cdr temp))
		      ((atom subtree) subtree)
		      (t (do* ((last nil subtree)
			       (subtree subtree (Cdr subtree)))
			      ((atom subtree)
			       (if (setq temp (nsublis-macro))
				   (setf (cdr last) (cdr temp))))
			   (if (setq temp (nsublis-macro))
			       (return (setf (Cdr last) (Cdr temp)))
			       (setf (car subtree) (s (car subtree)))))
			 subtree))))
      (s tree))))


;;; Functions for using lists as sets

(defun member (item list &key (key #'identity) (test #'eql testp)
		    (test-not nil notp))
  "Returns tail of list beginning with first element satisfying EQUALity,
   :test, or :test-not with a given item."
  (do ((list list (cdr list)))
      ((null list) nil)
    (let ((car (Car list)))
      (if (satisfies-the-test item car)
	  (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) nil)
    (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 testp) (test-not nil notp))
  "Add item to list unless it is already a member"
  (if (if notp (member item list :test-not test-not :key key)
	  (member 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))
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res list1))
    (do () ((atom 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))
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res nil))
    (do () ((atom 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))
  (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))
  (if (and testp notp)
      (error "Test and test-not both supplied."))
  (let ((res nil))
    (do () ((atom 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 ()))
      ((atom x)
       (if (null splicex)
	   (setq list1 list2)
	   (rplacd splicex list2))
       list1)
    (do ((y list2 (cdr y))
	 (splicey ()))
	((atom 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))
  (dolist (elt list1)
    (if (with-set-keys (member (funcall key elt) list2))
	(return-from subsetp nil)))
  T)


;;; Functions that operate on association lists

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

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

(eval-when (compile)

(defmacro assoc-guts (test-guy)
  `(do ((alist alist (cdr alist)))
       ((atom alist))
     (if (car alist)
	 (if ,test-guy (return (car alist))))))

)

(defun assoc (item alist &rest keywords)
  "Returns the cons in alist whose car is equal (by a given test or EQL) to
   the Item."
  (with-keywords keywords
      ((:test test)
       (:test-not test-not))
    (cond (test
	   (assoc-guts (funcall test item (caar alist))))
	  (test-not
	   (assoc-guts (not (funcall test item (caar alist)))))
	  (t
	   (assoc-guts (eql item (caar alist)))))))

(defun assoc-if (predicate alist)
  "Returns the first cons in alist whose car satisfies the Predicate."
  (assoc-guts (funcall predicate (caar alist))))

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

(defun rassoc (item alist &rest keywords)
  (declare (list alist))
  "Returns the cons in alist whose cdr is equal (by a given test or EQL) to
   the Item."
  (with-keywords keywords
      ((:test test)
       (:test-not test-not))
    (cond (test
	   (assoc-guts (funcall test item (cdar alist))))
	  (test-not
	   (assoc-guts (not (funcall test item (cdar alist)))))
	  (t
	   (assoc-guts (eql item (cdar alist)))))))

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

(defun rassoc-if-not (predicate alist)
  "Returns the first cons in alist whose cdr does not satisfy the Predicate."
  (assoc-guts (not (funcall predicate (cdar alist)))))

;;; 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))))

(defun assq (item alist)
  "Return the first pair of alist where item EQ the key of pair"
  (do ((x alist (cdr x)))
      ((atom x) '())
    (if (eq item (caar x)) (return (car x)))))

(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