forked from filonenko-mikhail/cltl2-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.tex
2612 lines (2248 loc) · 112 KB
/
list.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%Part{List, Root = "CLM.MSS"}
%%% Chapter of Common Lisp Manual. Copyright 1984, 1988, 1989 Guy L. Steele Jr.
\clearpage\def\pagestatus{FINAL PROOF}
\ifx \rulang\Undef
\chapter{Lists}
A \emph{cons}, or dotted pair, is a compound data object having two components
called the \emph{car} and \emph{cdr}. Each component may be any Lisp object.
A \emph{list} is a chain of conses
linked by \emph{cdr} fields; the chain is terminated by some atom
(a non-cons object).
An ordinary list is terminated by {\nil}, the empty list
(also written {\emptylist}).
A list whose \emph{cdr} chain is terminated by some non-{\nil} atom is called
a \emph{dotted list}.
The recommended predicate for testing for the end of a list is \cdf{endp}.
\section{Conses}
These are the basic operations on conses viewed as pairs rather than
as the constituents of a list.
\begin{defun}[Function]
car list
This returns the \emph{car} of \emph{list}, which must be a cons or {\emptylist};
that is, \emph{list} must satisfy the predicate \cdf{listp}.
By definition, the \emph{car} of {\emptylist} is {\emptylist}.
If the cons is regarded as the first cons of a list, then \cdf{car}
returns the first element of the list.
For example:
\begin{lisp}
(car '(a b c)) \EV\ a
\end{lisp}
See \cdf{first}.
The \emph{car} of a cons may be altered by using \cdf{rplaca} or \cdf{setf}.
\end{defun}
\begin{defun}[Function]
cdr list
This returns the \emph{cdr} of \emph{list}, which must be a cons or {\emptylist};
that is, \emph{list} must satisfy the predicate \cdf{listp}.
By definition, the \emph{cdr} of {\emptylist} is {\emptylist}.
If the cons is regarded as the first cons of a list, then \cdf{cdr}
returns the rest of the list, which is a list with all elements
but the first of the original list.
For example:
\begin{lisp}
(cdr '(a b c)) \EV\ (b c)
\end{lisp}
See \cdf{rest}.
The \emph{cdr} of a cons may be altered by using \cdf{rplacd} or \cdf{setf}.
\end{defun}
\begin{defun}[Function]
caar list \\
cadr list \\
cdar list \\
cddr list \\
caaar list \\
caadr list \\
cadar list \\
caddr list \\
cdaar list \\
cdadr list \\
cddar list \\
cdddr list \\
caaaar list \\
caaadr list \\
caadar list \\
caaddr list \\
cadaar list \\
cadadr list \\
caddar list \\
cadddr list \\
cdaaar list \\
cdaadr list \\
cdadar list \\
cdaddr list \\
cddaar list \\
cddadr list \\
cdddar list \\
cddddr list
All of the compositions of up to four \cdf{car} and \cdf{cdr} operations
are defined as separate Common Lisp functions.
The names of these functions begin with \cdf{c} and end with \cdf{r},
and in between is a sequence of \cdf{a} and \cdf{d} letters
corresponding to
the composition performed by the function.
For example:
\begin{lisp}
(cddadr x) \textrm{is the same as} (cdr (cdr (car (cdr x))))
\end{lisp}
If the argument is regarded as a list, then \cdf{cadr} returns
the second element of the list, \cdf{caddr} the third, and \cdf{cadddr}
the fourth. If the first element of a list is a list, then
\cdf{caar} is the first element of the sublist, \cdf{cdar} is the
rest of that sublist, and \cdf{cadar} is the second element of the sublist,
and so on.
As a matter of style, it is often preferable to define a function or
macro to access part of a complicated data structure, rather than to use
a long \cdf{car}/\cdf{cdr} string. For example, one might define
a macro to extract the list of parameter variables from a lambda-expression:
\begin{lisp}
(defmacro lambda-vars (lambda-exp) `(cadr ,lambda-exp))
\end{lisp}
and then use \cdf{lambda-vars} for this purpose instead of \cdf{cadr}.
See also \cdf{defstruct}, which will automatically define
new record data types and access functions for instances of them.
Any of these functions may be used to specify a \emph{place} for \cdf{setf}.
\end{defun}
\begin{defun}[Function]
cons x y
\cdf{cons} is the primitive function to create a new \emph{cons} whose
\emph{car} is \emph{x} and whose \emph{cdr} is \emph{y}.
For example:
\begin{lisp}
(cons 'a 'b) \EV\ (a . b) \\
(cons 'a (cons 'b (cons 'c '{\emptylist}))) \EV\ (a b c) \\
(cons 'a '(b c d)) \EV\ (a b c d)
\end{lisp}
\cdf{cons} may be thought of as creating a \emph{cons}, or as adding a new element
to the front of a list.
\end{defun}
\begin{defun}[Function]
tree-equal x y &key :test :test-not
This is a predicate that is true if \emph{x} and \emph{y} are
isomorphic trees with identical leaves, that is, if \emph{x} and \emph{y}
are atoms that satisfy the test (by default \cdf{eql}),
or if they are both conses and their \emph{car}'s are \cdf{tree-equal}
and their \emph{cdr}'s are \cdf{tree-equal}.
Thus \cdf{tree-equal} recursively compares conses (but not any other objects
that have components). See \cdf{equal}, which does recursively
compare certain other structured objects, such as strings.
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\end{defun}
\section{Lists}
The following functions perform various operations on lists.
The list is one of the original Lisp data types. The very name ``Lisp''
is an abbreviation for ``LISt Processing.''
\goodbreak
\begin{defun}[Function]
endp object
The predicate \cdf{endp} is the recommended way to test for the end
of a list. It is false of conses, true of {\nil}, and an error for
all other arguments.
\beforenoterule
\begin{implementation}
Implementations are encouraged to signal an
error, especially in the interpreter, for a non-list argument.
The \cdf{endp} function is defined so as to allow compiled code
to perform simply an atom check or a null check if speed is more
important than safety.
\end{implementation}
\afternoterule
\end{defun}
\begin{defun}[Function]
list-length list
\cdf{list-length} returns, as an integer, the length of \emph{list}.
\cdf{list-length} differs from \cdf{length} when the \emph{list} is
circular; \cdf{length} may fail to return, whereas \cdf{list-length}
will return {\nil}.
For example:
\begin{lisp}
(list-length '{\emptylist}) \EV\ 0 \\
(list-length '(a b c d)) \EV\ 4 \\
(list-length '(a (b c) d)) \EV\ 3 \\
(let ((x (list 'a b c))) \\
~~(rplacd (last x) x) \\
~~(list-length x)) \EV\ {\nil}
\end{lisp}
\cdf{list-length} could be implemented as follows:
\begin{lisp}
(defun list-length (x) \\
~~(do ((n 0 (+ n 2))~~~~~~~~~~~~;\textrm{Counter} \\
~~~~~~~(fast x (cddr fast))~~~~~;\textrm{Fast pointer: leaps by 2} \\
~~~~~~~(slow x (cdr slow)))~~~~~;\textrm{Slow pointer: leaps by 1} \\
~~~~~~(nil) \\
~~~~;; If fast pointer hits the end, return the count. \\
~~~~(when (endp fast) (return n)) \\
~~~~(when (endp (cdr fast)) (return (+ n 1))) \\
~~~~;; If fast pointer eventually equals slow pointer, \\
~~~~;; 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 \\
~~~~;; fast pointer will equal the slow pointer. \\
~~~~;; That fact justifies this implementation.) \\
~~~~(when (and (eq fast slow) (> n 0)) (return nil))))
\end{lisp}
See \cdf{length}, which will return the length of any sequence.
\end{defun}
\begin{defun}[Function]
nth n list
\cd{(nth \emph{n} \emph{list})} returns the \emph{n}th element of \emph{list}, where
the \emph{car} of the list is the ``zeroth'' element.
The argument \emph{n} must be a non-negative integer.
If the length of the list is not greater than \emph{n}, then the result
is {\emptylist}, that is, {\false}.
(This is consistent with the idea that the \emph{car} and \emph{cdr}
of {\emptylist} are each {\emptylist}.)
For example:
\begin{lisp}
(nth 0 '(foo bar gack)) \EV\ foo \\
(nth 1 '(foo bar gack)) \EV\ bar \\
(nth 3 '(foo bar gack)) \EV\ {\emptylist}
\end{lisp}
\cdf{nth} may be used to specify a \emph{place} to \cdf{setf};
when \cdf{nth} is used in this way, the argument \emph{n} must be less
than the length of the \emph{list}.
Note that the arguments to \cdf{nth} are reversed from the order
used by most other sequence selector functions such as \cdf{elt}.
\end{defun}
\begin{defun}[Function]
first list \\
second list \\
third list \\
fourth list \\
fifth list \\
sixth list \\
seventh list \\
eighth list \\
ninth list \\
tenth list
These functions are sometimes convenient for accessing particular
elements of a list. \cdf{first} is the same as \cdf{car},
\cdf{second} is the same as \cdf{cadr}, \cdf{third} is the
same as \cdf{caddr}, and so on.
Note that the ordinal numbering used here is one-origin,
as opposed to the zero-origin numbering used by \cdf{nth}:
\begin{lisp}
(fifth x) \EQ\ (nth 4 x)
\end{lisp}
\cdf{setf} may be used with each of these functions to store
into the indicated position of a list.
\end{defun}
\begin{defun}[Function]
rest list
\cdf{rest} means the same as \cdf{cdr} but mnemonically complements \cdf{first}.
\cdf{setf} may be used with \cdf{rest} to replace the \emph{cdr} of a list
with a new value.
\end{defun}
\begin{defun}[Function]
nthcdr n list
\cd{(nthcdr \emph{n} \emph{list})} performs the \cdf{cdr} operation \emph{n} times
on \emph{list}, and returns the result.
For example:
\begin{lisp}
(nthcdr 0 '(a b c)) \EV\ (a b c) \\
(nthcdr 2 '(a b c)) \EV\ (c) \\
(nthcdr 4 '(a b c)) \EV\ {\emptylist}
\end{lisp}
In other words, it returns the \emph{n}th \emph{cdr} of the list.
\begin{lisp}
(car (nthcdr n x)) \EQ\ (nth n x)
\end{lisp}
The argument \emph{n} must be a non-negative integer.
\end{defun}
\begin{defun}[Function]
last list &optional (n 1)
\cdf{last} returns the tail of the \emph{list}
consisting of the last \emph{n} conses of \emph{list}. The \emph{list} may
be a dotted list. It is an error if the \emph{list} is circular.
The argument \emph{n} must be a non-negative integer.
If \emph{n} is zero, then the atom that terminates the \emph{list}
is returned. If \emph{n} is not less than the number of cons cells
making up the \emph{list}, then the \emph{list} itself is returned.
For example:
\begin{lisp}
(setq x '(a b c d)) \\
(last x) \EV\ (d) \\
(rplacd (last x) '(e f)) \\
x \EV\ '(a b c d e f) \\
(last x 3) \EV\ (d e f) \\
(last '()) \EV\ () \\
(last '(a b c . d)) \EV\ (c . d) \\
(last '(a b c . d) 0) \EV\ d \\
(last '(a b c . d) 2) \EV\ (b c . d) \\
(last '(a b c . d) 1729) \EV\ (a b c . d)
\end{lisp}
\end{defun}
\begin{defun}[Function]
list &rest args
\cdf{list} constructs and returns a list of its arguments.
For example:
\begin{lisp}
(list 3 4 'a (car '(b . c)) (+ 6 -2)) \EV\ (3 4 a b 4)
\end{lisp}
\begin{lisp}
(list) \EV\ () \\
(list (list 'a 'b) (list 'c 'd 'e)) \EV\ ((a b) (c d e))
\end{lisp}
\end{defun}
\begin{defun}[Function]
list* arg &rest others
\cdf{list*} is like \cdf{list} except that the last \emph{cons}
of the constructed list is ``dotted.'' The last argument to \cdf{list*}
is used as the \emph{cdr} of the last cons constructed;
this need not be an atom. If it is not an atom,
then the effect is to add several new elements to the front of a list.
For example:
\begin{lisp}
(list* 'a 'b 'c 'd) \EV\ (a b c . d)
\end{lisp}
This is like
\begin{lisp}
(cons 'a (cons 'b (cons 'c 'd)))
\end{lisp}
Also:
\begin{lisp}
(list* 'a 'b 'c '(d e f)) \EV\ (a b c d e f) \\*
(list* x) \EQ\ x
\end{lisp}
\end{defun}
\begin{defun}[Function]
make-list size &key :initial-element
This creates and returns a list containing \emph{size} elements, each
of which is initialized to the \cd{:initial-element}
argument (which defaults to {\false}).
\emph{size} should be a non-negative integer.
For example:
\begin{lisp}
(make-list 5) \EV\ ({\false} {\false} {\false} {\false} {\false}) \\
(make-list 3 \cd{:initial-element} 'rah) \EV\ (rah rah rah)
\end{lisp}
\end{defun}
\begin{defun}[Function]
append &rest lists
The arguments to \cdf{append} are lists. The result is a list that is the
concatenation of the arguments.
The arguments are not destroyed.
For example:
\begin{lisp}
(append '(a b c) '(d e f) '{\emptylist} '(g)) \EV\ (a b c d e f g)
\end{lisp}
Note that \cdf{append} copies the top-level list structure of each of its
arguments \emph{except} the last.
The function \cdf{concatenate} can perform a similar operation, but always
copies all its arguments. See also \cdf{nconc}, which is like \cdf{append}
but destroys all arguments but the last.
The last argument actually need not be a list but may be any Lisp object,
which becomes the tail end of the constructed list.
For example, \cd{(append '(a b c) 'd)} \EV\ \cd{(a b c . d)}.
\cd{(append \emph{x} '{\emptylist})} is an idiom once frequently used to copy the
list \emph{x}, but the \cdf{copy-list} function is more appropriate to this
task.
\end{defun}
\begin{defun}[Function]
copy-list list
This returns a list that is \cdf{equal} to \emph{list}, but not \cdf{eq}.
Only the top level of list structure is copied; that is, \cdf{copy-list}
copies in the \emph{cdr} direction but not in the \emph{car} direction.
If the list is ``dotted,'' that is, \cd{(cdr (last \emph{list}))}
is a non-{\nil} atom, this will be true of the returned list also.
See also \cdf{copy-seq} and \cdf{copy-tree}.
\end{defun}
\begin{defun}[Function]
copy-alist list
\cdf{copy-alist} is for copying association lists. The top level of
list structure of \emph{list} is copied, just as for \cdf{copy-list}.
In addition, each element of \emph{list} that is a cons is replaced
in the copy by a new cons with the same \emph{car} and \emph{cdr}.
\end{defun}
\begin{defun}[Function]
copy-tree object
\cdf{copy-tree} is for copying trees of conses.
The argument \emph{object} may be any Lisp object.
If it is not a cons, it is returned; otherwise
the result is a new cons of the results of calling \cdf{copy-tree}
on the \emph{car} and \emph{cdr} of the argument. In other words,
all conses in the tree are copied recursively, stopping
only when non-conses are encountered.
Circularities and the sharing of substructure are \emph{not} preserved.
\end{defun}
\begin{defun}[Function]
revappend x y
\cd{(revappend \emph{x} \emph{y})} is exactly the same as
\cd{(append (reverse \emph{x}) \emph{y})} except that it is potentially more
efficient. Both \emph{x} and \emph{y} should be lists.
The argument \emph{x} is copied, not destroyed.
Compare this with \cdf{nreconc}, which destroys its first argument.
\end{defun}
\begin{defun}[Function]
nconc &rest lists
\cdf{nconc} takes lists as arguments. It returns a list that is the arguments
concatenated together. The arguments are changed rather than copied.
(Compare this with \cdf{append}, which copies arguments rather than
destroying them.)
For example:
\begin{lisp}
(setq x '(a b c)) \\
(setq y '(d e f)) \\
(nconc x y) \EV\ (a b c d e f) \\
x \EV\ (a b c d e f)
\end{lisp}
Note, in the example, that the value of \cdf{x} is now different,
since its last cons has been \cdf{rplacd}'d to the value of \cdf{y}.
If one were then to evaluate \cd{(nconc x y)} again,
it would yield a piece of ``circular'' list
structure, whose printed representation would be
\cd{(a b c d e f d e f d e f ...)}, repeating forever;
if the \cdf{*print-circle*} switch were non-{\nil},
it would be printed as \cd{(a b c . \#1=(d e f . \#1\#))}.
The side-effect behavior of \cdf{nconc} is specified by a recursive relationship
outlined in the following table, in which a call to \cdf{nconc} matching
the earliest possible
pattern on the left is required to have side-effect behavior
equivalent to the corresponding expression on the right.
\begin{flushleft}
\begin{tabular}{@{}ll@{}}
\cd{(nconc)}&\cd{nil~~~~~;}\textrm{No side effects} \\
\cd{(nconc nil . \emph{r})~~~~}&\cd{(nconc . \emph{r})} \\
\cd{(nconc \emph{x})}&\emph{x} \\
\cd{(nconc \emph{x} \emph{y})}&\cd{(let ((p \emph{x}) (q \emph{y}))} \\
&\cd{~~(rplacd (last p) q)} \\
&\cd{~~p)} \\
\cd{(nconc \emph{x} \emph{y} . \emph{r})}&\cd{(nconc (nconc \emph{x} \emph{y})
. \emph{r})}
\end{tabular}
\end{flushleft}
\end{defun}
\begin{defun}[Function]
nreconc x y
\cd{(nreconc \emph{x} \emph{y})} is exactly the same as
\cd{(nconc (nreverse \emph{x}) \emph{y})} except that it is potentially more
efficient. Both \emph{x} and \emph{y} should be lists.
The argument \emph{x} is destroyed.
Compare this with \cdf{revappend}.
\begin{lisp}
(setq planets '(jupiter mars earth venus mercury)) \\
(setq more-planets '(saturn uranus pluto neptune)) \\
(nreconc more-planets planets) \\
\`\EV\ (neptune pluto uranus saturn jupiter mars earth venus mercury) \\
~~\textrm{and now the value of \cdf{more-planets} is not well defined}
\end{lisp}
\cd{(nreconc \emph{x} \emph{y})} is permitted and
required to have side-effect behavior
equivalent to that of \cd{(nconc (nreverse \emph{x})~\emph{y})}.
\end{defun}
\begin{defmac}
push item place
The form \emph{place} should be the name of a generalized variable
containing a list; \emph{item} may refer to any Lisp object. The \emph{item}
is consed onto the front of the list, and the augmented list is stored
back into \emph{place} and returned.
The form \emph{place} may be any form acceptable as a
generalized variable to \cdf{setf}. If the list held in \emph{place} is
viewed as a push-down stack, then \cdf{push} pushes an element onto the top
of the stack.
For example:
\begin{lisp}
(setq x '(a (b c) d)) \\
(push 5 (cadr x)) \EV\ (5 b c) \textrm{and now} x \EV\ (a (5 b c) d)
\end{lisp}
The effect of \cd{(push \emph{item} \emph{place})}
is roughly equivalent to
\begin{lisp}
(setf \emph{place} (cons \emph{item} \emph{place}))
\end{lisp}
except that the latter would evaluate any subforms of \emph{place}
twice, while \cdf{push} takes care to evaluate them only once.
Moreover, for certain \emph{place} forms \cdf{push} may be
significantly more efficient than the \cdf{setf} version.
Note that \emph{item} is fully evaluated before any part of \emph{place}
is evaluated.
\end{defmac}
\begin{defmac}
pushnew item place &key :test :test-not :key
The form \emph{place} should be the name of a generalized variable
containing a list; \emph{item} may refer to any Lisp object. If the
\emph{item} is not already a member of the list (as determined by
comparisons using the \cd{:test} predicate, which defaults to \cdf{eql}),
then the \emph{item} is consed onto the front of the list, and
the augmented list is stored back into \emph{place} and returned; otherwise
the unaugmented list is returned. The form \emph{place} may be
any form acceptable as a generalized variable to \cdf{setf}. If the
list held in \emph{place} is viewed as a set, then \cdf{pushnew} adjoins an
element to the set; see \cdf{adjoin}.
The keyword arguments to \cdf{pushnew}
follow the conventions for the generic sequence
functions. See chapter~\ref{KSEQUE}.
In effect, these keywords are simply passed on to the \cdf{adjoin} function.
\cdf{pushnew} returns the new contents of the \emph{place}.
For example:
\begin{lisp}
(setq x '(a (b c) d)) \\
(pushnew 5 (cadr x)) \EV\ (5 b c) \textrm{and now} x \EV\ (a (5 b c) d) \\
(pushnew 'b (cadr x)) \EV\ (5 b c) \textrm{and \cdf{x} is unchanged}
\end{lisp}
The effect of
\begin{lisp}
(pushnew \emph{item} \emph{place} \cd{:test} \emph{p})
\end{lisp}
is roughly equivalent to
\begin{lisp}
(setf \emph{place} (adjoin \emph{item} \emph{place} \cd{:test} \emph{p}))
\end{lisp}
except that the latter would evaluate any subforms of
\emph{place} twice, while \cdf{pushnew} takes care to evaluate them only once.
Moreover, for certain \emph{place} forms \cdf{pushnew} may be
significantly more efficient than the \cdf{setf} version.
Note that \emph{item} is fully evaluated before any part of \emph{place}
is evaluated.
\end{defmac}
\begin{defmac}
pop place
The form \emph{place} should be the name of a generalized variable
containing a list. The result of \cdf{pop} is the \cdf{car} of the contents
of \emph{place}, and as a side effect the \cdf{cdr} of the contents is stored
back into \emph{place}. The form \emph{place} may be any form acceptable as a
generalized variable to \cdf{setf}. If the list held in \emph{place} is
viewed as a push-down stack, then \cdf{pop} pops an element from the top of
the stack and returns it.
For example:
\begin{lisp}
(setq stack '(a b c)) \\
(pop stack) \EV\ a \textrm{and now} stack \EV\ (b c)
\end{lisp}
The effect of \cd{(pop \emph{place})} is roughly equivalent to
\begin{lisp}
(prog1 (car \emph{place}) (setf \emph{place} (cdr \emph{place})))
\end{lisp}
except that the latter would evaluate any subforms of \emph{place}
three times, while \cdf{pop} takes care to evaluate them only once.
Moreover, for certain \emph{place} forms \cdf{pop} may be
significantly more efficient than the \cdf{setf} version.
\end{defmac}
\begin{defun}[Function]
butlast list &optional n
This creates and returns a list with the same elements as \emph{list},
excepting the last \emph{n} elements.
\emph{n} defaults to 1. The argument is not destroyed.
If the \emph{list} has fewer than \emph{n} elements, then {\emptylist} is returned.
For example:
\begin{lisp}
(butlast '(a b c d)) \EV\ (a b c) \\
(butlast '((a b) (c d))) \EV\ ((a b)) \\
(butlast '(a)) \EV\ {\emptylist} \\
(butlast nil) \EV\ {\emptylist}
\end{lisp}
The name is from the phrase ``all elements but the last.''
\end{defun}
\begin{defun}[Function]
nbutlast list &optional n
This is the destructive version of \cdf{butlast}; it changes the \emph{cdr} of
the cons \emph{n}+1 from the end of the \emph{list} to {\nil}. \emph{n} defaults to 1.
If the \emph{list} has fewer than \emph{n} elements, then \cdf{nbutlast}
returns {\emptylist}, and the argument is not modified. (Therefore
one normally writes \cd{(setq a (nbutlast a))} rather than simply
\cd{(nbutlast a)}.)
For example:
\begin{lisp}
(setq foo '(a b c d)) \\
(nbutlast foo) \EV\ (a b c) \\
foo \EV\ (a b c) \\
(nbutlast '(a)) \EV\ {\emptylist} \\
(nbutlast '{\nil}) \EV\ {\emptylist}
\end{lisp}
\end{defun}
\begin{defun}[Function]
ldiff list sublist
\emph{list} should be a list, and \emph{sublist} should be a sublist
of \emph{list}, that is, one of the conses that make up \emph{list}.
\cdf{ldiff} (meaning ``list difference'') will return a new (freshly consed)
list, whose elements are those elements of \emph{list} that appear before
\emph{sublist}. If \emph{sublist} is not a tail of \emph{list}
(and in particular if \emph{sublist} is {\nil}),
then a copy of the entire \emph{list} is returned.
The argument \emph{list} is not destroyed.
For example:
\begin{lisp}
(setq x '(a b c d e)) \\
(setq y (cdddr x)) \EV\ (d e) \\
(ldiff x y) \EV\ (a b c) \\[4pt]
\textrm{but} (ldiff '(a b c d) '(c d)) \EV\ (a b c d)
\end{lisp}
since the sublist was not \cdf{eq} to any part of the list.
\end{defun}
\section{Alteration of List Structure}
The functions \cdf{rplaca} and \cdf{rplacd}
may be used to make alterations in already existing
list structure, that is, to change the \emph{car} or \emph{cdr} of an
existing cons.
One may also use \cdf{setf} in conjunction with \cdf{car} and \cdf{cdr}.
The structure is not copied but is destructively altered;
hence caution should be exercised when using these functions, as
strange side effects can occur if portions of list structure become
shared.
The \cdf{nconc}, \cdf{nreverse}, \cdf{nreconc},
and \cdf{nbutlast} functions, already
described,
have the same property, as do certain of the generic sequence
functions such as \cdf{delete}.
However, they are normally not
used for this side effect; rather, the list-structure modification
is purely for efficiency, and compatible non-modifying functions
are provided.
\begin{defun}[Function]
rplaca x y
\cd{(rplaca \emph{x} \emph{y})} changes the \emph{car} of \emph{x} to \emph{y} and returns
(the modified) \emph{x}. \emph{x} must be a cons, but \emph{y} may be any
Lisp object.
For example:
\begin{lisp}
(setq g '(a b c)) \\
(rplaca (cdr g) 'd) \EV\ (d c) \\
\textrm{Now} g \EV\ (a d c)
\end{lisp}
\end{defun}
\begin{defun}[Function]
rplacd x y
\cd{(rplacd \emph{x} \emph{y})} changes the \emph{cdr} of \emph{x} to \emph{y} and returns
(the modified) \emph{x}. \emph{x} must be a cons, but \emph{y} may be
any Lisp object.
For example:
\begin{lisp}
(setq x '(a b c)) \\
(rplacd x 'd) \EV\ (a . d) \\
\textrm{Now} x \EV\ (a . d)
\end{lisp}
\end{defun}
The functions \cdf{rplaca} and \cdf{rplacd} go back to the earliest
origins of Lisp, along with \cdf{car}, \cdf{cdr}, and \cdf{cons}.
Nowadays, however, they seem to be falling by the wayside.
More and more Common Lisp programmers use \cdf{setf} for nearly
all structure modifications: \cd{(rplaca x~y)} is rendered
as \cd{(setf (car x)~y)} or perhaps as \cd{(setf (first x)~y)}.
Even more likely is that a \cdf{defstruct} structure or a CLOS class
is used in place of a list, if the data structure is at all complicated;
in this case \cdf{setf} is used with a slot accessor.
\section{Substitution of Expressions}
\indexterm{substitution}
A number of functions are provided for performing substitutions
within a tree. All take a tree and a description
of old subexpressions to be replaced by new ones.
They come in non-destructive and destructive varieties
and specify substitution either by two arguments or by an association list.
The naming conventions for these functions and for their keyword
arguments generally follow the conventions for the generic sequence
functions. See chapter~\ref{KSEQUE}.
\begin{defun}[Function]
subst new old tree &key :test :test-not :key \\
subst-if new test tree &key :key \\
subst-if-not new test tree &key :key
\cd{(subst \emph{new} \emph{old} \emph{tree})} makes a copy of \emph{tree},
substituting \emph{new} for every subtree or leaf of \emph{tree}
(whether the subtree or leaf is a \emph{car} or a \emph{cdr} of its parent)
such that \emph{old} and the subtree or leaf satisfy the test. It
returns the modified copy of \emph{tree}. The original \emph{tree} is
unchanged, but the result tree may share with parts of the argument
\emph{tree}.
For example:
\begin{lisp}
(subst 'tempest 'hurricane \\
~~~~~~~'(shakespeare wrote (the hurricane))) \\
~~~\EV\ (shakespeare wrote (the tempest)) \\
\\
(subst 'foo '{\nil} '(shakespeare wrote (twelfth night))) \\
~~~\EV\ (shakespeare wrote (twelfth night . foo) . foo) \\
\\
(subst '(a . cons) '(old . pair) \\
~~~~~~~'((old . spice) ((old . shoes) old . pair) (old . pair)) \\
~~~~~~~\cd{:test} \#'equal) \\
~~~\EV\ ((old . spice) ((old . shoes) a . cons) (a . cons))
\end{lisp}
This function is not destructive; that is, it does not change
the \emph{car} or \emph{cdr} of any already existing list structure.
One possible definition of \cdf{subst}:
\begin{lisp}
(defun subst (old new tree \cd{\&rest} x \cd{\&key} test test-not key) \\*
~~(cond ((satisfies-the-test old tree :test test \\*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~:test-not test-not :key key) \\*
~~~~~~~~~new) \\*
~~~~~~~~((atom tree) tree) \\
~~~~~~~~(t (let ((a (apply \#'subst old new (car tree) x)) \\*
~~~~~~~~~~~~~~~~~(d (apply \#'subst old new (cdr tree) x))) \\
~~~~~~~~~~~~~(if (and (eql a (car tree)) \\*
~~~~~~~~~~~~~~~~~~~~~~(eql d (cdr tree))) \\*
~~~~~~~~~~~~~~~~~tree \\*
~~~~~~~~~~~~~~~~~(cons a d))))))
\end{lisp}
See also \cdf{substitute}, which substitutes for top-level elements
of a sequence.
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\end{defun}
\begin{defun}[Function]
nsubst new old tree &key :test :test-not :key \\
nsubst-if new test tree &key :key \\
nsubst-if-not new test tree &key :key
\cdf{nsubst} is a destructive version of \cdf{subst}. The list structure of
\emph{tree} is altered by destructively replacing with \emph{new}
each leaf or subtree of the \emph{tree} such that \emph{old} and the leaf
or subtree satisfy the test.
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\end{defun}
\begin{defun}[Function]
sublis alist tree &key :test :test-not :key
\cdf{sublis} makes substitutions for objects in a tree
(a structure of conses).
The first argument to \cdf{sublis} is an association list.
The second argument is the tree in which
substitutions are to be made, as for \cdf{subst}.
\cdf{sublis} looks at all subtrees and leaves of the tree;
if a subtree or leaf appears as a key in the association
list (that is, the key and the subtree or leaf satisfy the test),
it is replaced by the object with which it is associated.
This operation is non-destructive. In effect, \cdf{sublis} can
perform several \cdf{subst} operations simultaneously.
For example:
\begin{lisp}
(sublis '((x . 100) (z . zprime)) \\*
~~~~~~~~'(plus x (minus g z x p) 4 . x)) \\*
~~~\EV\ (plus 100 (minus g zprime 100 p) 4 . 100) \\*
\\*
(sublis '(((+ x y) . (- x y)) ((- x y) . (+ x y))) \\*
~~~~~~~~'(* (/ (+ x y) (+ x p)) (- x y)) \\*
~~~~~~~~:test \#'equal) \\*
~~~\EV\ (* (/ (- x y) (+ x p)) (+ x y))
\end{lisp}
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\end{defun}
\begin{defun}[Function]
nsublis alist tree &key :test :test-not :key
\cdf{nsublis} is like \cdf{sublis} but destructively modifies the relevant
parts of the \emph{tree}.
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\end{defun}
\section{Using Lists as Sets}
Common Lisp includes functions that allow a list of items to be
treated as a \emph{set}.
There are functions to add, remove, and search for items in a list,
based on various criteria.
There are also set union, intersection, and difference functions.
The naming conventions for these functions and for their keyword
arguments generally follow the conventions that apply to the generic sequence
functions. See chapter~\ref{KSEQUE}.
\begin{defun}[Function]
member item list &key :test :test-not :key \\
member-if predicate list &key :key \\
member-if-not predicate list &key :key
The \emph{list} is searched for an element that satisfies the test.
If none is found, {\false} is returned;
otherwise, the tail of \emph{list} beginning
with the first element that satisfied the test is returned.
The \emph{list} is searched on the top level only.
These functions are suitable for use as predicates.
For example:
\begin{lisp}
(member 'snerd '(a b c d)) \EV\ {\false} \\
(member-if \#'numberp '(a \#{\Xbackslash}Space 5/3 foo)) \EV\ (5/3 foo) \\
(member 'a '(g (a y) c a d e a f)) \EV\ (a d e a f)
\end{lisp}
Note, in the last example,
that the value returned by \cdf{member} is \cdf{eq} to the portion of the list
beginning with \cdf{a}.
Thus \cdf{rplaca} on the result of \cdf{member} may be used
to alter the found list element,
if a check is first made that \cdf{member} did not return {\false}.
See also \cdf{find} and \cdf{position}.
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\end{defun}
\begin{defun}[Function]
tailp sublist list
\cdf{tailp} is true if and only if there exists an integer \emph{n} such that
\begin{lisp}
(eql \emph{sublist} (nthcdr \emph{n} \emph{list}))
\end{lisp}
\emph{list} may be a dotted list (implying that
implementations must use \cdf{atom} and not \cdf{endp} to check for
the end of the \emph{list}).
\end{defun}
\begin{defun}[Function]
adjoin item list &key :test :test-not :key
\cdf{adjoin} is used to add an element to a set, provided that
it is not already a member. The equality test defaults to \cdf{eql}.
\begin{lisp}
(adjoin \emph{item} \emph{list}) \EQ\ (if (member \emph{item} \emph{list}) \emph{list} (cons \emph{item} \emph{list}))
\end{lisp}
In general, the test may be any predicate; the \emph{item} is added to the
list only if there is no element of the list that ``satisfies the test.''
\cdf{adjoin} deviates from the usual rules described in chapter~\ref{KSEQUE}
for the treatment of arguments named \emph{item} and \cd{:key}.
If a \cd{:key} function is specified, it is applied to \emph{item}
as well as to each element of the list. The rationale is that
if the \emph{item} is not yet in the list, it soon will be, and so
the test is more properly viewed as being between two elements
rather than between a separate \emph{item} and an element.
\begin{lisp}
(adjoin \emph{item} \emph{list} :key \emph{fn}) \\
~~\EQ\ (if (member (funcall \emph{fn} \emph{item}) \emph{list}
:key \emph{fn}) \emph{list} (cons \emph{item} \emph{list}))
\end{lisp}
See \cdf{pushnew}.
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\end{defun}
\begin{defun}[Function]
union list1 list2 &key :test :test-not :key \\
nunion list1 list2 &key :test :test-not :key
\cdf{union} takes two lists and returns a new list containing
everything that is an element of either of the \emph{lists}.
If there is a duplication between two lists,
only one of the duplicate instances will be in the result.
If either of the arguments has duplicate entries within it,
the redundant entries
may or may not appear in the result.
For example:
\begin{lisp}
(union '(a b c) '(f a d)) \\
~~~\EV\ (a b c f d) \textrm{or} (b c f a d) \textrm{or} (d f a b c) \textrm{or} ... \\
\\
(union '((x 5) (y 6)) '((z 2) (x 4)) :key \#'car) \\
~~~\EV\ ((x 5) (y 6) (z 2)) \textrm{or} ((x 4) (y 6) (z 2)) \textrm{or} ...
\end{lisp}
There is no guarantee that the order of elements in the result will
reflect the ordering of the arguments in any particular way.
The implementation is therefore free to use any of a variety of strategies.
The result list may share cells with, or be \cdf{eq} to, either of the arguments
if appropriate.
In general, the test may be any predicate, and the union operation may be
described as follows. For all possible ordered pairs consisting of one
element from \emph{list1} and one element from \emph{list2}, the test is used
to determine whether they ``match.'' For every matching pair, at least
one of the two elements of the pair will be in the result. Moreover, any
element from either list that matches no element of the other will appear
in the result. All this is very general, but probably not particularly
useful unless the test is an equivalence relation.
The \cd{:test-not} argument can be useful when the test function
is the logical negation of an equivalence test. A good example
of this is the function \cdf{mismatch}, which is logically inverted
so that possibly useful information can be returned if the arguments do not
match. This additional ``useful information'' is discarded in the following
example; \cdf{mismatch} is used purely as a predicate.
\begin{lisp}
(union '(\#(a b) \#(5 0 6) \#(f 3)) \\
~~~~~~~'(\#(5 0 6) (a b) \#(g h)) \\
~~~~~~~:test-not \\
~~~~~~~\#'mismatch) \\
~~~\EV\ (\#(a b) \#(5 0 6) \#(f 3) \#(g h))~~~~~;\textrm{One possible result} \\
~~~\EV\ ((a b) \#(f 3) \#(5 0 6) \#(g h))~~~~~~;\textrm{Another possible result}
\end{lisp}
Using \cd{\cd{:test-not} \#'mismatch} differs from using
\cd{\cd{:test} \#'equalp}, for example, because \cdf{mismatch}
will determine that \cd{\#(a b)} and \cd{(a b)} are the same,
while \cdf{equalp} would regard them as not the same.
\cdf{nunion} is the destructive version of \cdf{union}.
It performs the same operation but may destroy the argument lists,
perhaps in order to use their cells to construct the result.