(FILECREATED "30-Sep-86 11:53:23" {ERIS}<LISPCORE>SOURCES>CMLLIST.;10 57557  

      changes to:  (VARS CMLLISTCOMS)
                   (FNS CL:MEMBER XXTEST1 XXTEST0 %%TEST TREE-EQUAL ADJOIN)
                   (FUNCTIONS XXTEST1 XXTEST0 %%%%TEST1 %%%%TEST0)
                   (MACROS CL:LIST-EVERY)

      previous date: "29-Sep-86 20:03:51" {ERIS}<LISPCORE>SOURCES>CMLLIST.;7)


(* "
Copyright (c) 1985, 1986 by Xerox Corporation.  All rights reserved.
")

(PRETTYCOMPRINT CMLLISTCOMS)

(RPAQQ CMLLISTCOMS 
       [(* ;;; "CMLLIST.  Common Lisp Lists Covers all of chapter 15 -- By Kelly Roach, Larry Masinter, and Ron Fischer, Frank Shih."
           )
        (COMS (* ;; "Section 15.1 Conses.")
              (* ;; "CAR, CDR, ..., CDDDDR (all functions on pages 262-263) are OK.")
              (* ;; "CONS is OK.")
              (FNS TREE-EQUAL %%TREE-EQUAL-TEST %%TREE-EQUAL-TEST-NOT))
        (COMS (* ;; "Section 15.2 Lists.")
              (FNS ENDP LIST-LENGTH)
              (COMS (FNS CL:NTH %%SETNTH)
                    (PROP SETFN CL:NTH))
              (COMS (FNS CL:FIRST SECOND THIRD FOURTH FIFTH SIXTH SEVENTH EIGHTH NINTH TENTH REST)
                    (* ;; "Macros to make SETF work and code optimize")
                    (OPTIMIZERS CL:FIRST SECOND THIRD FOURTH FIFTH SIXTH SEVENTH EIGHTH NINTH TENTH 
                           REST))
              (FNS NTHCDR)
              (* ;; "LAST LIST LIST* are OK.")
              (FNS MAKE-LIST)
              (* ;; "Common Lisp APPEND is different from Interlisp APPEND because Interlisp APPEND copies its last arg while Common Lisp APPEND does not.  See page 268 of the silver book."
                 )
              (FNS CL:APPEND %%APPEND)
              (FNS COPY-LIST COPY-ALIST COPY-TREE REVAPPEND)
              (* ;; "NCONC is OK.")
              (FNS NRECONC)
              (* ;; 
   "CL:PUSH and CL:PUSHNEW are macros defined somewhere else.  Don't know where that is.  POP is OK."
                 )
              (FNS BUTLAST NBUTLAST CL:LDIFF))
        (COMS (* ;; "Section 15.3 Alteration of List Structure.")
              (* ;; "RPLACA RPLACD are OK."))
        (COMS (* ;; "Section 15.4 Substitution of Expressions.")
              (DECLARE: DONTCOPY DOEVAL@COMPILE (FUNCTIONS SPLICE TEST-KEYS-SUPPLIED))
              (FNS %%TEST)
              (FNS XXTEST1 XXTEST0)
              (FNS CL:SUBST %%SUBST SUBST-IF %%SUBST-IF SUBST-IF-NOT %%SUBST-IF-NOT NSUBST %%NSUBST 
                   NSUBST-IF %%NSUBST-IF NSUBST-IF-NOT %%NSUBST-IF-NOT)
              (FNS CL:SUBLIS %%SUBLIS NSUBLIS %%NSUBLIS))
        (COMS (* ;; "Section 15.5 Usng Lists as Sets.")
              (FNS CL:MEMBER %%MEMBER MEMBER-IF MEMBER-IF-NOT)
              (OPTIMIZERS CL:MEMBER)
              (* ;; "TAILP is OK.")
              (FNS ADJOIN)
              (FNS CL:UNION NUNION)
              (FNS CL:INTERSECTION NINTERSECTION)
              (FNS SET-DIFFERENCE NSET-DIFFERENCE)
              (FNS SET-EXCLUSIVE-OR NSET-EXCLUSIVE-OR)
              (FNS SUBSETP))
        (COMS (* ;; "Section 15.6 Association Lists.")
              (FNS ACONS PAIRLIS)
              (FNS CL:ASSOC %%ASSOC ASSOC-IF ASSOC-IF-NOT RASSOC RASSOC-IF RASSOC-IF-NOT))
        (COMS (* ;; "Section 7.8.4 Mapping")
              (FNS CL:MAPCAR CL:MAPLIST CL:MAPC MAPL MAPCAN CL:MAPCON %%MAP))
        [COMS (* ;; "Stuff I'm not sure where to place yet.")
              (* ;; "It seems that DELQ ASSQ MEMQ aren't part of Common Lisp but part of Spice Lisp.  What functions in the system are currently depending on these functions?"
                 )
              (FNS DELQ)
              (PROP DMACRO CL:LIST-EVERY)
              (P (MOVD (QUOTE ASSOC)
                       (QUOTE ASSQ))
                 (MOVD (QUOTE FMEMB)
                       (QUOTE MEMQ]
        (DECLARE: DONTEVAL@LOAD DOEVAL@COMPILE DONTCOPY COMPILERVARS
               (ADDVARS (NLAMA)
                      (NLAML)
                      (LAMA DELQ %%MAP CL:MAPCON MAPCAN MAPL CL:MAPC CL:MAPLIST CL:MAPCAR 
                            RASSOC-IF-NOT RASSOC-IF RASSOC ASSOC-IF-NOT ASSOC-IF %%ASSOC CL:ASSOC 
                            PAIRLIS ACONS SUBSETP NSET-EXCLUSIVE-OR SET-EXCLUSIVE-OR NSET-DIFFERENCE 
                            SET-DIFFERENCE NINTERSECTION CL:INTERSECTION NUNION CL:UNION ADJOIN 
                            MEMBER-IF-NOT MEMBER-IF %%MEMBER CL:MEMBER %%NSUBLIS NSUBLIS %%SUBLIS 
                            CL:SUBLIS %%NSUBST-IF-NOT NSUBST-IF-NOT %%NSUBST-IF NSUBST-IF %%NSUBST 
                            NSUBST %%SUBST-IF-NOT SUBST-IF-NOT %%SUBST-IF SUBST-IF %%SUBST CL:SUBST 
                            XXTEST0 XXTEST1 %%TEST CL:LDIFF NBUTLAST BUTLAST NRECONC REVAPPEND 
                            COPY-TREE COPY-ALIST CL:APPEND MAKE-LIST NTHCDR REST TENTH NINTH EIGHTH 
                            SEVENTH SIXTH FIFTH FOURTH THIRD SECOND CL:FIRST %%SETNTH CL:NTH 
                            LIST-LENGTH %%TREE-EQUAL-TEST-NOT %%TREE-EQUAL-TEST TREE-EQUAL])



(* ;;; 
"CMLLIST.  Common Lisp Lists Covers all of chapter 15 -- By Kelly Roach, Larry Masinter, and Ron Fischer, Frank Shih."
)




(* ;; "Section 15.1 Conses.")




(* ;; "CAR, CDR, ..., CDDDDR (all functions on pages 262-263) are OK.")




(* ;; "CONS is OK.")

(DEFINEQ

(TREE-EQUAL
  [CL:LAMBDA (X Y &KEY (TEST (FUNCTION EQL))
                TEST-NOT)                                    (* FS "29-Sep-86 17:08")
                                                             (* Returns T if X and Y are isomorphic 
                                                             trees with identical leaves.
                                                             *)
         (COND
            ((AND TEST TEST-NOT)
             (CL:ERROR "Both TEST and TEST-NOT supplied."))
            (TEST-NOT (%%TREE-EQUAL-TEST-NOT X Y TEST-NOT))
            (T (%%TREE-EQUAL-TEST X Y TEST])

(%%TREE-EQUAL-TEST
  [CL:LAMBDA (X Y TEST)                                      (* kbr: " 1-Jul-86 20:47")
         (COND
            ((CONSP X)
             (AND (CONSP Y)
                  (%%TREE-EQUAL-TEST (CAR X)
                         (CAR Y)
                         TEST)
                  (%%TREE-EQUAL-TEST (CDR X)
                         (CDR Y)
                         TEST)))
            ((CONSP Y)
             NIL)
            (T (FUNCALL TEST X Y])

(%%TREE-EQUAL-TEST-NOT
  [CL:LAMBDA (X Y TEST-NOT)                                  (* kbr: " 1-Jul-86 20:47")
         (COND
            ((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)))
            ((CONSP Y)
             NIL)
            (T (NOT (FUNCALL TEST-NOT X Y])
)



(* ;; "Section 15.2 Lists.")

(DEFINEQ

(ENDP
  [LAMBDA (OBJECT)                                           (* raf "29-Jan-86 17:46")
    (COND
       ((NULL OBJECT)
        T)
       ((CL:LISTP OBJECT)
        NIL)
       (T (ERROR "Arg not list" OBJECT])

(LIST-LENGTH
  [CL:LAMBDA (LIST)                                          (* kbr: "24-Jun-86 23:09")
                                                             (* Returns the length of the given 
                                                             LIST or NIL if the LIST is circular.
                                                             *)
         (CL:DO ((N 0 (+ N 2))
                 (Y LIST (CDDR Y))
                 (Z LIST (CDR Z)))
                (NIL)
                (COND
                   ((ENDP Y)
                    (RETURN N)))
                [COND
                   ((ENDP (CDR Y))
                    (RETURN (+ N 1]
                (COND
                   ((AND (EQ Y Z)
                         (> N 0))
                    (RETURN NIL])
)
(DEFINEQ

(CL:NTH
  (CL:LAMBDA (N LIST)                                        (* kbr: "25-Jun-86 23:03")
         (CAR (NTHCDR N LIST))))

(%%SETNTH
  [CL:LAMBDA (N LIST NEWVAL)                                 (* kbr: " 4-Jun-86 23:35")
         (DECLARE (TYPE FIXNUM N))                           (* Sets the Nth element of LIST
                                                             (zero based) to NEWVAL *)
         (COND
            ((< N 0)
             (CL:ERROR "~S is an illegal N for SETF of NTH." N))
            (T (CL:DO ((COUNT N (1- COUNT)))
                      ((ZEROP COUNT)
                       (RPLACA LIST NEWVAL)
                       NEWVAL)
                      (DECLARE (TYPE FIXNUM COUNT))
                      (COND
                         ((ENDP (CDR LIST))
                          (CL:ERROR "~S is too large an index for SETF of NTH." N))
                         (T (SETQ LIST (CDR LIST])
)

(PUTPROPS CL:NTH SETFN %%SETNTH)
(DEFINEQ

(CL:FIRST
  (CL:LAMBDA (LIST)                                          (* lmm "20-Jan-86 20:58")
         (CAR LIST)))

(SECOND
  (CL:LAMBDA (LIST)                                          (* lmm "20-Jan-86 20:58")
         (CADR LIST)))

(THIRD
  (CL:LAMBDA (LIST)
         (CADDR LIST)))

(FOURTH
  (CL:LAMBDA (LIST)
         (CADDDR LIST)))

(FIFTH
  (CL:LAMBDA (LIST)
         (CAR (CDDDDR LIST))))

(SIXTH
  (CL:LAMBDA (LIST)
         (CADR (CDDDDR LIST))))

(SEVENTH
  (CL:LAMBDA (LIST)
         (CADDR (CDDDDR LIST))))

(EIGHTH
  (CL:LAMBDA (LIST)
         (CADDDR (CDDDDR LIST))))

(NINTH
  [CL:LAMBDA (LIST)
         (CAR (CDDDDR (CDDDDR LIST])

(TENTH
  [CL:LAMBDA (LIST)
         (CADR (CDDDDR (CDDDDR LIST])

(REST
  (CL:LAMBDA (LIST)
         (CDR LIST)))
)



(* ;; "Macros to make SETF work and code optimize")

(DEFOPTIMIZER CL:FIRST (&REST ARGS) (CONS (QUOTE CAR)
                                          ARGS))

(DEFOPTIMIZER SECOND (&REST ARGS) (CONS (QUOTE CADR)
                                        ARGS))

(DEFOPTIMIZER THIRD (LIST) (BQUOTE (CADDR (\, LIST))))

(DEFOPTIMIZER FOURTH (LIST) (BQUOTE (CADDDR (\, LIST))))

(DEFOPTIMIZER FIFTH (LIST) (BQUOTE (CAR (CDDDDR (\, LIST)))))

(DEFOPTIMIZER SIXTH (LIST) (BQUOTE (CADR (CDDDDR (\, LIST)))))

(DEFOPTIMIZER SEVENTH (LIST) (BQUOTE (CADDR (CDDDDR (\, LIST)))))

(DEFOPTIMIZER EIGHTH (LIST) (BQUOTE (CADDDR (CDDDDR (\, LIST)))))

(DEFOPTIMIZER NINTH (LIST) (BQUOTE (CAR (CDDDDR (CDDDDR (\, LIST))))))

(DEFOPTIMIZER TENTH (LIST) (BQUOTE (CADR (CDDDDR (CDDDDR (\, LIST))))))

(DEFOPTIMIZER REST (LIST) (BQUOTE (CDR (\, LIST))))

(DEFINEQ

(NTHCDR
  [CL:LAMBDA (N LIST)                                        (* kbr: "25-Jun-86 23:05")
         (CL:IF (NOT (AND (INTEGERP N)
                          (>= N 0)))
                (CL:ERROR "N = ~S is illegal for NTHCDR." N)
                (PROG NIL
                  LP  (COND
                         ((= N 0)
                          (RETURN LIST))
                         ((ENDP LIST)
                          (RETURN NIL)))
                      (SETQ LIST (CDR LIST))
                      (SETQ N (1- N))
                      (GO LP])
)



(* ;; "LAST LIST LIST* are OK.")

(DEFINEQ

(MAKE-LIST
  [CL:LAMBDA (SIZE &KEY INITIAL-ELEMENT)
         (DECLARE (TYPE FIXNUM SIZE))                        (* raf " 5-Dec-85 04:32")
         (COND
            ((< SIZE 0)
             (CL:ERROR "~S is an illegal size for MAKE-LIST." SIZE))
            (T (CL:DO ((COUNT SIZE (1- COUNT))
                       (RESULT (QUOTE NIL)
                              (CONS INITIAL-ELEMENT RESULT)))
                      ((ZEROP COUNT)
                       RESULT)
                      (DECLARE (TYPE FIXNUM COUNT])
)



(* ;; 
"Common Lisp APPEND is different from Interlisp APPEND because Interlisp APPEND copies its last arg while Common Lisp APPEND does not.  See page 268 of the silver book."
)

(DEFINEQ

(CL:APPEND
  [LAMBDA L                                                  (* kbr: "17-Sep-86 17:05")
          
          (* The result is a list that is the concatenation of the arguments.
          The arguments are not destroyed. Note that APPEND copies the top-level list 
          structure of each of its arguments except the last.
          *)

    (PROG (VAL N)
          [COND
             ((EQ L 0)
              (RETURN NIL))
             ((EQ L 1)
              (RETURN (ARG L 1]
          (SETQ VAL (ARG L L))
          (for N from (SUB1 L) to 1 by -1 do (SETQ VAL (%%APPEND (ARG L N)
                                                              VAL)))
          (RETURN VAL])

(%%APPEND
  [LAMBDA (L1 L2)                                            (* kbr: "17-Sep-86 17:03")
    (PROG (ANSWER TAIL)
          (COND
             ((ENDP L1)
              (RETURN L2)))
          (SETQ ANSWER (LIST (pop L1)))
          (SETQ TAIL ANSWER)
      LOOP
          (COND
             ((ENDP L1)
              (SETF (CDR TAIL)
                    L2)
              (RETURN ANSWER)))
          (SETF (CDR TAIL)
                (LIST (pop L1)))
          (SETQ TAIL (CDR TAIL))
          (GO LOOP])
)
(DEFINEQ

(COPY-LIST
  [LAMBDA (L)                                                (* lmm " 6-May-86 23:02")
    (COND
       ((LISTP L)
        (PROG ((VAL (CONS (CAR L)
                          NIL))
               TAIL)
              (SETQ TAIL VAL)
          LP  [RPLACD TAIL (SETQ TAIL (LIST (CAR (OR (LISTP (SETQ L (CDR L)))
                                                     (PROGN (AND L (RPLACD TAIL L))
                                                            (RETURN VAL]
              (GO LP)))
       (T L])

(COPY-ALIST
  [CL:LAMBDA (ALIST)                                         (* kbr: " 4-Jun-86 23:33")
         (COND
            ((CL:ATOM ALIST)
             (COND
                (ALIST (CL:ERROR "~S is not a list." ALIST))
                (T NIL)))
            (T (LET [(RESULT (CONS [COND
                                      ((CL:ATOM (CAR ALIST))
                                       (CAR ALIST))
                                      (T (CONS (CAAR ALIST)
                                               (CDAR ALIST]
                                   (QUOTE NIL]
                    (CL:DO [(X (CDR ALIST)
                               (CDR X))
                            (SPLICE RESULT (CDR (RPLACD SPLICE (CONS [COND
                                                                        ((CL:ATOM (CAR X))
                                                                         (CAR X))
                                                                        (T (CONS (CAAR X)
                                                                                 (CDAR X]
                                                                     (QUOTE NIL]
                           ((CL:ATOM X)                      (* Non-null terminated alist done 
                                                             here. *)
                            (COND
                               ((NOT (NULL X))
                                (RPLACD SPLICE X)))
                            RESULT])

(COPY-TREE
  [CL:LAMBDA (OBJECT)                                        (* kbr: " 4-Jun-86 23:34")
                                                             (* COPY-TREE recursively copys trees 
                                                             of conses. *)
         (COND
            ((NOT (CONSP OBJECT))
             OBJECT)
            (T (CONS (COPY-TREE (CAR OBJECT))
                     (COPY-TREE (CDR OBJECT])

(REVAPPEND
  (CL:LAMBDA (X Y)                                           (* kbr: " 4-Jun-86 23:34")
                                                             (* Returns (APPEND (REVERSE X) Y) *)
         (CL:DO ((TOP X (CDR TOP))
                 (RESULT Y (CONS (CAR TOP)
                                 RESULT)))
                ((ENDP TOP)
                 RESULT))))
)



(* ;; "NCONC is OK.")

(DEFINEQ

(NRECONC
  (CL:LAMBDA (X Y)                                           (* kbr: "31-Aug-85 15:19")
         (NCONC (NREVERSE X)
                Y)))
)



(* ;; 
"CL:PUSH and CL:PUSHNEW are macros defined somewhere else.  Don't know where that is.  POP is OK."
)

(DEFINEQ

(BUTLAST
  [CL:LAMBDA (LIST &OPTIONAL (N 1))
         (DECLARE (TYPE FIXNUM N))                           (* raf " 5-Dec-85 04:33")
         (COND
            ((< N 0)
             (SETQ N 0))
            (T NIL))
         (LET [(LENGTH (1- (LENGTH (THE LIST LIST]
              (DECLARE (TYPE FIXNUM LENGTH))
              (COND
                 ((< LENGTH N)
                  NIL)
                 (T (CL: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])

(NBUTLAST
  [CL:LAMBDA (LIST &OPTIONAL (N 1))
         (DECLARE (TYPE FIXNUM N))                           (* raf " 5-Dec-85 04:33")
         (COND
            ((< N 0)
             (SETQ N 0))
            (T NIL))
         (LET [(LENGTH (1- (LENGTH (THE LIST LIST]
              (DECLARE (TYPE FIXNUM LENGTH))
              (COND
                 ((< LENGTH N)
                  NIL)
                 (T (CL:DO ((1ST (CDR LIST)
                                 (CDR 1ST))
                            (2ND LIST 1ST)
                            (COUNT LENGTH (1- COUNT)))
                           ((= COUNT N)
                            (RPLACD 2ND NIL)
                            LIST])

(CL:LDIFF
  [CL:LAMBDA (LIST SUBLIST)                                  (* raf " 5-Dec-85 04:36")
         (CL:DO* ((LIST LIST (CDR LIST))
                  (RESULT (LIST NIL))
                  (SPLICE RESULT))
                ((OR (NULL LIST)
                     (EQ LIST SUBLIST))
                 (CDR RESULT))
                (SETQ SPLICE (CDR (RPLACD SPLICE (LIST (CAR LIST])
)



(* ;; "Section 15.3 Alteration of List Structure.")




(* ;; "RPLACA RPLACD are OK.")




(* ;; "Section 15.4 Substitution of Expressions.")

(DECLARE: DONTCOPY DOEVAL@COMPILE 
(DEFMACRO SPLICE (SOURCE DESTINATION) (BQUOTE (LET ((TEMP (\, SOURCE)))
                                                   (SETF (\, SOURCE)
                                                         (CDR (\, SOURCE))
                                                         (CDR TEMP)
                                                         (\, DESTINATION)
                                                         (\, DESTINATION)
                                                         TEMP))))

(DEFMACRO TEST-KEYS-SUPPLIED NIL (QUOTE (PROGN (COND ((AND TEST TEST-NOT)
                                                      (CL:ERROR "Both TEST and TEST-NOT supplied."))
                                                     ((AND (NULL TEST)
                                                           (NULL TEST-NOT))
                                                      (SETQ TEST (FUNCTION EQL))))
                                               (COND ((NULL KEY)
                                                      (SETQ KEY (FUNCTION IDENTITY)))))))

)
(DEFINEQ

(%%TEST
  [CL:LAMBDA (ITEM KEYITEM TEST TEST-NOT KEY)                (* FS "29-Sep-86 11:39")
         "This code should go away, use %%%%test0 & %%%%test1 instead"
         (COND
            (TEST (FUNCALL TEST ITEM (FUNCALL KEY KEYITEM)))
            [TEST-NOT (NOT (FUNCALL TEST-NOT ITEM (FUNCALL KEY KEYITEM]
            (T (SHOULDNT])
)
(DEFINEQ

(XXTEST1
  [CL:LAMBDA (ITEM KEYITEM TEST KEY)                         (* FS "29-Sep-86 20:50")
         (CL:BLOCK XXTEST1 (FUNCALL TEST ITEM (FUNCALL KEY KEYITEM])

(XXTEST0
  [CL:LAMBDA (ITEM KEYITEM TEST-NOT KEY)                     (* FS "29-Sep-86 20:52")
         (NOT (FUNCALL TEST-NOT ITEM (FUNCALL KEY KEYITEM])
)
(DEFINEQ

(CL:SUBST
  (CL:LAMBDA (NEW OLD TREE &KEY TEST TEST-NOT KEY)           (* kbr: "28-Jun-86 21:34")
                                                             (* Make copy of TREE substituting NEW 
                                                             for every subtree such that OLD and 
                                                             the subtree satisfy TEST *)
         (TEST-KEYS-SUPPLIED)
         (%%SUBST NEW OLD TREE TEST TEST-NOT KEY)))

(%%SUBST
  [CL:LAMBDA (NEW OLD TREE TEST TEST-NOT KEY)                (* kbr: "28-Jun-86 21:50")
                                                             (* Make copy of TREE substituting NEW 
                                                             for every subtree such that OLD and 
                                                             the subtree satisfy TEST
                                                             (usually EQL) *)
         (COND
            ((%%TEST OLD TREE TEST TEST-NOT KEY)
             NEW)
            ((CL:ATOM TREE)
             TREE)
            (T (LET ((CAR (%%SUBST NEW OLD (CAR TREE)
                                 TEST TEST-NOT KEY))
                     (CDR (%%SUBST NEW OLD (CDR TREE)
                                 TEST TEST-NOT KEY)))
                    (COND
                       ((AND (EQ CAR (CAR TREE))
                             (EQ CDR (CDR TREE)))
                        TREE)
                       (T (CONS CAR CDR])

(SUBST-IF
  (CL:LAMBDA (NEW TEST TREE &KEY (KEY (FUNCTION IDENTITY)))  (* kbr: "25-Jun-86 15:33")
                                                             (* Substitutes new for subtrees for 
                                                             which test is true. *)
         (%%SUBST-IF NEW TEST TREE KEY)))

(%%SUBST-IF
  [CL:LAMBDA (NEW TEST TREE KEY)                             (* kbr: "25-Jun-86 16:18")
                                                             (* Substitutes new for subtrees for 
                                                             which test is true. *)
         (COND
            ((FUNCALL TEST (FUNCALL KEY TREE))
             NEW)
            ((CL:ATOM TREE)
             TREE)
            (T (LET ((CAR (%%SUBST-IF NEW TEST (CAR TREE)
                                 KEY))
                     (CDR (%%SUBST-IF NEW TEST (CDR TREE)
                                 KEY)))
                    (COND
                       ((AND (EQ CAR (CAR TREE))
                             (EQ CDR (CDR TREE)))
                        TREE)
                       (T (CONS CAR CDR])

(SUBST-IF-NOT
  (CL:LAMBDA (NEW TEST TREE &KEY (KEY (FUNCTION IDENTITY)))  (* kbr: "25-Jun-86 15:51")
                                                             (* Substitutes new for subtrees for 
                                                             which test is false.
                                                             *)
         (%%SUBST-IF-NOT NEW TEST TREE KEY)))

(%%SUBST-IF-NOT
  [CL:LAMBDA (NEW TEST TREE KEY)                             (* kbr: "25-Jun-86 16:19")
                                                             (* Substitutes new for subtrees for 
                                                             which test is false.
                                                             *)
         (COND
            ((NOT (FUNCALL TEST (FUNCALL KEY TREE)))
             NEW)
            ((CL:ATOM TREE)
             TREE)
            (T (LET ((CAR (%%SUBST-IF-NOT NEW TEST (CAR TREE)
                                 KEY))
                     (CDR (%%SUBST-IF-NOT NEW TEST (CDR TREE)
                                 KEY)))
                    (COND
                       ((AND (EQ CAR (CAR TREE))
                             (EQ CDR (CDR TREE)))
                        TREE)
                       (T (CONS CAR CDR])

(NSUBST
  (CL:LAMBDA (NEW OLD TREE &KEY TEST TEST-NOT KEY)           (* kbr: "28-Jun-86 21:38")
         (TEST-KEYS-SUPPLIED)
         (%%NSUBST NEW OLD TREE TEST TEST-NOT KEY)))

(%%NSUBST
  (CL:LAMBDA (NEW OLD TREE TEST TEST-NOT KEY)                (* kbr: "27-Aug-86 13:36")
                                                             (* Conditionally destructively 
                                                             substitute NEW for OLD in TREE *)
         (PROG (TAIL)
               (COND
                  ((%%TEST OLD TREE TEST TEST-NOT KEY)
                   (RETURN NEW)))
               (SETQ TAIL TREE)
           LOOP
               (COND
                  ((CL:ATOM TAIL)
                   (RETURN TREE)))
               (COND
                  ((%%TEST OLD (CAR TAIL)
                          TEST TEST-NOT KEY)
                   (SETF (CAR TAIL)
                         NEW))
                  (T (%%NSUBST NEW OLD (CAR TAIL)
                            TEST TEST-NOT KEY)))
               (COND
                  ((%%TEST OLD (CDR TAIL)
                          TEST TEST-NOT KEY)
                   (SETF (CDR TAIL)
                         NEW)
                   (RETURN TREE)))
               (SETQ TAIL (CDR TAIL))
               (GO LOOP))))

(NSUBST-IF
  (CL:LAMBDA (NEW TEST TREE &KEY (KEY (FUNCTION IDENTITY)))  (* kbr: "25-Jun-86 15:54")
         (%%NSUBST-IF NEW TEST TREE KEY)))

(%%NSUBST-IF
  (CL:LAMBDA (NEW TEST TREE KEY)                             (* kbr: "25-Jun-86 16:20")
         (COND
            ((FUNCALL TEST (FUNCALL KEY TREE))
             NEW)
            ((CL:ATOM TREE)
             TREE)
            (T [CL:DO* ((LAST NIL TREE)
                        (TREE TREE (CDR TREE)))
                      ((CL:ATOM TREE)
                       (COND
                          ((FUNCALL TEST (FUNCALL KEY TREE))
                           (SETF (CDR LAST)
                                 NEW))
                          (T NIL)))
                      (COND
                         ((FUNCALL TEST (FUNCALL KEY TREE))
                          (RETURN (SETF (CDR LAST)
                                        NEW)))
                         (T (SETF (CAR TREE)
                                  (%%NSUBST-IF NEW TEST (CAR TREE)
                                         KEY]
               TREE))))

(NSUBST-IF-NOT
  (CL:LAMBDA (NEW TEST TREE &KEY (KEY (FUNCTION IDENTITY)))  (* kbr: "25-Jun-86 15:55")
         (%%NSUBST-IF-NOT NEW TEST TREE KEY)))

(%%NSUBST-IF-NOT
  (CL:LAMBDA (NEW TEST TREE KEY)                             (* kbr: "25-Jun-86 16:21")
         (COND
            ((NOT (FUNCALL TEST (FUNCALL KEY TREE)))
             NEW)
            ((CL:ATOM TREE)
             TREE)
            (T [CL:DO* ((LAST NIL TREE)
                        (TREE TREE (CDR TREE)))
                      ((CL:ATOM TREE)
                       (COND
                          ((NOT (FUNCALL TEST (FUNCALL KEY TREE)))
                           (SETF (CDR LAST)
                                 NEW))
                          (T NIL)))
                      (COND
                         ((NOT (FUNCALL TEST (FUNCALL KEY TREE)))
                          (RETURN (SETF (CDR LAST)
                                        NEW)))
                         (T (SETF (CAR TREE)
                                  (%%NSUBST-IF-NOT NEW TEST (CAR TREE)
                                         KEY]
               TREE))))
)
(DEFINEQ

(CL:SUBLIS
  (CL:LAMBDA (ALIST TREE &KEY TEST TEST-NOT KEY)             (* kbr: "28-Jun-86 21:39")
                                                             (* Substitutes from ALIST into TREE 
                                                             nondestructively. *)
         (TEST-KEYS-SUPPLIED)
         (%%SUBLIS ALIST TREE TEST TEST-NOT KEY)))

(%%SUBLIS
  [CL:LAMBDA (ALIST TREE TEST TEST-NOT KEY)                  (* kbr: "27-Aug-86 13:08")
                                                             (* Substitutes from alist into tree 
                                                             nondestructively. *)
         (LET [(BUCKET (%%ASSOC (FUNCALL KEY TREE)
                              ALIST TEST TEST-NOT (CL:FUNCTION IDENTITY]
              (COND
                 (BUCKET (CDR BUCKET))
                 ((CL:ATOM TREE)
                  TREE)
                 (T (LET ((CAR (%%SUBLIS ALIST (CAR TREE)
                                      TEST TEST-NOT KEY))
                          (CDR (%%SUBLIS ALIST (CDR TREE)
                                      TEST TEST-NOT KEY)))
                         (CL:IF (AND (EQ CAR (CAR TREE))
                                     (EQ CDR (CDR TREE)))
                                TREE
                                (CONS CAR CDR])

(NSUBLIS
  (CL:LAMBDA (ALIST TREE &KEY KEY TEST TEST-NOT)             (* kbr: "28-Jun-86 21:39")
         (TEST-KEYS-SUPPLIED)
         (%%NSUBLIS ALIST TREE TEST TEST-NOT KEY)))

(%%NSUBLIS
  (CL:LAMBDA (ALIST TREE TEST TEST-NOT KEY)                  (* kbr: "27-Aug-86 13:08")
         (PROG (BUCKET)
               (COND
                  ((SETQ BUCKET (%%ASSOC (FUNCALL KEY TREE)
                                       ALIST TEST TEST-NOT (CL:FUNCTION IDENTITY)))
                   (RETURN (CDR BUCKET)))
                  ((CL:ATOM TREE)
                   (RETURN TREE)))
               [CL:DO* ((LAST NIL TREE)
                        (TREE TREE (CDR TREE)))
                      ((CL:ATOM TREE)
                       (COND
                          ((SETQ BUCKET (%%ASSOC (FUNCALL KEY TREE)
                                               ALIST TEST TEST-NOT (CL:FUNCTION IDENTITY)))
                           (SETF (CDR LAST)
                                 (CDR BUCKET)))
                          (T NIL)))
                      (COND
                         [(SETQ BUCKET (%%ASSOC (FUNCALL KEY TREE)
                                              ALIST TEST TEST-NOT (CL:FUNCTION IDENTITY)))
                          (RETURN (SETF (CDR LAST)
                                        (CDR BUCKET]
                         (T (SETF (CAR TREE)
                                  (%%NSUBLIS ALIST (CAR TREE)
                                         TEST TEST-NOT KEY]
               (RETURN TREE))))
)



(* ;; "Section 15.5 Usng Lists as Sets.")

(DEFINEQ

(CL:MEMBER
  [CL:LAMBDA (ITEM LIST &KEY TEST TEST-NOT KEY)              (* FS "29-Sep-86 20:26")
         (TEST-KEYS-SUPPLIED)
         (COND
            (TEST (for TAIL on LIST when (XXTEST1 ITEM (CAR TAIL)
                                                TEST KEY) do (RETURN TAIL)))
            (TEST-NOT (for TAIL on LIST when (XXTEST0 ITEM (CAR TAIL)
                                                    TEST-NOT KEY) do (RETURN TAIL])

(%%MEMBER
  (CL:LAMBDA (ITEM LIST TEST TEST-NOT KEY)                   (* kbr: "28-Jun-86 21:52")
         (for TAIL on LIST when (%%TEST ITEM (CAR TAIL)
                                       TEST TEST-NOT KEY) do (RETURN TAIL))))

(MEMBER-IF
  [CL:LAMBDA (TEST LIST &KEY (KEY (FUNCTION IDENTITY)))      (* raf "29-Jan-86 17:36")
         (COND
            ((NOT (CL:LISTP LIST))
             (CL:ERROR "~S is not a list." LIST)))
         (CL:DO ((LIST LIST (CDR LIST)))
                ((ENDP LIST)
                 NIL)
                (COND
                   ((FUNCALL TEST (FUNCALL KEY (CAR LIST)))
                    (RETURN LIST))
                   (T NIL])

(MEMBER-IF-NOT
  [CL:LAMBDA (TEST LIST &KEY (KEY (FUNCTION IDENTITY)))      (* raf "29-Jan-86 17:37")
         (COND
            ((NOT (CL:LISTP LIST))
             (CL:ERROR "~S is not a list." LIST)))
         (CL:DO ((LIST LIST (CDR LIST)))
                ((ENDP LIST)
                 NIL)
                (COND
                   ([NOT (FUNCALL TEST (FUNCALL KEY (CAR LIST]
                    (RETURN LIST))
                   (T NIL])
)
(DEFOPTIMIZER CL:MEMBER (ITEM LIST &KEY (TEST (QUOTE (FUNCTION EQL))
                                              TESTSUPPLIED)
                              (TEST-NOT NIL TESTNOTSUPPLIED)
                              (KEY (QUOTE (FUNCTION IDENTITY)))) 
                                                             (* optimize simple cases)
   (LET ((TESTC (CONSTANTEXPRESSIONP TEST))
         (KEYC (CONSTANTEXPRESSIONP KEY))
         (TESTNOTC (CONSTANTEXPRESSIONP TEST-NOT))
         (LISTC (CONSTANTEXPRESSIONP LIST)))
        (COND
           ((AND LISTC KEYC (EQ (CAR TESTC)
                                (QUOTE EQL))
                 (EVERY (CAR LISTC)
                        (FUNCTION (LAMBDA (X)
                                    (NOT (NUMBERP (APPLY* (CAR KEYC)
                                                         X)))))))
            (SETQ TESTC (QUOTE (EQ)))))
        (COND
           ((AND TESTC KEYC TESTNOTC)
            (if (AND (EQ (CAR TESTC)
                         (QUOTE EQ))
                     (EQ (CAR KEYC)
                         (QUOTE IDENTITY))
                     (NOT TESTNOTSUPPLIED))
                then (LIST (QUOTE FMEMB)
                           ITEM LIST)
              else (LET ((ITERVAR (GENSYM "MEMBER"))
                         (ITEMVAR (GENSYM "MEMBER-ITEM")))
                        (BQUOTE (LET ((, ITEMVAR , ITEM))
                                     (for , ITERVAR on , LIST , (if TESTNOTSUPPLIED
                                                                    then (QUOTE unless)
                                                                  else (QUOTE when))
                                                       (, (if TESTNOTSUPPLIED
                                                              then (CAR TESTNOTC)
                                                            else (CAR TESTC))
                                                          (, (CAR KEYC)
                                                             (CAR , ITERVAR))
                                                          , ITEMVAR) do (RETURN , ITERVAR)))))))
           (T (QUOTE IGNOREMACRO)))))




(* ;; "TAILP is OK.")

(DEFINEQ

(ADJOIN
  [CL:LAMBDA (ITEM LIST &KEY TEST TEST-NOT KEY)              (* FS "29-Sep-86 18:24")
                                                             (* Add ITEM to LIST unless it is 
                                                             already a member *)
                                                             (* 
                                                             Adjoin% applies% KEY% to% ITEM% %(pg.% 276%))
         (TEST-KEYS-SUPPLIED)
         (COND
            ((%%MEMBER (FUNCALL KEY ITEM)
                    LIST TEST TEST-NOT KEY)
             LIST)
            (T (CONS ITEM LIST])
)
(DEFINEQ

(CL:UNION
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:38")
                                                             (* Returns the union of LIST1 and 
                                                             LIST2 *)
         (TEST-KEYS-SUPPLIED)
         (LET ((RES LIST1))
              (DOLIST (ELEMENT LIST2)
                     (CL:IF (NOT (%%MEMBER (FUNCALL KEY ELEMENT)
                                        LIST1 TEST TEST-NOT KEY))
                            (CL:PUSH ELEMENT RES)))
              RES)))

(NUNION
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:39")
         (TEST-KEYS-SUPPLIED)
         (LET ((RES LIST1))
              [CL:DO NIL ((ENDP LIST2))
                     (COND
                        ((NOT (%%MEMBER (FUNCALL KEY (CAR LIST2))
                                     LIST1 TEST TEST-NOT KEY))
                         (SPLICE LIST2 RES))
                        (T (SETQ LIST2 (CDR LIST2]
              RES)))
)
(DEFINEQ

(CL:INTERSECTION
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:39")
                                                             (* Returns the intersection of LIST1 
                                                             and LIST2 *)
         (TEST-KEYS-SUPPLIED)
         (LET ((RES NIL))
              (DOLIST (ELEMENT LIST1)
                     (CL:IF (%%MEMBER (FUNCALL KEY ELEMENT)
                                   LIST2 TEST TEST-NOT KEY)
                            (CL:PUSH ELEMENT RES)))
              RES)))

(NINTERSECTION
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:39")
         (TEST-KEYS-SUPPLIED)
         (LET ((RES NIL))
              [CL:DO NIL ((ENDP LIST1))
                     (COND
                        ((%%MEMBER (FUNCALL KEY (CAR LIST1))
                                LIST2 TEST TEST-NOT KEY)
                         (SPLICE LIST1 RES))
                        (T (SETQ LIST1 (CDR LIST1]
              RES)))
)
(DEFINEQ

(SET-DIFFERENCE
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:40")
         (TEST-KEYS-SUPPLIED)
         (LET ((RES NIL))
              (DOLIST (ELEMENT LIST1)
                     (COND
                        ((NOT (%%MEMBER (FUNCALL KEY ELEMENT)
                                     LIST2 TEST TEST-NOT KEY))
                         (CL:PUSH ELEMENT RES))
                        (T NIL)))
              RES)))

(NSET-DIFFERENCE
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:41")
         (TEST-KEYS-SUPPLIED)
         (LET ((RES NIL))
              [CL:DO NIL ((ENDP LIST1))
                     (COND
                        ((NOT (%%MEMBER (FUNCALL KEY (CAR LIST1))
                                     LIST2 TEST TEST-NOT KEY))
                         (SPLICE LIST1 RES))
                        (T (SETQ LIST1 (CDR LIST1]
              RES)))
)
(DEFINEQ

(SET-EXCLUSIVE-OR
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:41")
                                                             (* Returns new list of elements 
                                                             appearing exactly once in LIST1 and 
                                                             LIST2 *)
         (TEST-KEYS-SUPPLIED)
         (LET ((RESULT NIL))
              [DOLIST (ELEMENT LIST1)
                     (COND
                        ((NOT (%%MEMBER (FUNCALL KEY ELEMENT)
                                     LIST2 TEST TEST-NOT KEY))
                         (SETQ RESULT (CONS ELEMENT RESULT]
              [DOLIST (ELEMENT LIST2)
                     (COND
                        ((NOT (%%MEMBER (FUNCALL KEY ELEMENT)
                                     LIST1 TEST TEST-NOT KEY))
                         (SETQ RESULT (CONS ELEMENT RESULT]
              RESULT)))

(NSET-EXCLUSIVE-OR
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "31-Aug-86 21:05")
         (TEST-KEYS-SUPPLIED)
         (PROG (MATCHED KEYX KEYY SPLICEX SPLICEY)
          
          (* Each element that LIST1 and LIST2 have in common are cut out of LIST1 and 
          LIST2. Then what is left of LIST1 and LIST2 is NCONCed together to form the 
          ANSWER. SPLICEX and SPLICEY are the cells if any just preceding tails X and Y 
          on LIST1 and LIST2. *)
          
          (* No attempt is made to setify the answer if LIST1 and LIST2 contain 
          duplicates, but this algorithm can guarantee that the answer will not have 
          duplicates if LIST1 and LIST2 do not. *)

               [for X on LIST1
                  do (SETQ MATCHED NIL)
                     (SETQ KEYX (FUNCALL KEY (CAR X)))
                     (SETQ SPLICEY NIL)
                     (for Y on LIST2 do (SETQ KEYY (FUNCALL KEY (CAR Y)))
                                        (COND
                                           ((COND
                                               (TEST-NOT (NOT (FUNCALL TEST-NOT KEYX KEYY)))
                                               (T (FUNCALL TEST KEYX KEYY)))
                                                             (* (CAR X) matches (CAR Y) Splice
                                                             (CAR Y) out of LIST2.
                                                             *)
                                            (SETQ MATCHED T)
                                            (RPLACD X (CL:DELETE KEYX (CDR X)
                                                             :TEST TEST :TEST-NOT TEST-NOT :KEY KEY))
                                            (RPLACD Y (CL:DELETE KEYX (CDR Y)
                                                             :TEST TEST :TEST-NOT TEST-NOT :KEY KEY))
                                            [COND
                                               ((NULL SPLICEX)
                                                (SETQ LIST1 (CDR X)))
                                               (T (RPLACD SPLICEX (CDR X]
                                            [COND
                                               ((NULL SPLICEY)
                                                (SETQ LIST2 (CDR Y)))
                                               (T (RPLACD SPLICEY (CDR Y]
                                            (RETURN)))       (* No match, so advance SPLICEY 
                                                             pointer to next cell of LIST2.
                                                             *)
                                        (SETQ SPLICEY Y))
                     (COND
                        ((NOT MATCHED)                       (* No match, so advance SPLICEX 
                                                             pointer to next cell of LIST1.
                                                             *)
                         (SETQ SPLICEX X]                    (* NCONC remainder of LIST1 and 
                                                             remainders of LIST2 to form answer.
                                                             *)
               (COND
                  ((NULL SPLICEX)
                   (SETQ LIST1 LIST2))
                  (T (RPLACD SPLICEX LIST2)))
               (RETURN LIST1))))
)
(DEFINEQ

(SUBSETP
  (CL:LAMBDA (LIST1 LIST2 &KEY TEST TEST-NOT KEY)            (* kbr: "27-Aug-86 18:42")
         (TEST-KEYS-SUPPLIED)
         (for ELEMENT in LIST1 always (%%MEMBER (FUNCALL KEY ELEMENT)
                                             LIST2 TEST TEST-NOT KEY))))
)



(* ;; "Section 15.6 Association Lists.")

(DEFINEQ

(ACONS
  (CL:LAMBDA (KEY DATUM ALIST)                               (* kbr: " 4-Jun-86 23:40")
                                                             (* Construct a new alist by adding the 
                                                             pair (KEY . DATUM) to ALIST *)
         (CONS (CONS KEY DATUM)
               ALIST)))

(PAIRLIS
  (CL:LAMBDA (KEYS DATA &OPTIONAL ALIST)                     (* kbr: "30-Jun-86 13:41")
                                                             (* Construct an association list from 
                                                             KEYS and DATA (adding to ALIST) *)
         (PROG (K D CELL ANSWER)                             (* Note: This implementation has the 
                                                             feature that the alist comes out in 
                                                             the same order that KEYS and DATA are 
                                                             input. *)
               (SETQ K KEYS)
               (SETQ D DATA)
               (COND
                  ((AND (ENDP K)
                        (ENDP D))
                   (RETURN ALIST))
                  ((OR (ENDP K)
                       (ENDP D))
                   (GO ERROR)))
               [SETQ ANSWER (LIST (CONS (CAR K)
                                        (CAR D]              (* CELL is the last cons cell in 
                                                             ANSWER. *)
               (SETQ CELL ANSWER)
           LOOP
               (SETQ K (CDR K))
               (SETQ D (CDR D))
               (COND
                  ((AND (ENDP K)
                        (ENDP D))
                   (SETF (CDR CELL)
                         ALIST)
                   (RETURN ANSWER))
                  ((OR (ENDP K)
                       (ENDP D))
                   (GO ERROR)))
               [SETF (CDR CELL)
                     (LIST (CONS (CAR K)
                                 (CAR D]
               (SETQ CELL (CDR CELL))
               (GO LOOP)
           ERROR
               (CL:ERROR "PAIRLIS keys and data have different length."))))
)
(DEFINEQ

(CL:ASSOC
  (CL:LAMBDA (ITEM ALIST &KEY TEST TEST-NOT KEY)             (* kbr: "28-Jun-86 21:46")
         (TEST-KEYS-SUPPLIED)
         (%%ASSOC ITEM ALIST TEST TEST-NOT KEY)))

(%%ASSOC
  [CL:LAMBDA (ITEM ALIST TEST TEST-NOT KEY)                  (* kbr: "28-Jun-86 21:52")
         (CL:DO ((ALIST ALIST (CDR ALIST)))
                ((ENDP ALIST))
                (CL:IF (CAR ALIST)
                       (CL:IF (%%TEST ITEM (CAAR ALIST)
                                     TEST TEST-NOT KEY)
                              (RETURN (CAR ALIST])

(ASSOC-IF
  [CL:LAMBDA (PREDICATE ALIST)                               (* kbr: "25-Jun-86 17:46")
                                                             (* Returns the first cons in ALIST 
                                                             whose car satisfies the PREDICATE *)
         (CL:DO ((ALIST ALIST (CDR ALIST)))
                ((ENDP ALIST))
                (CL:IF (CAR ALIST)
                       (CL:IF (FUNCALL PREDICATE (CAAR ALIST))
                              (RETURN (CAR ALIST])

(ASSOC-IF-NOT
  [CL:LAMBDA (PREDICATE ALIST)                               (* kbr: "25-Jun-86 17:47")
                                                             (* Returns the first cons in ALIST 
                                                             whose car does not satisfy the 
                                                             PREDICATE *)
         (CL:DO ((ALIST ALIST (CDR ALIST)))
                ((ENDP ALIST))
                (CL:IF (CAR ALIST)
                       (CL:IF (NOT (FUNCALL PREDICATE (CAAR ALIST)))
                              (RETURN (CAR ALIST])

(RASSOC
  [CL:LAMBDA (ITEM ALIST &KEY TEST TEST-NOT KEY)
         (DECLARE (TYPE LIST ALIST))                         (* kbr: "28-Jun-86 21:47")
         (TEST-KEYS-SUPPLIED)
         (CL:DO ((ALIST ALIST (CDR ALIST)))
                ((ENDP ALIST))
                (CL:IF (CAR ALIST)
                       (CL:IF (%%TEST ITEM (CDAR ALIST)
                                     TEST TEST-NOT KEY)
                              (RETURN (CAR ALIST])

(RASSOC-IF
  [CL:LAMBDA (PREDICATE ALIST)                               (* kbr: "25-Jun-86 17:51")
                                                             (* Returns the first cons in ALIST 
                                                             whose cdr satisfies the PREDICATE *)
         (CL:DO ((ALIST ALIST (CDR ALIST)))
                ((ENDP ALIST))
                (CL:IF (CAR ALIST)
                       (CL:IF (FUNCALL PREDICATE (CDAR ALIST))
                              (RETURN (CAR ALIST])

(RASSOC-IF-NOT
  [CL:LAMBDA (PREDICATE ALIST)                               (* kbr: "25-Jun-86 17:52")
                                                             (* Returns the first cons in ALIST 
                                                             whose cdr does not satisfy the 
                                                             PREDICATE *)
         (CL:DO ((ALIST ALIST (CDR ALIST)))
                ((ENDP ALIST))
                (CL:IF (CAR ALIST)
                       (CL:IF (NOT (FUNCALL PREDICATE (CDAR ALIST)))
                              (RETURN (CAR ALIST])
)



(* ;; "Section 7.8.4 Mapping")

(DEFINEQ

(CL:MAPCAR
  (CL:LAMBDA (FUNCTION LIST &REST MORE-LISTS)                (* raf "18-Dec-85 17:28")
         (%%MAP FUNCTION (CONS LIST MORE-LISTS)
                :LIST T)))

(CL:MAPLIST
  (CL:LAMBDA (FUNCTION LIST &REST MORE-LISTS)                (* kbr: " 4-Jun-86 23:47")
                                                             (* Applies FUNCTION to successive CDRs 
                                                             of list, returns list of results.
                                                             *)
         (%%MAP FUNCTION (CONS LIST MORE-LISTS)
                :LIST NIL)))

(CL:MAPC
  (CL:LAMBDA (FUNCTION LIST &REST MORE-LISTS)                (* kbr: " 4-Jun-86 23:46")
                                                             (* Applies FUNCTION to successive 
                                                             elements of lists, returns NIL . *)
         (%%MAP FUNCTION (CONS LIST MORE-LISTS)
                NIL T)))

(MAPL
  (CL:LAMBDA (FUNCTION LIST &REST MORE-LISTS)                (* kbr: " 4-Jun-86 23:47")
                                                             (* Applies FUNCTION to successive CDRs 
                                                             of list, returns NIL . *)
         (%%MAP FUNCTION (CONS LIST MORE-LISTS)
                NIL NIL)))

(MAPCAN
  (CL:LAMBDA (FUNCTION LIST &REST MORE-LISTS)                (* kbr: " 4-Jun-86 23:47")
                                                             (* Applies FUNCTION to successive 
                                                             elements of list, returns NCONC of 
                                                             results. *)
         (%%MAP FUNCTION (CONS LIST MORE-LISTS)
                :NCONC T)))

(CL:MAPCON
  (CL:LAMBDA (FUNCTION LIST &REST MORE-LISTS)                (* kbr: " 4-Jun-86 23:48")
                                                             (* Applies FUNCTION to successive CDRs 
                                                             of lists, returns NCONC of results.
                                                             *)
         (%%MAP FUNCTION (CONS LIST MORE-LISTS)
                :NCONC NIL)))

(%%MAP
  [CL:LAMBDA (FUNCTION ORIGINAL-ARGLISTS ACCUMULATE TAKE-CAR)(* kbr: " 4-Jun-86 23:46")
          
          (* 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))
               (CL:DO ((RES NIL)
                       (ARGS (QUOTE NIL)
                             (QUOTE NIL)))
                      [(DOLIST (X ARGLISTS NIL)
                              (COND
                                 ((NULL X)
                                  (RETURN T))
                                 (T NIL)))
                       (COND
                          (ACCUMULATE (CDR RET-LIST))
                          (T (CAR ORIGINAL-ARGLISTS]
                      (CL:DO ((L ARGLISTS (CDR L)))
                             ((NULL L))
                             [push ARGS (COND
                                           (TAKE-CAR (CAAR L))
                                           (T (CAR L]
                             (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])
)



(* ;; "Stuff I'm not sure where to place yet.")




(* ;; 
"It seems that DELQ ASSQ MEMQ aren't part of Common Lisp but part of Spice Lisp.  What functions in the system are currently depending on these functions?"
)

(DEFINEQ

(DELQ
  [CL:LAMBDA (ITEM LIST &OPTIONAL (N 0 NP))                  (* kbr: " 4-Jun-86 23:48")
         (DECLARE (TYPE FIXNUM N))                           (* Returns list with all
                                                             (up to N) elements with all elements 
                                                             EQ to ITEM deleted *)
         (CL:DO ((X LIST (CDR X))
                 (SPLICE (QUOTE NIL)))
                ((OR (ENDP X)
                     (AND NP (ZEROP N)))
                 LIST)
                (COND
                   [(EQ ITEM (CAR X))
                    (SETQ N (1- N))
                    (COND
                       ((NULL SPLICE)
                        (SETQ LIST (CDR X)))
                       (T (RPLACD SPLICE (CDR X]
                   (T (SETQ SPLICE X])
)

(PUTPROPS CL:LIST-EVERY DMACRO 
          (DEFMACRO (FUNCTION &REST LISTS) (LET
                                            ((GENSYM (GENSYM))
                                             (TAIL-VARS (for X in LISTS collect (GENSYM))))
                                            (BQUOTE
                                             (LET
                                              (((\, GENSYM)
                                                T))
                                              (CL:DO
                                               ((\,@ (for X in LISTS as TAIL-VAR in TAIL-VARS collect
                                                          (LIST TAIL-VAR X (BQUOTE
                                                                            (CDR (\, TAIL-VAR)))))))
                                               ((NOT (AND (\,@ TAIL-VARS)))
                                                (\, GENSYM))
                                               (OR
                                                (SETQ
                                                 (\, GENSYM)
                                                 (AND
                                                  (\, GENSYM)
                                                  (FUNCALL
                                                   (\, FUNCTION)
                                                   (\,@ (for X in TAIL-VARS collect
                                                             (BQUOTE (CAR (\, X))))))))
                                                (RETURN NIL)))))) )
)
(MOVD (QUOTE ASSOC)
      (QUOTE ASSQ))
(MOVD (QUOTE FMEMB)
      (QUOTE MEMQ))
(DECLARE: DONTEVAL@LOAD DOEVAL@COMPILE DONTCOPY COMPILERVARS 

(ADDTOVAR NLAMA )

(ADDTOVAR NLAML )

(ADDTOVAR LAMA 
          DELQ %%MAP CL:MAPCON MAPCAN MAPL CL:MAPC CL:MAPLIST CL:MAPCAR RASSOC-IF-NOT RASSOC-IF 
               RASSOC ASSOC-IF-NOT ASSOC-IF %%ASSOC CL:ASSOC PAIRLIS ACONS SUBSETP NSET-EXCLUSIVE-OR 
               SET-EXCLUSIVE-OR NSET-DIFFERENCE SET-DIFFERENCE NINTERSECTION CL:INTERSECTION NUNION 
               CL:UNION ADJOIN MEMBER-IF-NOT MEMBER-IF %%MEMBER CL:MEMBER %%NSUBLIS NSUBLIS %%SUBLIS 
               CL:SUBLIS %%NSUBST-IF-NOT NSUBST-IF-NOT %%NSUBST-IF NSUBST-IF %%NSUBST NSUBST 
               %%SUBST-IF-NOT SUBST-IF-NOT %%SUBST-IF SUBST-IF %%SUBST CL:SUBST XXTEST0 XXTEST1 
               %%TEST CL:LDIFF NBUTLAST BUTLAST NRECONC REVAPPEND COPY-TREE COPY-ALIST CL:APPEND 
               MAKE-LIST NTHCDR REST TENTH NINTH EIGHTH SEVENTH SIXTH FIFTH FOURTH THIRD SECOND 
               CL:FIRST %%SETNTH CL:NTH LIST-LENGTH %%TREE-EQUAL-TEST-NOT %%TREE-EQUAL-TEST 
               TREE-EQUAL)
)
(PUTPROPS CMLLIST COPYRIGHT ("Xerox Corporation" 1985 1986))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (5300 6963 (TREE-EQUAL 5310 . 5952) (%%TREE-EQUAL-TEST 5954 . 6442) (
%%TREE-EQUAL-TEST-NOT 6444 . 6961)) (7001 8057 (ENDP 7011 . 7238) (LIST-LENGTH 7240 . 8055)) (8058 
9046 (CL:NTH 8068 . 8212) (%%SETNTH 8214 . 9044)) (9085 9925 (CL:FIRST 9095 . 9225) (SECOND 9227 . 
9356) (THIRD 9358 . 9412) (FOURTH 9414 . 9470) (FIFTH 9472 . 9533) (SIXTH 9535 . 9597) (SEVENTH 9599
 . 9664) (EIGHTH 9666 . 9731) (NINTH 9733 . 9800) (TENTH 9802 . 9870) (REST 9872 . 9923)) (10808 11390
 (NTHCDR 10818 . 11388)) (11432 11986 (MAKE-LIST 11442 . 11984)) (12174 13467 (CL:APPEND 12184 . 12925
) (%%APPEND 12927 . 13465)) (13468 16386 (COPY-LIST 13478 . 14005) (COPY-ALIST 14007 . 15525) (
COPY-TREE 15527 . 15989) (REVAPPEND 15991 . 16384)) (16417 16587 (NRECONC 16427 . 16585)) (16704 18634
 (BUTLAST 16714 . 17521) (NBUTLAST 17523 . 18237) (CL:LDIFF 18239 . 18632)) (19898 20261 (%%TEST 19908
 . 20259)) (20262 20617 (XXTEST1 20272 . 20447) (XXTEST0 20449 . 20615)) (20618 28263 (CL:SUBST 20628
 . 21122) (%%SUBST 21124 . 22163) (SUBST-IF 22165 . 22510) (%%SUBST-IF 22512 . 23340) (SUBST-IF-NOT 
23342 . 23761) (%%SUBST-IF-NOT 23763 . 24675) (NSUBST 24677 . 24871) (%%NSUBST 24873 . 26013) (
NSUBST-IF 26015 . 26172) (%%NSUBST-IF 26174 . 27120) (NSUBST-IF-NOT 27122 . 27287) (%%NSUBST-IF-NOT 
27289 . 28261)) (28264 31199 (CL:SUBLIS 28274 . 28657) (%%SUBLIS 28659 . 29641) (NSUBLIS 29643 . 29837
) (%%NSUBLIS 29839 . 31197)) (31250 32930 (CL:MEMBER 31260 . 31750) (%%MEMBER 31752 . 32015) (
MEMBER-IF 32017 . 32468) (MEMBER-IF-NOT 32470 . 32928)) (35189 35857 (ADJOIN 35199 . 35855)) (35858 
36945 (CL:UNION 35868 . 36455) (NUNION 36457 . 36943)) (36946 38028 (CL:INTERSECTION 36956 . 37544) (
NINTERSECTION 37546 . 38026)) (38029 39004 (SET-DIFFERENCE 38039 . 38507) (NSET-DIFFERENCE 38509 . 
39002)) (39005 43534 (SET-EXCLUSIVE-OR 39015 . 39997) (NSET-EXCLUSIVE-OR 39999 . 43532)) (43535 43844 
(SUBSETP 43545 . 43842)) (43894 46173 (ACONS 43904 . 44267) (PAIRLIS 44269 . 46171)) (46174 49579 (
CL:ASSOC 46184 . 46377) (%%ASSOC 46379 . 46768) (ASSOC-IF 46770 . 47310) (ASSOC-IF-NOT 47312 . 47935) 
(RASSOC 47937 . 48408) (RASSOC-IF 48410 . 48951) (RASSOC-IF-NOT 48953 . 49577)) (49619 53678 (
CL:MAPCAR 49629 . 49817) (CL:MAPLIST 49819 . 50283) (CL:MAPC 50285 . 50671) (MAPL 50673 . 51053) (
MAPCAN 51055 . 51520) (CL:MAPCON 51522 . 51988) (%%MAP 51990 . 53676)) (53908 54773 (DELQ 53918 . 
54771)))))
STOP