-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap-14.texi
4088 lines (2901 loc) · 116 KB
/
chap-14.texi
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
@node Conses
@chapter Conses
@menu
* Cons Concepts::
* Conses Dictionary::
@end menu
@node Cons Concepts
@section Cons Concepts
A @newterm{cons} is a compound data @term{object}
having two components called the @term{car} and the @term{cdr}.
@float Figure,fig14.1
@cartouche
@multitable{car}{rplaca}{rplacd}
@item car @tab cons @tab rplacd
@item cdr @tab rplaca @tab
@end multitable
@end cartouche
@caption{Some defined names relating to conses.}
@end float
Depending on context, a group of connected @term{conses} can be viewed
in a variety of different ways. A variety of operations is provided to
support each of these various views.
@menu
* Conses as Trees::
* Conses as Lists::
@end menu
@node Conses as Trees
@subsection Conses as Trees
A @newterm{tree} is a binary recursive data structure made up of
@term{conses} and @term{atoms}:
the @term{conses} are themselves also @term{trees}
(sometimes called ``subtrees'' or ``branches''), and the @term{atoms}
are terminal nodes (sometimes called @newterm{leaves}).
Typically, the @term{leaves} represent data while the branches
establish some relationship among that data.
@float Figure,fig14.2
@cartouche
@multitable{caaaar}{caddar}{copy-tree}{nsubst-if-not}
@item caaaar @tab caddar @tab cdar @tab nsubst
@item caaadr @tab cadddr @tab cddaar @tab nsubst-if
@item caaar @tab caddr @tab cddadr @tab nsubst-if-not
@item caadar @tab cadr @tab cddar @tab nthcdr
@item caaddr @tab cdaaar @tab cdddar @tab sublis
@item caadr @tab cdaadr @tab cddddr @tab subst
@item caar @tab cdaar @tab cdddr @tab subst-if
@item cadaar @tab cdadar @tab cddr @tab subst-if-not
@item cadadr @tab cdaddr @tab copy-tree @tab tree-equal
@item cadar @tab cdadr @tab nsublis @tab
@end multitable
@end cartouche
@caption{Some defined names relating to trees.}
@end float
@node General Restrictions on Parameters that must be Trees
@subsubsection General Restrictions on Parameters that must be Trees
Except as explicitly stated otherwise,
for any @term{standardized} @term{function} that takes a @term{parameter}
that is required to be a @term{tree},
the consequences are undefined
if that @term{tree} is circular.
@node Conses as Lists
@subsection Conses as Lists
A @newterm{list} is a chain of @term{conses} in which the @term{car} of each
@term{cons} is an @term{element} of the @term{list},
and the @term{cdr} of each @term{cons} is either the next
link in the chain or a terminating @term{atom}.
A @newterm{proper list} is a @term{list} terminated by the @term{empty list}.
The @term{empty list} is a @term{proper list}, but is not a @term{cons}.
An @newterm{improper list} is a @term{list} that is not a @term{proper list};
that is, it is a @term{circular list} or a @term{dotted list}.
A @newterm{dotted list} is a @term{list} that has a terminating @term{atom}
that is not the @term{empty list}. A @term{non-nil} @term{atom} by itself
is not considered to be a @term{list} of any kind---not even a @term{dotted list}.
A @newterm{circular list} is a chain of @term{conses} that has no termination
because some @term{cons} in the chain is the @term{cdr} of a later @term{cons}.
@float Figure,fig14.3
@cartouche
@multitable{copy-alist}{member-if-not}{nbutlast}{revappend}
@item append @tab last @tab nbutlast @tab rest
@item butlast @tab ldiff @tab nconc @tab revappend
@item copy-alist @tab list @tab ninth @tab second
@item copy-list @tab list* @tab nreconc @tab seventh
@item eighth @tab list-length @tab nth @tab sixth
@item endp @tab make-list @tab nthcdr @tab tailp
@item fifth @tab member @tab pop @tab tenth
@item first @tab member-if @tab push @tab third
@item fourth @tab member-if-not @tab pushnew @tab
@end multitable
@end cartouche
@caption{Some defined names relating to lists.}
@end float
@node Lists as Association Lists
@subsubsection Lists as Association Lists
An @newterm{association list} is a @term{list} of @term{conses}
representing an association of @term{keys} with @term{values},
where the @term{car} of each @term{cons} is the @term{key}
and the @term{cdr} is the @term{value} associated with that @term{key}.
@float Figure,fig14.4
@cartouche
@multitable{acons}{assoc-if-not}{pairlis}{rassoc-if-not}
@item acons @tab assoc-if @tab pairlis @tab rassoc-if
@item assoc @tab assoc-if-not @tab rassoc @tab rassoc-if-not
@end multitable
@end cartouche
@caption{Some defined names related to assocation lists.}
@end float
@node Lists as Sets
@subsubsection Lists as Sets
@term{Lists} are sometimes viewed as sets by considering their elements
unordered and by assuming there is no duplication of elements.
@float Figure,fig14.5
@cartouche
@multitable{nintersection}{nset-exclusive-or}{set-exclusive-or}{union}
@item adjoin @tab nset-difference @tab set-difference @tab union
@item intersection @tab nset-exclusive-or @tab set-exclusive-or @tab
@item nintersection @tab nunion @tab subsetp @tab
@end multitable
@end cartouche
@caption{Some defined names related to sets.}
@end float
@node General Restrictions on Parameters that must be Lists
@subsubsection General Restrictions on Parameters that must be Lists
Except as explicitly specified otherwise,
any @term{standardized} @term{function} that takes a @term{parameter}
that is required to be a @term{list} should be prepared to signal
an error @oftype{type-error} if the @term{value} received is a @term{dotted list}.
Except as explicitly specified otherwise,
for any @term{standardized} @term{function} that takes a @term{parameter}
that is required to be a @term{list},
the consequences are undefined
if that @term{list} is @term{circular}.
@node Conses Dictionary
@section Conses Dictionary
@menu
* list (System Class)::
* null (System Class)::
* cons (System Class)::
* atom (Type)::
* cons (Function)::
* consp::
* atom (Function)::
* rplaca; rplacd::
* car; cdr; caar; cadr; cdar; cddr; caaar; caadr; cadar; caddr; cdaar; cd+::
* copy-tree::
* sublis; nsublis::
* subst; subst-if; subst-if-not; nsubst; nsubst-if; nsubst-if-not::
* tree-equal::
* copy-list::
* list; list*::
* list-length::
* listp::
* make-list::
* push::
* pop::
* first; second; third; fourth; fifth; sixth; seventh; eighth; ninth; ten+::
* nth::
* endp::
* null (Function)::
* nconc::
* append::
* revappend; nreconc::
* butlast; nbutlast::
* last::
* ldiff; tailp::
* nthcdr::
* rest::
* member; member-if; member-if-not::
* mapc; mapcar; mapcan; mapl; maplist; mapcon::
* acons::
* assoc; assoc-if; assoc-if-not::
* copy-alist::
* pairlis::
* rassoc; rassoc-if; rassoc-if-not::
* get-properties::
* getf::
* remf::
* intersection; nintersection::
* adjoin::
* pushnew::
* set-difference; nset-difference::
* set-exclusive-or; nset-exclusive-or::
* subsetp::
* union; nunion::
@end menu
@node list (System Class)
@syindexanchor{list, SC}
@subsection list (System Class)
@cindex list
@subsubheading Class Precedence List:
@symbolref{list, SC},
@symbolref{sequence, SYM},
@symbolref{t, SC}
@subsubheading Description:
A @newterm{list} is a chain of @term{conses} in which the @term{car} of each
@term{cons} is an @term{element} of the @term{list}, and the @term{cdr} of
each @term{cons} is either the next link in the chain or a terminating
@term{atom}.
A @newterm{proper list} is a chain of @term{conses} terminated by
the @newterm{empty list}, @empty{}, which is itself a @term{proper list}.
A @newterm{dotted list} is a @term{list} which has a terminating @term{atom}
that is not the @term{empty list}.
A @newterm{circular list} is a chain of @term{conses} that has no termination
because some @term{cons} in the chain is the @term{cdr} of a later @term{cons}.
@term{Dotted lists} and @term{circular lists} are also @term{lists}, but usually
the unqualified term ``list'' within this specification means @term{proper list}.
Nevertheless, @thetype{list} unambiguously includes @term{dotted lists}
and @term{circular lists}.
For each @term{element} of a @term{list} there is a @term{cons}.
The @term{empty list} has no @term{elements} and is not a @term{cons}.
@Thetypes{cons} and @symbolref{null, SC} form an @term{exhaustive partition}
of the @term{type} @symbolref{list, SC}.
@subsubheading See Also:
@ref{Left-Parenthesis},
@ref{Printing Lists and Conses}
@node null (System Class)
@syindexanchor{null, SC}
@subsection null (System Class)
@cindex null
@subsubheading Class Precedence List:
@symbolref{null, SC},
@symbolref{symbol, SYM},
@symbolref{list, SC},
@symbolref{sequence, SYM},
@symbolref{t, SC}
@subsubheading Description:
The only @term{object} @oftype{null} is @nil{},
which represents the @term{empty list} and can also be notated @empty{}.
@subsubheading See Also:
@ref{Symbols as Tokens},
@ref{Left-Parenthesis},
@ref{Printing Symbols}
@node cons (System Class)
@syindexanchor{cons, SC}
@subsection cons (System Class)
@cindex cons
@subsubheading Class Precedence List:
@symbolref{cons, SC},
@symbolref{list, SC},
@symbolref{sequence, SYM},
@symbolref{t, SC}
@subsubheading Description:
A @term{cons} is a compound @term{object} having two components,
called the @term{car} and @term{cdr}. These form a @term{dotted pair}.
Each component can be any @term{object}.
@subsubheading Compound Type Specifier Kind:
Specializing.
@subsubheading Compound Type Specifier Syntax:
@Deftype{cons, @ttbrac{car-typespec @brac{cdr-typespec}}}
@subsubheading Compound Type Specifier Arguments:
@param{car-typespec}---a @term{type specifier},
or the @term{symbol} @t{*}.
@Default{the @term{symbol} @t{*}}
@param{cdr-typespec}---a @term{type specifier},
or the @term{symbol} @t{*}.
@Default{the @term{symbol} @t{*}}
@subsubheading Compound Type Specifier Description:
This denotes the set of @term{conses}
whose @term{car} is constrained to be of @term{type} @param{car-typespec} and
whose @term{cdr} is constrained to be of @term{type} @param{cdr-typespec}.
(If either @param{car-typespec} or @param{cdr-typespec} is @t{*},
it is as if @thetype{t} had been denoted.)
@subsubheading See Also:
@ref{Left-Parenthesis},
@ref{Printing Lists and Conses}
@node atom (Type)
@syindexanchor{atom, T}
@subsection atom (Type)
@cindex atom
@subsubheading Supertypes:
@symbolref{atom, T},
@symbolref{t, SC}
@subsubheading Description:
It is equivalent to @tt{(not cons)}.
@node cons (Function)
@syindexanchor{cons, F}
@subsection cons (Function)
@cindex cons
@subsubheading Syntax:
@DefunWithValues{cons, object-1 object-2, cons}
@subsubheading Arguments and Values:
@param{object-1}---an @term{object}.
@param{object-2}---an @term{object}.
@param{cons}---a @term{cons}.
@subsubheading Description:
Creates a @term{fresh} @term{cons}, the @term{car} of which is @param{object-1}
and the @term{cdr} of which is @param{object-2}.
@subsubheading Examples:
@lisp
(cons 1 2) @EV{} (1 . 2)
(cons 1 nil) @EV{} (1)
(cons nil 2) @EV{} (NIL . 2)
(cons nil nil) @EV{} (NIL)
(cons 1 (cons 2 (cons 3 (cons 4 nil)))) @EV{} (1 2 3 4)
(cons 'a 'b) @EV{} (A . B)
(cons 'a (cons 'b (cons 'c '@empty{}))) @EV{} (A B C)
(cons 'a '(b c d)) @EV{} (A B C D)
@end lisp
@subsubheading See Also:
@ref{list}
@subsubheading Notes:
If @param{object-2} is a @term{list}, @symbolref{cons, SC} can be thought of as
producing a new @term{list} which is like it but has @param{object-1} prepended.
@node consp
@syindexanchor{consp, SYM}
@subsection consp (Function)
@cindex consp
@subsubheading Syntax:
@DefunWithValues{consp, object, generalized-boolean}
@subsubheading Arguments and Values:
@param{object}---an @term{object}.
@param{generalized-boolean}---a @term{generalized boolean}.
@subsubheading Description:
@TypePredicate{object, cons}
@subsubheading Examples:
@lisp
(consp nil) @EV{} @term{false}
(consp (cons 1 2)) @EV{} @term{true}
@end lisp
The @term{empty list} is not a @term{cons}, so
@lisp
(consp '()) @EQ{} (consp 'nil) @EV{} @term{false}
@end lisp
@subsubheading See Also:
@ref{listp}
@subsubheading Notes:
@lisp
(consp @param{object}) @EQ{} (typep @param{object} 'cons) @EQ{} (not (typep @param{object} 'atom)) @EQ{} (typep @param{object} '(not atom))
@end lisp
@node atom (Function)
@syindexanchor{atom, F}
@subsection atom (Function)
@cindex atom
@subsubheading Syntax:
@DefunWithValues{atom, object, generalized-boolean}
@subsubheading Arguments and Values:
@param{object}---an @term{object}.
@param{generalized-boolean}---a @term{generalized boolean}.
@subsubheading Description:
@TypePredicate{object, atom}
@subsubheading Examples:
@lisp
(atom 'sss) @EV{} @term{true}
(atom (cons 1 2)) @EV{} @term{false}
(atom nil) @EV{} @term{true}
(atom '()) @EV{} @term{true}
(atom 3) @EV{} @term{true}
@end lisp
@subsubheading Notes:
@lisp
(atom @param{object}) @EQ{} (typep @param{object} 'atom) @EQ{} (not (consp @param{object}))
@EQ{} (not (typep @param{object} 'cons)) @EQ{} (typep @param{object} '(not cons))
@end lisp
@node rplaca; rplacd
@syindexanchor{rplaca, SYM}
@syindexanchor{rplacd, SYM}
@subsection rplaca, rplacd (Function)
@cindex rplaca
@cindex rplacd
@anchor{rplaca}
@subsubheading Syntax:
@DefunWithValues{rplaca, cons object, cons}
@DefunWithValues{rplacd, cons object, cons}
@subsubheading Pronunciation:
@symbolref{rplaca, SYM}: @pronounced{@stress{r@harde{}}@Stress{plak}@schwa{}}
or @pronounced{@stress{r@schwa{}}@Stress{plak}@schwa{}}
@symbolref{rplacd, SYM}: @pronounced{@stress{r@harde{}}@Stress{plak}d@schwa{}}
or @pronounced{@stress{r@schwa{}}@Stress{plak}d@schwa{}}
or @pronounced{@stress{r@harde{}}@Stress{plak}d@harde{}}
or @pronounced{@stress{r@schwa{}}@Stress{plak}d@harde{}}
@subsubheading Arguments and Values:
@param{cons}---a @term{cons}.
@param{object}---an @term{object}.
@subsubheading Description:
@symbolref{rplaca, SYM} replaces the @term{car} of the @param{cons} with @param{object}.
@symbolref{rplacd, SYM} replaces the @term{cdr} of the @param{cons} with @param{object}.
@subsubheading Examples:
@lisp
(defparameter *some-list* (list* 'one 'two 'three 'four)) @EV{} *some-list*
*some-list* @EV{} (ONE TWO THREE . FOUR)
(rplaca *some-list* 'uno) @EV{} (UNO TWO THREE . FOUR)
*some-list* @EV{} (UNO TWO THREE . FOUR)
(rplacd (last *some-list*) (list 'IV)) @EV{} (THREE IV)
*some-list* @EV{} (UNO TWO THREE IV)
@end lisp
@subsubheading Side Effects:
The @param{cons} is modified.
@Shouldchecktype{cons, a @term{cons}}
@node car; cdr; caar; cadr; cdar; cddr; caaar; caadr; cadar; caddr; cdaar; cd+
@syindexanchor{car, SYM}
@syindexanchor{cdr, SYM}
@syindexanchor{caar, SYM}
@syindexanchor{cadr, SYM}
@syindexanchor{cdar, SYM}
@syindexanchor{cddr, SYM}
@syindexanchor{caaaar, SYM}
@syindexanchor{caaadr, SYM}
@syindexanchor{caaar, SYM}
@syindexanchor{caadar, SYM}
@syindexanchor{caaddr, SYM}
@syindexanchor{caadr, SYM}
@syindexanchor{cadaar, SYM}
@syindexanchor{cadadr, SYM}
@syindexanchor{cadar, SYM}
@syindexanchor{caddar, SYM}
@syindexanchor{cadddr, SYM}
@syindexanchor{caddr, SYM}
@syindexanchor{cdaaar, SYM}
@syindexanchor{cdaadr, SYM}
@syindexanchor{cdaar, SYM}
@syindexanchor{cdadar, SYM}
@syindexanchor{cdaddr, SYM}
@syindexanchor{cdadr, SYM}
@syindexanchor{cddaar, SYM}
@syindexanchor{cddadr, SYM}
@syindexanchor{cddar, SYM}
@syindexanchor{cdddar, SYM}
@syindexanchor{cddddr, SYM}
@syindexanchor{cdddr, SYM}
@subsection car, cdr, caar, cadr, cdar, cddr, caaar, caadr, cadar, caddr, cdaar, cdadr, cddar, cdddr, caaaar, caaadr, caadar, caaddr, cadaar, cadadr, caddar, cadddr, cdaaar, cdaadr, cdadar, cdaddr, cddaar, cddadr, cdddar, cddddr (Accessor)
@cindex car
@cindex caaaar
@cindex caaadr
@cindex caaar
@cindex caadar
@cindex caaddr
@cindex caadr
@cindex caar
@cindex cadaar
@cindex cadadr
@cindex cadar
@cindex caddar
@cindex cadddr
@cindex caddr
@cindex cadr
@cindex cdaaar
@cindex cdaadr
@cindex cdaar
@cindex cdadar
@cindex cdaddr
@cindex cdadr
@cindex cdar
@cindex cddaar
@cindex cddadr
@cindex cddar
@cindex cdddar
@cindex cdddr
@cindex cddr
@cindex cdr
@cindex cddddr
@anchor{car}
@anchor{cdr}
@subsubheading Syntax:
car x @EV{} object | (setf (x object) new-object)@*
cdr x @EV{} object | (setf (x object) new-object)@*
caar x @EV{} object | (setf (x object) new-object)@*
cadr x @EV{} object | (setf (x object) new-object)@*
cdar x @EV{} object | (setf (x object) new-object)@*
cddr x @EV{} object | (setf (x object) new-object)@*
caaar x @EV{} object | (setf (x object) new-object)@*
caadr x @EV{} object | (setf (x object) new-object)@*
cadar x @EV{} object | (setf (x object) new-object)@*
caddr x @EV{} object | (setf (x object) new-object)@*
cdaar x @EV{} object | (setf (x object) new-object)@*
cdadr x @EV{} object | (setf (x object) new-object)@*
cddar x @EV{} object | (setf (x object) new-object)@*
cdddr x @EV{} object | (setf (x object) new-object)@*
caaaar x @EV{} object | (setf (x object) new-object)@*
caaadr x @EV{} object | (setf (x object) new-object)@*
caadar x @EV{} object | (setf (x object) new-object)@*
caaddr x @EV{} object | (setf (x object) new-object)@*
cadaar x @EV{} object | (setf (x object) new-object)@*
cadadr x @EV{} object | (setf (x object) new-object)@*
caddar x @EV{} object | (setf (x object) new-object)@*
cadddr x @EV{} object | (setf (x object) new-object)@*
cdaaar x @EV{} object | (setf (x object) new-object)@*
cdaadr x @EV{} object | (setf (x object) new-object)@*
cdadar x @EV{} object | (setf (x object) new-object)@*
cdaddr x @EV{} object | (setf (x object) new-object)@*
cddaar x @EV{} object | (setf (x object) new-object)@*
cddadr x @EV{} object | (setf (x object) new-object)@*
cdddar x @EV{} object | (setf (x object) new-object)@*
cddddr x @EV{} object | (setf (x object) new-object)@*
@*
@subsubheading Pronunciation:
@symbolref{cadr, SYM}: @pronounced{@Stress{ka}@stress{d@schwa{} r}}
@symbolref{caddr, SYM}: @pronounced{@Stress{kad}@schwa{} @stress{d@schwa{} r}}
or @pronounced{@Stress{ka}@stress{d\.ud@schwa{} r}}
@symbolref{cdr, SYM}: @pronounced{@Stress{k\.u}@stress{d@schwa{} r}}
@symbolref{cddr, SYM}: @pronounced{@Stress{k\.ud}@schwa{} @stress{d@schwa{} r}}
or @pronounced{@Stress{k@schwa{}}@stress{d\.ud@schwa{} r}}
@subsubheading Arguments and Values:
@param{x}---a @term{list}.
@param{object}---an @term{object}.
@param{new-object}---an @term{object}.
@subsubheading Description:
If @param{x} is a @term{cons}, @symbolref{car, SYM} returns the @term{car}
of that @term{cons}. If @param{x} is @nil{}, @symbolref{car, SYM} returns @nil{}.
If @param{x} is a @term{cons}, @symbolref{cdr, SYM} returns the @term{cdr}
of that @term{cons}. If @param{x} is @nil{}, @symbolref{cdr, SYM} returns @nil{}.
@term{Functions} are provided which perform compositions of up to four
@symbolref{car, SYM} and @symbolref{cdr, SYM} operations. Their @term{names} consist of
a @f{C}, followed by two, three, or four occurrences of @f{A} or @f{D},
and finally an @f{R}. The series of @f{A}'s and @f{D}'s in each
@term{function}'s @term{name} is chosen to identify the series of
@symbolref{car, SYM} and @symbolref{cdr, SYM} operations that is performed by the function.
The order in which the @f{A}'s and @f{D}'s appear is the inverse of the
order in which the corresponding operations are performed. @Thenextfigure{}@spc{}
defines the relationships precisely.
@float Figure,fig14.6
@cartouche
@multitable{This @term{place} @mat{@ldots{}}}{Is equivalent to this @term{place} @mat{@ldots{}}}
@headitem This @term{place} @mat{@ldots{}} @tab Is equivalent to this @term{place} @mat{@ldots{}}
@item @f{(caar @param{x})} @tab @f{(car (car @param{x}))}
@item @f{(cadr @param{x})} @tab @f{(car (cdr @param{x}))}
@item @f{(cdar @param{x})} @tab @f{(cdr (car @param{x}))}
@item @f{(cddr @param{x})} @tab @f{(cdr (cdr @param{x}))}
@item @f{(caaar @param{x})} @tab @f{(car (car (car @param{x})))}
@item @f{(caadr @param{x})} @tab @f{(car (car (cdr @param{x})))}
@item @f{(cadar @param{x})} @tab @f{(car (cdr (car @param{x})))}
@item @f{(caddr @param{x})} @tab @f{(car (cdr (cdr @param{x})))}
@item @f{(cdaar @param{x})} @tab @f{(cdr (car (car @param{x})))}
@item @f{(cdadr @param{x})} @tab @f{(cdr (car (cdr @param{x})))}
@item @f{(cddar @param{x})} @tab @f{(cdr (cdr (car @param{x})))}
@item @f{(cdddr @param{x})} @tab @f{(cdr (cdr (cdr @param{x})))}
@item @f{(caaaar @param{x})} @tab @f{(car (car (car (car @param{x}))))}
@item @f{(caaadr @param{x})} @tab @f{(car (car (car (cdr @param{x}))))}
@item @f{(caadar @param{x})} @tab @f{(car (car (cdr (car @param{x}))))}
@item @f{(caaddr @param{x})} @tab @f{(car (car (cdr (cdr @param{x}))))}
@item @f{(cadaar @param{x})} @tab @f{(car (cdr (car (car @param{x}))))}
@item @f{(cadadr @param{x})} @tab @f{(car (cdr (car (cdr @param{x}))))}
@item @f{(caddar @param{x})} @tab @f{(car (cdr (cdr (car @param{x}))))}
@item @f{(cadddr @param{x})} @tab @f{(car (cdr (cdr (cdr @param{x}))))}
@item @f{(cdaaar @param{x})} @tab @f{(cdr (car (car (car @param{x}))))}
@item @f{(cdaadr @param{x})} @tab @f{(cdr (car (car (cdr @param{x}))))}
@item @f{(cdadar @param{x})} @tab @f{(cdr (car (cdr (car @param{x}))))}
@item @f{(cdaddr @param{x})} @tab @f{(cdr (car (cdr (cdr @param{x}))))}
@item @f{(cddaar @param{x})} @tab @f{(cdr (cdr (car (car @param{x}))))}
@item @f{(cddadr @param{x})} @tab @f{(cdr (cdr (car (cdr @param{x}))))}
@item @f{(cdddar @param{x})} @tab @f{(cdr (cdr (cdr (car @param{x}))))}
@item @f{(cddddr @param{x})} @tab @f{(cdr (cdr (cdr (cdr @param{x}))))}
@end multitable
@end cartouche
@caption{CAR and CDR variants}
@end float
@symbolref{setf, SYM} can also be used with any of these functions to change an
existing component of @param{x}, but @symbolref{setf, SYM} will not make new
components. So, for example, the @term{car} of a @term{cons}
can be assigned with @symbolref{setf, SYM} of @symbolref{car, SYM},
but the @term{car} of @nil{}@spc{}cannot be assigned with @symbolref{setf, SYM} of @symbolref{car, SYM}.
Similarly, the @term{car} of the @term{car} of a @term{cons} whose @term{car}
is a @term{cons} can be assigned with @symbolref{setf, SYM} of @symbolref{caar, SYM},
but neither @nil{} nor a @term{cons} whose car is @nil{}@spc{}can be assigned
with @symbolref{setf, SYM} of @symbolref{caar, SYM}.
The argument @param{x} is permitted to be a @term{dotted list}
or a @term{circular list}.
@subsubheading Examples:
@lisp
(car nil) @EV{} NIL
(cdr '(1 . 2)) @EV{} 2
(cdr '(1 2)) @EV{} (2)
(cadr '(1 2)) @EV{} 2
(car '(a b c)) @EV{} A
(cdr '(a b c)) @EV{} (B C)
@end lisp
@subsubheading Exceptional Situations:
The functions @symbolref{car, SYM} and @symbolref{cdr, SYM}
should signal @symbolref{type-error, SYM} if they receive an argument which is not a
@term{list}. The other functions (@symbolref{caar, SYM}, @symbolref{cadr, SYM},
@mat{@ldots{}} @symbolref{cddddr, SYM}) should behave for the purpose of
error checking as if defined by appropriate calls to @symbolref{car, SYM} and
@symbolref{cdr, SYM}.
@subsubheading See Also:
@ref{rplaca}, @ref{first}, @ref{rest}
@subsubheading Notes:
The @term{car} of a @term{cons} can also be altered by using @symbolref{rplaca, SYM},
and the @term{cdr} of a @term{cons} can be altered by using @symbolref{rplacd, SYM}.
@lisp
(car @i{x}) @EQ{} (first @i{x})
(cadr @i{x}) @EQ{} (second @i{x}) @EQ{} (car (cdr @i{x}))
(caddr @i{x}) @EQ{} (third @i{x}) @EQ{} (car (cdr (cdr @i{x})))
(cadddr @i{x}) @EQ{} (fourth @i{x}) @EQ{} (car (cdr (cdr (cdr @i{x}))))
@end lisp
@node copy-tree
@syindexanchor{copy-tree, SYM}
@subsection copy-tree (Function)
@cindex copy-tree
@subsubheading Syntax:
@DefunWithValues{copy-tree, tree, new-tree}
@subsubheading Arguments and Values:
@param{tree}---a @term{tree}.
@param{new-tree}---a @term{tree}.
@subsubheading Description:
Creates a @term{copy} of a @term{tree} of @term{conses}.
If @param{tree} is not a @term{cons}, it is returned;
otherwise, the result is a new @term{cons} of the results of calling @symbolref{copy-tree, SYM}
on the @term{car} and @term{cdr} of @param{tree}.
In other words, all @term{conses} in the @term{tree} represented by @param{tree}
are copied recursively, stopping only when non-@term{conses} are encountered.
@symbolref{copy-tree, SYM} does not preserve circularities and the sharing of substructure.
@subsubheading Examples:
@lisp
(setq object (list (cons 1 "one")
(cons 2 (list 'a 'b 'c))))
@EV{} ((1 . "one") (2 A B C))
(setq object-too object) @EV{} ((1 . "one") (2 A B C))
(setq copy-as-list (copy-list object))
(setq copy-as-alist (copy-alist object))
(setq copy-as-tree (copy-tree object))
(eq object object-too) @EV{} @term{true}
(eq copy-as-tree object) @EV{} @term{false}
(eql copy-as-tree object) @EV{} @term{false}
(equal copy-as-tree object) @EV{} @term{true}
(setf (first (cdr (second object))) "a"
(car (second object)) "two"
(car object) '(one . 1)) @EV{} (ONE . 1)
object @EV{} ((ONE . 1) ("two" "a" B C))
object-too @EV{} ((ONE . 1) ("two" "a" B C))
copy-as-list @EV{} ((1 . "one") ("two" "a" B C))
copy-as-alist @EV{} ((1 . "one") (2 "a" B C))
copy-as-tree @EV{} ((1 . "one") (2 A B C))
@end lisp
@subsubheading See Also:
@ref{tree-equal}
@node sublis; nsublis
@syindexanchor{sublis, SYM}
@syindexanchor{nsublis, SYM}
@subsection sublis, nsublis (Function)
@cindex sublis
@cindex nsublis
@subsubheading Syntax:
@DefunWithValues{sublis, alist tree @keyparam{} keyparam test test-not, new-tree}
@DefunWithValues{nsublis, alist tree @keyparam{} keyparam test test-not, new-tree}
@subsubheading Arguments and Values:
@param{alist}---an @term{association list}.
@param{tree}---a @term{tree}.
@param{test}---a @term{designator} for a @term{function} of two @term{arguments}
that returns a @term{generalized boolean}.
@param{test-not}---a @term{designator} for
a @term{function} of two @term{arguments}
that returns a @term{generalized boolean}.
@param{key}---a @term{designator} for a @term{function} of one argument,
or @nil{}.
@param{new-tree}---a @term{tree}.
@subsubheading Description:
@symbolref{sublis, SYM} makes substitutions for @term{objects} in @param{tree}
(a structure of @term{conses}).
@symbolref{nsublis, SYM} is like @symbolref{sublis, SYM}
but destructively modifies the relevant
parts of the @param{tree}.
@symbolref{sublis, SYM} looks at all subtrees and leaves of @param{tree};
if a subtree or leaf appears as a key in @param{alist}
(that is, the key and the subtree or leaf @term{satisfy the test}),
it is replaced by the @term{object} with which that key is associated.
This operation is non-destructive. In effect, @symbolref{sublis, SYM} can
perform several @symbolref{subst, SYM} operations simultaneously.
If @symbolref{sublis, SYM} succeeds, a new copy of @param{tree} is returned in
which each occurrence of such a subtree or leaf is replaced by the
@term{object} with which it is associated. If no changes are made, the
original tree is returned. The original @param{tree} is left unchanged,
but the result tree may share cells with it.
@symbolref{nsublis, SYM} is permitted to modify @param{tree}
but otherwise returns the same values as @symbolref{sublis, SYM}.
@subsubheading Examples:
@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))
(setq tree1 '(1 (1 2) ((1 2 3)) (((1 2 3 4)))))
@EV{} (1 (1 2) ((1 2 3)) (((1 2 3 4))))
(sublis '((3 . "three")) tree1)
@EV{} (1 (1 2) ((1 2 "three")) (((1 2 "three" 4))))
(sublis '((t . "string"))
(sublis '((1 . "") (4 . 44)) tree1)
:key #'stringp)
@EV{} ("string" ("string" 2) (("string" 2 3)) ((("string" 2 3 44))))
tree1 @EV{} (1 (1 2) ((1 2 3)) (((1 2 3 4))))
(setq tree2 '("one" ("one" "two") (("one" "Two" "three"))))
@EV{} ("one" ("one" "two") (("one" "Two" "three")))
(sublis '(("two" . 2)) tree2)
@EV{} ("one" ("one" "two") (("one" "Two" "three")))
tree2 @EV{} ("one" ("one" "two") (("one" "Two" "three")))
(sublis '(("two" . 2)) tree2 :test 'equal)
@EV{} ("one" ("one" 2) (("one" "Two" "three")))
(nsublis '((t . 'temp))
tree1
:key #'(lambda (x) (or (atom x) (< (list-length x) 3))))
@EV{} ((QUOTE TEMP) (QUOTE TEMP) QUOTE TEMP)
@end lisp
@subsubheading Side Effects:
@symbolref{nsublis, SYM} modifies @param{tree}.
@subsubheading See Also:
@ref{subst},
@ref{Compiler Terminology},
@ref{Traversal Rules and Side Effects}
@subsubheading Notes:
The @kwd{test-not} parameter is deprecated.
Because the side-effecting variants (@eg{} @symbolref{nsublis, SYM}) potentially
change the path that is being traversed, their effects in the presence
of shared or circular structure structure may vary in surprising ways
when compared to their non-side-effecting alternatives. To see this,
consider the following side-effect behavior, which might be exhibited by
some implementations:
@lisp
(defun test-it (fn)
(let* ((shared-piece (list 'a 'b))
(data (list shared-piece shared-piece)))
(funcall fn '((a . b) (b . a)) data)))
(test-it #'sublis) @EV{} ((B A) (B A))
(test-it #'nsublis) @EV{} ((A B) (A B))
@end lisp
@node subst; subst-if; subst-if-not; nsubst; nsubst-if; nsubst-if-not
@syindexanchor{subst, SYM}
@syindexanchor{subst-if, SYM}
@syindexanchor{subst-if-not, SYM}
@syindexanchor{nsubst, SYM}
@syindexanchor{nsubst-if, SYM}
@syindexanchor{nsubst-if-not, SYM}
@subsection subst, subst-if, subst-if-not, nsubst, nsubst-if, nsubst-if-not (Function)
@cindex subst
@cindex subst-if
@cindex subst-if-not
@cindex nsubst
@cindex nsubst-if
@cindex nsubst-if-not
@anchor{subst}
@anchor{nsubst}
@subsubheading Syntax:
@DefunWithValues{subst, new old tree @keyparam{} keyparam test test-not, new-tree}
@DefunWithValues{subst-if, new predicate tree @keyparam{} keyparam, new-tree}
@DefunWithValues{subst-if-not, new predicate tree @keyparam{} keyparam, new-tree}
@DefunWithValues{nsubst, new old tree @keyparam{} keyparam test test-not, new-tree}
@DefunWithValues{nsubst-if, new predicate tree @keyparam{} keyparam, new-tree}
@DefunWithValues{nsubst-if-not, new predicate tree @keyparam{} keyparam, new-tree}
@subsubheading Arguments and Values:
@param{new}---an @term{object}.
@param{old}---an @term{object}.
@param{predicate}---a @term{symbol} that names a @term{function},
or a @term{function} of one argument
that returns a @term{generalized boolean} value.
@param{tree}---a @term{tree}.
@param{test}---a @term{designator} for a @term{function} of two @term{arguments}