Programming Language - Common Lisp 14. Conses
Programming Language - Common Lisp 14. Conses
Programming Language - Common Lisp 14. Conses
14. Conses
A cons is a compound data object having two components called the car and the cdr . A list is a chain of conses in which the car of each cons is an element of the list , and the cdr of
each cons is either the next link in the chain or a terminating atom .
car cons rplacd
cdr rplaca A proper list is a list terminated by the empty list . The empty list is a proper list , but is not a
cons .
Figure 14{1. Some dened names relating to conses. An improper list is a list that is not a proper list ; that is, it is a circular list or a dotted list .
Depending on context, a group of connected conses can be viewed in a variety of dierent ways. A dotted list is a list that has a terminating atom that is not the empty list . A non-nil atom by
A variety of operations is provided to support each of these various views. itself is not considered to be a list of any kind|not even a dotted list .
A circular list is a chain of conses that has no termination because some cons in the chain is the
14.1.1 Conses as Trees cdr of a later cons .
A tree is a binary recursive data structure made up of conses and atoms : the conses are them- append last nbutlast rest
selves also trees (sometimes called \subtrees" or \branches"), and the atoms are terminal nodes butlast ldi nconc revappend
(sometimes called leaves ). Typically, the leaves represent data while the branches establish some copy-alist list ninth second
relationship among that data. copy-list list* nreconc seventh
eighth list-length nth sixth
caaaar caddar cdar nsubst endp make-list nthcdr tailp
caaadr cadddr cddaar nsubst-if fth member pop tenth
caaar caddr cddadr nsubst-if-not rst member-if push third
caadar cadr cddar nthcdr fourth member-if-not pushnew
caaddr cdaaar cdddar sublis
caadr cdaadr cddddr subst Figure 14{3. Some dened names relating to lists.
caar cdaar cdddr subst-if
cadaar cdadar cddr subst-if-not
cadadr cdaddr copy-tree tree-equal
cadar cdadr nsublis 14.1.2.1 Lists as Association Lists
Figure 14{2. Some dened names relating to trees. An association list is a list of conses representing an association of keys with values , where the
car of each cons is the key and the cdr is the value associated with that key .
acons assoc-if pairlis rassoc-if
14.1.1.1 General Restrictions on Parameters that must be Trees assoc assoc-if-not rassoc rassoc-if-not
Except as explicitly stated otherwise, for any standardized function that takes a parameter that is Figure 14{4. Some dened names related to assocation lists.
required to be a tree , the consequences are undened if that tree is circular.
This denotes the set of conses whose car is constrained to be of type car-typespec and whose cdr (cons 'a (cons 'b (cons 'c '()))) !(A B C)
is constrained to be of type cdr-typespec . (If either car-typespec or cdr-typespec is *, it is as if the (cons 'a '(b c d)) ! (A B C D)
type t had been denoted.)
See Also:
See Also: list
Section 2.4.1 (Left-Parenthesis), Section 22.1.3.5 (Printing Lists and Conses)
Notes:
If object-2 is a list , cons can be thought of as producing a new list which is like it but has object-
atom Type 1 prepended.
Description: Notes:
Returns true if object is of type cons; otherwise, returns false .
(atom object (typep object 'atom) (not (consp object ))
)
Examples: (not (typep object 'cons)) (typep object '(not cons))
(consp nil) ! false
! true
rplaca, rplacd
(consp (cons 1 2))
car, cdr, caar, cadr, cdar, cddr, caaar, caadr, cadar, ::: car, cdr, caar, cadr, cdar, cddr, caaar, caadr, cadar, :::
car, cdr, caar, cadr, cdar, cddr, caaar, caadr, cadar, caddr: [ kad dr ] or [ _ r ]
ka dud
caddr, cdaar, cdadr, cddar, cdddr, caaaar, caaadr, cdr: [ ku_ dr ]
caadar, caaddr, cadaar, cadadr, caddar, cadddr,
cdaaar, cdaadr, cdadar, cdaddr, cddaar, cddadr, cddr: [ kud
_ dr ] or [ _ r ]
k dud
car, cdr, caar, cadr, cdar, cddr, caaar, caadr, cadar, :::
sublis, nsublis
(cons 2 (list 'a 'b 'c)))) Description:
! ((1 . "one") (2 A B C)) sublis makes substitutions for objects in tree (a structure of conses ). nsublis is like sublis but
(setq object-too object) !((1 . "one") (2 A B C)) destructively modies the relevant parts of the tree .
(setq copy-as-list (copy-list object))
(setq copy-as-alist (copy-alist object)) sublis looks at all subtrees and leaves of tree ; if a subtree or leaf appears as a key in alist (that
(setq copy-as-tree (copy-tree object)) is, the key and the subtree or leaf satisfy the test ), it is replaced by the object with which that
(eq object object-too) ! true key is associated. This operation is non-destructive. In eect, sublis can perform several subst
(eq copy-as-tree object) ! false operations simultaneously.
(eql copy-as-tree object) ! false If sublis succeeds, a new copy of tree is returned in which each occurrence of such a subtree or
(equal copy-as-tree object) ! true leaf is replaced by the object with which it is associated. If no changes are made, the original tree
(setf (first (cdr (second object))) "a"
(car (second object)) "two" is returned. The original tree is left unchanged, but the result tree may share cells with it.
(car object) '(one . 1)) !(ONE . 1)
nsublis is permitted to modify tree but otherwise returns the same values as sublis.
object ! ((ONE . 1) ("two" "a" B C))
object-too
copy-as-list
!
!
((ONE . 1) ("two" "a" B C))
((1 . "one") ("two" "a" B C))
Examples:
copy-as-alist ! ((1 . "one") (2 "a" B C)) (sublis '((x . 100) (z . zprime))
copy-as-tree ! ((1 . "one") (2 A B C)) '(plus x (minus g z x p) 4 . x))
!
See Also: (PLUS 100 (MINUS G ZPRIME 100 P) 4 . 100)
(sublis '(((+ x y) . (- x y)) ((- x y) . (+ x y)))
tree-equal '(* (/ (+ x y) (+ x p)) (- x y))
:test #'equal)
!
sublis, nsublis
(* (/ (- X Y) (+ X P)) (+ X Y))
Function !
(setq tree1 '(1 (1 2) ((1 2 3)) (((1 2 3 4)))))
(1 (1 2) ((1 2 3)) (((1 2 3 4))))
(sublis '((3 . "three")) tree1)
Syntax: ! (1 (1 2) ((1 2 "three")) (((1 2 "three" 4))))
sublis alist tree &key key test test-not ! new-tree (sublis '((t . "string"))
(sublis '((1 . "") (4 . 44)) tree1)
nsublis alist tree &key key test test-not ! new-tree :key #'stringp)
! ("string" ("string" 2) (("string" 2 3)) ((("string" 2 3 44))))
Arguments and Values: tree1 ! (1 (1 2) ((1 2 3)) (((1 2 3 4))))
alist |an association list . (setq tree2 '("one" ("one" "two") (("one" "Two" "three"))))
! ("one" ("one" "two") (("one" "Two" "three")))
tree |a tree . (sublis '(("two" . 2)) tree2)
test |a designator for a function of two arguments that returns a generalized boolean .
! ("one" ("one" "two") (("one" "Two" "three")))
tree2 ! ("one" ("one" "two") (("one" "Two" "three")))
test-not |a designator for a function of two arguments that returns a generalized boolean . !
(sublis '(("two" . 2)) tree2 :test 'equal)
("one" ("one" 2) (("one" "Two" "three")))
key |a designator for a function of one argument, or nil.
(nsublis '((t . 'temp))
new-tree |a tree . tree1
:key #'(lambda (x) (or (atom x) (< (list-length x) 3))))
! ((QUOTE TEMP) (QUOTE TEMP) QUOTE TEMP)
Side Eects: predicate |a symbol that names a function , or a function of one argument that returns a general-
nsublis modies tree . ized boolean value.
See Also: tree |a tree .
subst, Section 3.2.1 (Compiler Terminology), Section 3.6 (Traversal Rules and Side Eects) test |a designator for a function of two arguments that returns a generalized boolean .
Notes: test-not |a designator for a function of two arguments that returns a generalized boolean .
The :test-not parameter is deprecated. key |a designator for a function of one argument, or nil.
Because the side-eecting variants (e.g., nsublis) potentially change the path that is being new-tree |a tree .
traversed, their eects in the presence of shared or circular structure structure may vary in
surprising ways when compared to their non-side-eecting alternatives. To see this, consider the
following side-eect behavior, which might be exhibited by some implementations:
Description:
subst, subst-if , and subst-if-not perform substitution operations on tree . Each function searches
(defun test-it (fn) tree for occurrences of a particular old item of an element or subexpression that satises the test .
(let* ((shared-piece (list 'a 'b))
nsubst, nsubst-if , and nsubst-if-not are like subst, subst-if , and subst-if-not respectively,
(data (list shared-piece shared-piece)))
(funcall fn '((a . b) (b . a)) data)))
except that the original tree is modied.
(test-it #'sublis) !
((B A) (B A)) subst makes a copy of tree , substituting new for every subtree or leaf of tree (whether the subtree
(test-it #'nsublis) !
((A B) (A B)) or leaf is a car or a cdr of its parent) such that old and the subtree or leaf satisfy the test .
nsubst is a destructive version of subst. The list structure of tree is altered by destructively
replacing with new each leaf of the tree such that old and the leaf satisfy the test .
subst, subst-if, subst-if-not, nsubst, nsubst-if, For subst, subst-if , and subst-if-not, if the functions succeed, a new copy of the tree is returned
nsubst-if-not Function in which each occurrence of such an element is replaced by the new element or subexpression. If
no changes are made, the original tree may be returned. The original tree is left unchanged, but
the result tree may share storage with it.
Syntax: For nsubst, nsubst-if , and nsubst-if-not the original tree is modied and returned as the func-
subst new old tree &key key test test-not ! new-tree tion result, but the result may not be eq to tree .
subst-if new predicate tree &key key ! new-tree Examples:
subst-if-not new predicate tree &key key ! new-tree (setq tree1 '(1 (1 2) (1 2 3) (1 2 3 4))) !
(1 (1 2) (1 2 3) (1 2 3 4))
nsubst new old tree &key key test test-not ! new-tree (subst "two" 2 tree1) !(1 (1 "two") (1 "two" 3) (1 "two" 3 4))
(subst "five" 5 tree1) ! (1 (1 2) (1 2 3) (1 2 3 4))
nsubst-if new predicate tree &key key ! new-tree (eq tree1 (subst "five" 5 tree1)) ! implementation-dependent
nsubst-if-not new predicate tree &key key ! new-tree (subst 'tempest 'hurricane
'(shakespeare wrote (the hurricane)))
tree-equal
:test #'equal)
! ((OLD . SPICE) ((OLD . SHOES) A . CONS) (A . CONS))
tree-equal Function
list, list
list, list Function
copy-list Function Syntax:
list &rest objects ! list
Syntax: list* &rest objects+ ! result
copy-list list ! copy
Arguments and Values: Arguments and Values:
list |a proper list or a dotted list . object |an object .
copy |a list . list |a list .
Description: result |an object .
Returns a copy of list . If list is a dotted list , the resulting list will also be a dotted list . Description:
Only the list structure of list is copied; the elements of the resulting list are the same as the list returns a list containing the supplied objects .
corresponding elements of the given list . list* is like list except that the last argument to list becomes the car of the last cons constructed,
Examples: while the last argument to list* becomes the cdr of the last cons constructed. Hence, any given
call to list* always produces one fewer conses than a call to list with the same number of argu-
(setq lst (list 1 (list 2 3))) !
(1 (2 3))
ments.
(setq slst lst) !(1 (2 3)) If the last argument to list* is a list , the eect is to construct a new list which is similar, but
(setq clst (copy-list lst)) !
(1 (2 3))
which has additional elements added to the front corresponding to the preceding arguments of
(eq slst lst) ! true list*.
(eq clst lst) ! false
(equal clst lst) ! true If list* receives only one object , that object is returned, regardless of whether or not it is a list .
!
Examples:
(rplaca lst "one") ("one" (2 3))
slst ! ("one" (2 3))
clst ! (1 (2 3))
(setf (caadr lst) "two") !
"two" (list 1) ! (1)
lst ! ("one" ("two" 3)) (list* 1) ! 1
slst ! ("one" ("two" 3)) (setq a 1)! 1
clst ! (1 ("two" 3)) (list a 2)! (1 2)
'(a 2) ! (A 2)
Exceptional Situations: (list 'a 2)! (A 2)
The consequences are undened if list is a circular list . (list* a 2)! (1 . 2)
(list) ! i.e.
NIL ; , ()
See Also: !
(setq a '(1 2)) (1 2)
! true
copy-alist, copy-seq, copy-tree (eq a (list* a))
(list 3 4 'a (car '(b . c)) (+ 6 -2)) !
(3 4 A B 4)
Notes:
(list* 'a 'b 'c 'd) (cons 'a (cons 'b (cons 'c 'd)))
!
! (A B C . D)
The copy created is equal to list , but not eq. (list* 'a 'b 'c '(d e f)) (A B C D E F)
See Also:
cons
list-length
;; If fast pointer eventually equals slow pointer,
Function ;; then we must be stuck in a circular list.
;; (A deeper property is the converse: if we are
;; stuck in a circular list, then eventually the
Syntax: ;; fast pointer will equal the slow pointer.
Description:
push prepends item to the list that is stored in place , stores the resulting list in place , and
make-list Function returns the list .
For information about the evaluation of subforms of place , see Section 5.1.1.1 (Evaluation of
Syntax: Subforms to Places).
make-list size &key initial-element ! list Examples:
Arguments and Values: (setq llst '(nil)) ! (NIL)
size |a non-negative integer . (push 1 (car llst)) ! (1)
For information about the evaluation of subforms of place , see Section 5.1.1.1 (Evaluation of object , new-object |an object .
Subforms to Places).
Description:
Examples: The functions rst, second, third, fourth, fth, sixth, seventh, eighth, ninth, and tenth access
! the rst, second, third, fourth, fth, sixth, seventh, eighth, ninth, and tenth elements of list ,
(setq stack '(a b c)) (A B C) respectively. Specically,
(pop stack) !A
stack ! (B C) (first list ) (car list
)
(setq llst '((1 2 3 4))) !
((1 2 3 4)) (second list ) (car (cdr list
))
(pop (car llst)) !
1 (third list ) (car (cddr list
))
llst ! ((2 3 4)) (fourth list ) (car (cdddr list
))
list ) list
Side Eects: (fifth
(sixth list )
(car
(car
(cddddr ))
(cdr (cddddr ))) list
The contents of place are modied. (seventh list ) (car (cddr (cddddr ))) list
list ) list
See Also: (eighth
(ninth list )
(car
(car
(cdddr (cddddr
(cddddr (cddddr
)))
))) list
push, pushnew, Section 5.1 (Generalized Reference) (tenth list ) (car (cdr (cddddr (cddddr list ))))
Notes: setf can also be used with any of these functions to change an existing component. The same
equivalences apply. For example:
The eect of (pop place ) is roughly equivalent to
(prog1 (car place ) (setf place (cdr place ))) (setf (fifth list ) new-object ) (setf (car (cddddr list )) new-object )
except that the latter would evaluate any subforms of place three times, while pop evaluates them Examples:
only once.
(setq lst '(1 2 3 (4 5 6) ((V)) vi 7 8 9 10))
! (1 2 3 (4 5 6) ((V)) VI 7 8 9 10)
endp Function
nth Accessor Syntax:
endp list ! generalized-boolean
Syntax: Arguments and Values:
nth n list ! object
list |a list , which might be a dotted list or a circular list .
(setf (nth n list) new-object)
generalized-boolean|a generalized boolean .
Arguments and Values: Description:
n|a non-negative integer . Returns true if list is the empty list . Returns false if list is a cons .
list |a list , which might be a dotted list or a circular list . Examples:
object |an object . (endp nil) ! true
(endp '(1 2)) ! false
new-object |an object . (endp (cddr '(1 2))) ! true
Description: Exceptional Situations:
nth locates the nth element of list , where the car of the list is the \zeroth" element. Specically, Should signal an error of type type-error if list is not a list .
(nth n list ) (car (nthcdr n list ))
Notes:
nth may be used to specify a place to setf . Specically, The purpose of endp is to test for the end of proper list . Since endp does not descend into a
cons , it is well-dened to pass it a dotted list . However, if shorter \lists" are iteratively produced
(setf (nth n list ) new-object ) (setf (car (nthcdr n list )) new-object ) by calling cdr on such a dotted list and those \lists" are tested with endp, a situation that has
undened consequences will eventually result when the non-nil atom (which is not in fact a list )
nally becomes the argument to endp. Since this is the usual way in which endp is used, it is
Examples: conservative programming style and consistent with the intent of endp to treat endp as simply
! a function on proper lists which happens not to enforce an argument type of proper list except
(nth 0 '(foo bar baz)) FOO when the argument is atomic .
(nth 1 '(foo bar baz)) !BAR
(nth 3 '(foo bar baz)) !NIL
(setq 0-to-3
(setf (nth 2
0-to-3 ! (0
(list 0 1 2 3))
0-to-3) "two")
1 "two" 3)
! (0 1 2 3)
! "two" null Function
Description: Examples:
Returns t if object is the empty list ; otherwise, returns nil.
(nconc) ! NIL
Examples: (setq x '(a b c))
(setq y '(d e f))
!
!
(A B C)
(D E F)
(null !T
'()) (nconc x y) ! (A B C D E F)
(null !T
nil) x ! (A B C D E F)
(null t)! NIL Note, in the example, that the value of x is now dierent, since its last cons has been rplacd'd
(null 1)! NIL to the value of y. If (nconc x y) were evaluated again, it would yield a piece of a circular list ,
whose printed representation would be (A B C D E F D E F D E F ...), repeating forever; if the
See Also: *print-circle* switch were non-nil , it would be printed as (A B C . #1=(D E F . #1#)).
not (setq foo (list 'a 'b 'c 'd 'e)
Notes: bar
baz
(list 'f 'g 'h 'i 'j)
(list 'k 'l 'm)) ! (K L M)
null is intended to be used to test for the empty list whereas not is intended to be used to invert (setq foo (nconc foo bar baz)) ! (A B C D E F G H I J K L M)
a boolean (or generalized boolean ). Operationally, null and not compute the same result; which to foo ! (A B C D E F G H I J K L M)
use is a matter of style. bar ! (F G H I J K L M)
nconc
bar (list 'f 'g 'h 'i 'j)
Syntax: bar ! (F G H I J K L M)
baz ! (K L M)
nconc &rest lists ! concatenated-list
Arguments and Values: Side Eects:
list |each but the last must be a list (which might be a dotted list but must not be a circular The lists are modied rather than copied.
list ); the last list may be any object .
concatenated-list |a list .
See Also:
append, concatenate
Description:
Returns a list that is the concatenation of lists . If no lists are supplied, (nconc) returns nil. nconc
is dened using the following recursive relationship: append Function
(nconc) ! ()
(nconc nil . lists ) (nconc . lists )
list ! list
Syntax:
(nconc
(nconc
)
list-1 list-2 ) (progn (rplacd (last list-1 ) list-2 ) list-1 ) append &rest lists ! result
(nconc list-1 list-2 . lists ) (nconc (nconc list-1 list-2 ) . lists ) Arguments and Values:
list |each must be a proper list except the last, which may be any object .
revappend, nreconc
result |an object . This will be a list unless the last list was not a list and all preceding lists were Examples:
null .
(let ((list-1 (list 1 2 3))
Description: (list-2 (list 'a 'b 'c)))
append returns a new list that is the concatenation of the copies. lists are left unchanged; the list (print (revappend list-1 list-2))
structure of each of lists except the last is copied. The last argument is not copied; it becomes the (print (equal list-1 '(1 2 3)))
cdr of the nal dotted pair of the concatenation of the preceding lists , or is returned directly if (print (equal list-2 '(a b c))))
there are no preceding non-empty lists . . (3 2 1 A B C)
. T
Examples: . T
! T
(append '(a b c) '(d e f) '() '(g)) ! (A B C D E F G)
(append '(a b c) 'd) !
(A B C . D) (revappend '(1 2 3) '()) ! (3 2 1)
(setq lst '(a b c)) !
(A B C) (revappend '(1 !
2 3) '(a . b)) (3 2 1 A . B)
(append lst '(d)) !
(A B C D) (revappend '() '(a b c)) ! (A B C)
lst ! (A B C) (revappend '(1 2 3) 'a) !(3 2 1 . A)
(append) !NIL (revappend '() 'a) ! A ;degenerate case
(append 'a) !
A
revappend, nreconc
(print (equal list-2 '(a b c))))
Function . (3 2 1 A B C)
. NIL
. T
Syntax: ! T
(revappend list tail ) (nconc (reverse list ) tail ) (nbutlast (list 'a)) ! NIL
(nreconc list tail ) (nconc (nreverse list ) tail ) (nbutlast '()) !NIL
Exceptional Situations:
Should signal an error of type type-error if list is not a proper list or a dotted list . Should signal
butlast, nbutlast Function an error of type type-error if n is not a non-negative integer .
Notes:
Syntax: (butlast list n) (ldiff list (last list n))
butlast list &optional n ! result-list
nbutlast list &optional n ! result-list
Arguments and Values:
list |a list , which might be a dotted list but must not be a circular list .
last Function
n|a non-negative integer . Syntax:
result-list |a list . last list &optional n ! tail
Description: Arguments and Values:
butlast returns a copy of list from which the last n conses have been omitted. If n is not supplied, list |a list , which might be a dotted list but must not be a circular list .
its value is 1. If there are fewer than n conses in list , nil is returned and, in the case of nbutlast, n|a non-negative integer . The default is 1.
list is not modied.
tail |an object .
nbutlast is like butlast, but nbutlast may modify list . It changes the cdr of the cons n+1 from
the end of the list to nil. Description:
Examples: last returns the last n conses (not the last n elements) of list ). If list is (), last returns ().
! If n is zero, the atom that terminates list is returned. If n is greater than or equal to the number
(setq lst '(1 2 3 4 5 6 7 8 9))
(butlast lst) ! (1 2 3 4 5 6 7 8)
(1 2 3 4 5 6 7 8 9)
of cons cells in list , the result is list .
(butlast lst 5) ! (1 2 3 4)
(butlast lst (+ 5 5)) !NIL
Examples:
lst ! (1 2 3 4 5 6 7 8 9) (last nil) !NIL
(nbutlast lst 3) ! (1 2 3 4 5 6) (last '(1 2 3)) !(3)
lst ! (1 2 3 4 5 6) (last '(1 2 . 3)) ! (2 . 3)
(nbutlast lst 99) ! NIL (setq x (list 'a 'b 'c 'd)) !
(A B C D)
lst ! (1 2 3 4 5 6) (last x) !(D)
(butlast '(a b c d)) !(A B C) (rplacd (last x) (list 'e 'f)) x !
(A B C D E F)
(butlast '((a b) (c d))) !
((A B)) (last x) !(F)
(butlast '(a)) ! NIL
(butlast nil) ! NIL (last '(a b c)) ! (C)
(setq foo (list 'a 'b 'c 'd)) !
(A B C D)
(nbutlast foo) ! (A B C) (last '(a b c) 0) ! ()
foo ! (A B C) (last '(a b c) 1) ! (C)
ldi, tailp
(last '(a b c) 2) ! (B C) Description:
(last '(a b c) 3) ! (A B C) If object is the same as some tail of list , tailp returns true ; otherwise, it returns false .
(last '(a b c) 4) ! (A B C)
If object is the same as some tail of list , ldi returns a fresh list of the elements of list that
(last '(a . b) 0) ! B precede object in the list structure of list ; otherwise, it returns a copy 2 of list .
(last '(a . b) 1) ! (A . B)
(last '(a . b) 2) ! (A . B) Examples:
(let ((lists '#((a b c) (a b c . d))))
Exceptional Situations: (dotimes (i (length lists)) ()
The consequences are undened if list is a circular list . Should signal an error of type type-error (let ((list (aref lists i)))
The following code could be used to dene last. (format t "~& object=~S ~21T~S ~44T~S"
object (tailp object list) (ldiff list object))))))))
(defun last (list &optional (n 1)) .
(check-type n (integer 0)) . list=(A B C) (tailp object list) (ldiff list object)
(do ((l list (cdr l)) . object=(A B C) T NIL
(r list) . object=(C) T (A B)
(i 0 (+ i 1))) . object=(C) NIL (A B C)
((atom l) r) . object=(F G H) NIL (A B C)
(if (>= i n) (pop r)))) . object=NIL T (A B C)
. object=D NIL (A B C)
. object=X NIL (A B C)
.
T
(ldiff list object)
NIL
(A B)
Syntax: . object=(C . D)
. object=(F G H)
NIL
NIL
(A B C . D)
(A B C . D)
ldi list object ! result-list . object=NIL NIL (A B C . D)
. object=D T (A B C)
tailp object list ! generalized-boolean . object=X NIL (A B C . D)
Examples:
See Also: (nthcdr 0 '()) ! NIL
set-dierence (nthcdr 3 '()) ! NIL
(nthcdr 0 '(a b c)) ! (A B C)
Notes: (nthcdr 2 '(a b c)) ! (C)
If the list is a circular list , tailp will reliably yield a value only if the given object is in fact a (nthcdr 4 '(a b c)) ! ()
tail of list . Otherwise, the consequences are unspecied: a given implementation which detects (nthcdr 1 '(0 . 1)) ! 1
the circularity must return false , but since an implementation is not obliged to detect such a
situation , tailp might just loop indenitely without returning in that case. (locally (declare (optimize (safety 3)))
(nthcdr 3 '(0 . 1)))
tailp could be dened as follows: Error: Attempted to take CDR of 1.
item|an object . the value returned by member is identical to the portion of the list beginning with a. Thus
list |a proper list . rplaca on the result of member can be used to alter the part of the list where a was found
(assuming a check has been made that member did not return nil).
predicate |a designator for a function of one argument that returns a generalized boolean .
test |a designator for a function of two arguments that returns a generalized boolean .
test-not |a designator for a function of two arguments that returns a generalized boolean .
mapc,
Function
mapcar, mapcan, mapl, maplist, mapcon
key |a designator for a function of one argument, or nil.
tail |a list . Syntax:
mapc function &rest lists+ ! list-1
Description: mapcar function &rest lists+ ! result-list
member, member-if , and member-if-not each search list for item or for a top-level element that
satises the test . The argument to the predicate function is an element of list . mapcan function &rest lists+ ! concatenated-results
If some element satises the test , the tail of list beginning with this element is returned; otherwise
nil is returned. mapl function &rest lists+ ! list-1
list is searched on the top level only. maplist function &rest lists+ ! result-list
are equivalent in meaning with one exception: if nil appears in alist in place of a pair, and item
is nil, nd will compute the car of the nil in alist , nd that it is equal to item, and return nil,
pairlis Function
whereas assoc will ignore the nil in alist and continue to search for an actual cons whose car is
nil. Syntax:
pairlis keys data &optional alist ! new-alist
copy-alist Function Arguments and Values:
keys |a proper list .
Syntax: data|a proper list .
copy-alist alist ! new-alist alist |an association list . The default is the empty list .
Arguments and Values: new-alist |an association list .
alist |an association list .
Description:
new-alist |an association list . Returns an association list that associates elements of keys to corresponding elements of data.
The consequences are undened if keys and data are not of the same length .
Description:
copy-alist returns a copy of alist . If alist is supplied, pairlis returns a modied alist with the new pairs prepended to it. The new
pairs may appear in the resulting association list in either forward or backward order. The result
The list structure of alist is copied, and the elements of alist which are conses are also copied (as of
conses only). Any other objects which are referred to, whether directly or indirectly, by the alist
continue to be shared. (pairlis '(one two) '(1 2) '((three . 3) (four . 19)))
might be
((one . 1) (two . 2) (three . 3) (four . 19))
or Description:
rassoc , rassoc-if , and rassoc-if-not return the rst cons whose cdr satises the test . If no such
((two . 2) (one . 1) (three . 3) (four . 19))
cons is found, nil is returned.
Examples: If nil appears in alist in place of a pair, it is ignored.
(setq keys '(1 2 3)
data '("one" "two" "three")
Examples:
alist '((4 . "four"))) !
((4 . "four")) (setq alist '((1 . "one") (2 . "two") (3 . 3)))
(pairlis keys data) !((3 . "three") (2 . "two") (1 . "one")) ! ((1 . "one") (2 . "two") (3 . 3))
(pairlis keys data alist) (rassoc 3 alist) ! (3 . 3)
! ((3 . "three") (2 . "two") (1 . "one") (4 . "four")) (rassoc "two" alist) ! NIL
alist ! ((4 . "four")) (rassoc "two" alist :test 'equal) ! (2 . "two")
(rassoc 1 alist :key #'(lambda (x) (if (numberp x) (/ x 3)))) ! (3 . 3)
Exceptional Situations: (rassoc 'a '((a . b) (b . c) (c . a) (z . a))) ! (C . A)
Should be prepared to signal an error of type type-error if keys and data are not proper lists . (rassoc-if #'stringp alist) ! (1 . "one")
(rassoc-if-not #'vectorp alist) ! (3 . 3)
See Also:
acons See Also:
assoc , Section 3.6 (Traversal Rules and Side Eects)
rassoc, rassoc-if, rassoc-if-not Function Notes:
The :test-not parameter is deprecated.
Syntax: The function rassoc-if-not is deprecated.
rassoc item alist &key key test test-not ! entry It is possible to rplaca the result of rassoc, provided that it is not nil, in order to \update" alist .
rassoc-if predicate alist &key key ! entry The expressions
rassoc-if-not predicate alist &key key ! entry (rassoc item list :test fn)
and
Arguments and Values: (find item list :test fn :key #'cdr)
item|an object .
are equivalent in meaning, except when the item is nil and nil appears in place of a pair in the
alist |an association list . alist . See the function assoc.
predicate |a designator for a function of one argument that returns a generalized boolean .
test |a designator for a function of two arguments that returns a generalized boolean . get-properties Function
test-not |a designator for a function of two arguments that returns a generalized boolean .
key |a designator for a function of one argument, or nil. Syntax:
get-properties plist indicator-list ! indicator, value, tail
entry |a cons that is an element of the alist , or nil.
Arguments and Values:
getf
plist |a property list . indicator |an object .
indicator-list |a proper list (of indicators ). default |an object . The default is nil.
indicator |an object that is an element of indicator-list . value |an object .
value |an object . new-value |an object .
tail |a list . Description:
Description: getf nds a property on the plist whose property indicator is identical to indicator , and returns
its corresponding property value . If there are multiple properties 1 with that property indicator ,
get-properties is used to look up any of several property list entries all at once. getf uses the rst such property . If there is no property with that property indicator , default is
It searches the plist for the rst entry whose indicator is identical to one of the objects in returned.
indicator-list . If such an entry is found, the indicator and value returned are the property indi- setf of getf may be used to associate a new object with an existing indicator in the property list
cator and its associated property value , and the tail returned is the tail of the plist that begins held by place , or to create a new assocation if none exists. If there are multiple properties 1 with
with the found entry (i.e., whose car is the indicator ). If no such entry is found, the indicator , that property indicator , setf of getf associates the new-value with the rst such property . When
value , and tail are all nil. a getf form is used as a setf place , any default which is supplied is evaluated according to normal
Examples: left-to-right evaluation rules, but its value is ignored.
setf of getf is permitted to either write the value of place itself, or modify of any part, car or
(setq x '()) !NIL cdr , of the list structure held by place .
(setq *indicator-list* '(prop1 prop2)) !
(PROP1 PROP2)
(getf x 'prop1) !
NIL
(setf (getf x 'prop1) 'val1) !
VAL1
Examples:
(eq (getf x 'prop1) 'val1) ! true (setq x '()) !NIL
(get-properties x *indicator-list*) !
PROP1, VAL1, (PROP1 VAL1) (getf x 'prop1) !
NIL
x ! (PROP1 VAL1) (getf x 'prop1 7) !7
!
See Also:
(getf x 'prop1) NIL
(setf (getf x 'prop1) 'val1) ! VAL1
get, getf (eq (getf x 'prop1) 'val1) ! true
(getf x 'prop1) !
VAL1
!
getf
(getf x 'prop1 7) VAL1
Syntax: (setq foo (list 'a 'b 'c 'd 'e 'f)) !
(A B C D E F)
(setq bar (cddr foo)) !
(C D E F)
getf plist indicator &optional default ! value (remf foo 'c) ! true
(setf (getf place indicator &optional default) new-value) foo ! (A B E F)
bar
! (C D E F)
Arguments and Values: !
or
!
or
(C)
plist |a property list . !
or
(NIL)
(C NIL)
place |a place , the value of which is a property list . !
or
(C D)
Note that while supplying a default argument to getf in a setf situation is sometimes not very Side Eects:
interesting, it is still important because some macros, such as push and incf , require a place The property list stored in place is modied.
argument which data is both read from and written to. In such a context, if a default argument is
to be supplied for the read situation, it must be syntactically valid for the write situation as well. See Also:
For example, remprop, getf
(let ((plist '()))
(incf (getf plist 'count 0))
plist) ! (COUNT 1) intersection, nintersection Function
Syntax:
remf Macro intersection list-1 list-2 &key key test test-not ! result-list
nintersection list-1 list-2 &key key test test-not ! result-list
Syntax: Arguments and Values:
remf place indicator ! generalized-boolean list-1 |a proper list .
Arguments and Values: list-2 |a proper list .
place |a place . test |a designator for a function of two arguments that returns a generalized boolean .
indicator |an object . test-not |a designator for a function of two arguments that returns a generalized boolean .
generalized-boolean|a generalized boolean . key |a designator for a function of one argument, or nil.
Description: result-list |a list .
remf removes from the property list stored in place a property 1 with a property indicator identical
to indicator . If there are multiple properties 1 with the identical key, remf only removes the rst Description:
such property . remf returns false if no such property was found, or true if a property was found. intersection and nintersection return a list that contains every element that occurs in both list-1
The property indicator and the corresponding property value are removed in an undened order and list-2 .
by destructively splicing the property list. remf is permitted to either setf place or to setf any nintersection is the destructive version of intersection. It performs the same operation, but may
part, car or cdr, of the list structure held by that place . destroy list-1 using its cells to construct the result. list-2 is not destroyed.
For information about the evaluation of subforms of place , see Section 5.1.1.1 (Evaluation of The intersection operation is described as follows. For all possible ordered pairs consisting of one
Subforms to Places). element from list-1 and one element from list-2 , :test or :test-not are used to determine whether
they satisfy the test . The rst argument to the :test or :test-not function is an element of list-1 ;
the second argument is an element of list-2 . If :test or :test-not is not supplied, eql is used. It is
an error if :test and :test-not are supplied in the same function call.
intersection, nintersection
If :key is supplied (and not nil), it is used to extract the part to be tested from the list element. tions in portable code.
The argument to the :key function is an element of either list-1 or list-2 ; the :key function
typically returns part of the supplied element. If :key is not supplied or nil, the list-1 and list-2
elements are used. adjoin Function
For every pair that saties the test , exactly one of the two elements of the pair will be put in
the result. No element from either list appears in the result that does not satisfy the test for
an element from the other list . If one of the lists contains duplicate elements, there may be Syntax:
duplication in the result. adjoin item list &key key test test-not ! new-list
There is no guarantee that the order of elements in the result will re
ect the ordering of the Arguments and Values:
arguments in any particular way. The result list may share cells with, or be eq to, either list-1 or item|an object .
list-2 if appropriate.
list |a proper list .
Examples: test |a designator for a function of two arguments that returns a generalized boolean .
(setq list1 (list 1 1 2 3 4 a b c "A" "B" "C" "d")
list2 (list 1 4 5 b c d "a" "B" "c" "D")) test-not |a designator for a function of two arguments that returns a generalized boolean .
! (1 4 5 B C D "a" "B" "c" "D")
key |a designator for a function of one argument, or nil.
(intersection list1 list2) !
(C B 4 1 1)
(intersection list1 list2 :test 'equal) !
("B" C B 4 1 1) new-list |a list .
!
Description:
(intersection list1 list2 :test #'equalp) ("d" "C" "B" "A" C B 4 1 1)
(nintersection list1 list2) ! (1 1 4 B C)
list1 ! implementation-dependent e.g.
; , (1 1 4 B C) Tests whether item is the same as an existing element of list . If the item is not an existing ele-
list2 ! implementation-dependent e.g.
; , (1 4 5 B C D "a" "B" "c" "D") ment, adjoin adds it to list (as if by cons) and returns the resulting list ; otherwise, nothing is
(setq list1 (copy-list '((1 . 2) (2 . 3) (3 . 4) (4 . 5)))) added and the original list is returned.
! ((1 . 2) (2 . 3) (3 . 4) (4 . 5))
(setq list2 (copy-list '((1 . 3) (2 . 4) (3 . 6) (4 . 8)))) The test , test-not , and key aect how it is determined whether item is the same as an element of
! ((1 . 3) (2 . 4) (3 . 6) (4 . 8)) list . For details, see Section 17.2.1 (Satisfying a Two-Argument Test).
!
(nintersection list1 list2 :key #'cdr)
list1 ! implementation-dependent e.g.
;
((2 . 3) (3 . 4))
, ((1 . 2) (2 . 3) (3 . 4)) Examples:
list2 ! implementation-dependent e.g.
; , ((1 . 3) (2 . 4) (3 . 6) (4 . 8))
(setq slist '()) !NIL
Side Eects: (adjoin 'a slist)
!
! (A)
union, Section 3.2.1 (Compiler Terminology), Section 3.6 (Traversal Rules and Side Eects) Exceptional Situations:
Notes: Should be prepared to signal an error of type type-error if list is not a proper list .
The :test-not parameter is deprecated. See Also:
Since the nintersection side eect is not required, it should not be used in for-eect-only posi- pushnew, Section 3.6 (Traversal Rules and Side Eects)
Notes: It is implementation-dependent whether or not pushnew actually executes the storing form for its
The :test-not parameter is deprecated. place in the situation where the item is already a member of the list held by place .
(adjoin item list :key fn) Examples:
(if (member (fn item) list :key fn) list (cons item list))
(setq x '(a (b c) d)) ! (A (B C) D)
(pushnew 5 (cadr x)) !(5 B C)
!
pushnew
x (A (5 B C) D)
The result contains precisely those elements of list-1 and list-2 that appear in no matching pair. Arguments and Values:
The result list of set-exclusive-or might share storage with one of list-1 or list-2 . list-1 |a proper list .
list-2 |a proper list .
Examples:
test |a designator for a function of two arguments that returns a generalized boolean .
(setq lst1 (list 1 "a" "b")
lst2 (list 1 "A" "b")) !
(1 "A" "b") test-not |a designator for a function of two arguments that returns a generalized boolean .
(set-exclusive-or lst1 lst2) !
("b" "A" "b" "a")
key |a designator for a function of one argument, or nil.
(set-exclusive-or lst1 lst2 :test #'equal) !
("A" "a")
(set-exclusive-or lst1 lst2 :test 'equalp) !
NIL
generalized-boolean|a generalized boolean .
(nset-exclusive-or lst1 lst2) !
("a" "b" "A" "b")
!
(setq lst1 (list (("a" . "b") ("c" . "d") ("e" . "f")))) Description:
(("a" . "b") ("c" . "d") ("e" . "f"))
(setq lst2 (list (("c" . "a") ("e" . "b") ("d" . "a"))))
subsetp returns true if every element of list-1 matches some element of list-2 , and false otherwise.
! (("c" . "a") ("e" . "b") ("d" . "a")) Whether a list element is the same as another list element is determined by the functions specied
(nset-exclusive-or lst1 lst2 :test #'string= :key #'cdr) by the keyword arguments. The rst argument to the :test or :test-not function is typically
! (("c" . "d") ("e" . "f") ("c" . "a") ("d" . "a")) part of an element of list-1 extracted by the :key function; the second argument is typically part
lst1 ! (("a" . "b") ("c" . "d") ("e" . "f")) of an element of list-2 extracted by the :key function.
lst2 ! (("c" . "a") ("d" . "a"))
The argument to the :key function is an element of either list-1 or list-2 ; the return value is
Side Eects: part of the element of the supplied list element. If :key is not supplied or nil, the list-1 or list-2
nset-exclusive-or is permitted to modify any part, car or cdr , of the list structure of list-1 or element itself is supplied to the :test or :test-not function.
list-2 .
Examples:
Exceptional Situations: !
Should be prepared to signal an error of type type-error if list-1 and list-2 are not proper lists . (setq cosmos '(1 "a" (1 2)))
(subsetp '(1) cosmos) ! true
(1 "a" (1 2))
nunion list-1 list-2 &key key test test-not ! result-list (union '((x 5) (y 6)) '((z 2) (x 4)) :key #'car)
!
Arguments and Values:
((X 5) (Y 6) (Z 2))
! ((X 4) (Y 6) (Z 2))
or
test |a designator for a function of two arguments that returns a generalized boolean . ! (2 3 (2 3) "B" "C")
(nunion lst1 lst2)
test-not |a designator for a function of two arguments that returns a generalized boolean . ! (1 (1 2) "a" "b" 2 3 (2 3) "B" "C")
!
or
(1 2 (1 2) "a" "b" "C" "B" (2 3) 3)
key |a designator for a function of one argument, or nil.
result-list |a list .
Side Eects:
nunion is permitted to modify any part, car or cdr , of the list structure of list-1 or list-2 .
Description: Exceptional Situations:
union and nunion return a list that contains every element that occurs in either list-1 or list-2 . Should be prepared to signal an error of type type-error if list-1 and list-2 are not proper lists .
For all possible ordered pairs consisting of one element from list-1 and one element from list-2 ,
:test or :test-not is used to determine whether they satisfy the test . The rst argument to the
See Also:
:test or :test-not function is the part of the element of list-1 extracted by the :key function (if
intersection, Section 3.2.1 (Compiler Terminology), Section 3.6 (Traversal Rules and Side Eects)
supplied); the second argument is the part of the element of list-2 extracted by the :key function
(if supplied). Notes:
The argument to the :key function is an element of list-1 or list-2 ; the return value is part of the The :test-not parameter is deprecated.
supplied element. If :key is not supplied or nil, the element of list-1 or list-2 itself is supplied to Since the nunion side eect is not required, it should not be used in for-eect-only positions in
the :test or :test-not function. portable code.
For every matching pair, one of the two elements of the pair will be in the result. Any element
from either list-1 or list-2 that matches no element of the other will appear in the result.
If there is a duplication between list-1 and list-2 , only one of the duplicate instances will be in
the result. If either list-1 or list-2 has duplicate entries within it, the redundant entries might or
might not appear in the result.
The order of elements in the result do not have to re
ect the ordering of list-1 or list-2 in any
way. The result list may be eq to either list-1 or list-2 if appropriate.