-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlists.texi
2039 lines (1823 loc) · 79 KB
/
lists.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
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/lists
@node Lists, Sequences Arrays Vectors, Strings and Characters, Top
@c @chapter Lists
@chapter リスト
@c @cindex list
@c @cindex element (of list)
@cindex リスト
@cindex 要素(リスト)
@c A @dfn{list} represents a sequence of zero or more elements (which may
@c be any Lisp objects). The important difference between lists and
@c vectors is that two or more lists can share part of their structure; in
@c addition, you can insert or delete elements in a list without copying
@c the whole list.
@dfn{リスト}(list)は、0個以上の(任意のLispオブジェクトの)要素の列を
表現します。
リストとベクトルの重要な相違点は、
複数のリストがそれらの構造の一部を共有できることです。
さらに、リスト全体をコピーすることなく、
リストに要素を追加したり削除できることです。
@menu
* Cons Cells:: How lists are made out of cons cells.
* Lists as Boxes:: Graphical notation to explain lists.
* List-related Predicates:: Is this object a list? Comparing two lists.
* List Elements:: Extracting the pieces of a list.
* Building Lists:: Creating list structure.
* Modifying Lists:: Storing new pieces into an existing list.
* Sets And Lists:: A list can represent a finite mathematical set.
* Association Lists:: A list can represent a finite relation or mapping.
@end menu
@node Cons Cells
@c @section Lists and Cons Cells
@section リストとコンスセル
@c @cindex lists and cons cells
@c @cindex @code{nil} and lists
@cindex リストとコンスセル
@cindex @code{nil}とリスト
@c Lists in Lisp are not a primitive data type; they are built up from
@c @dfn{cons cells}. A cons cell is a data object that represents an
@c ordered pair. It holds, or ``points to,'' two Lisp objects, one labeled
@c as the @sc{car}, and the other labeled as the @sc{cdr}. These names are
@c traditional; see @ref{Cons Cell Type}. @sc{cdr} is pronounced
@c ``could-er.''
Lispのリストは基本データ型ではありません。
リストは@dfn{コンスセル}(cons cells)で構成されます。
コンスセルはドット対を表現するデータオブジェクトです。
ドット対は2つのLispオブジェクトを保持、つまり、『指し』ます。
その2つのLispオブジェクトの一方を@sc{car}、他方を@sc{cdr}といいます。
これらの名前は歴史的なものです。
@xref{Cons Cell Type}。
@sc{cdr}は『クダー』と読みます。
@c A list is a series of cons cells chained together, one cons cell per
@c element of the list. By convention, the @sc{car}s of the cons cells are
@c the elements of the list, and the @sc{cdr}s are used to chain the list:
@c the @sc{cdr} of each cons cell is the following cons cell. The @sc{cdr}
@c of the last cons cell is @code{nil}. This asymmetry between the
@c @sc{car} and the @sc{cdr} is entirely a matter of convention; at the
@c level of cons cells, the @sc{car} and @sc{cdr} slots have the same
@c characteristics.
リストはコンスセルを連ねたものであり、
リストの各要素ごとにコンスセルが1つあります。
慣習として、コンスセルの@sc{car}はリストの要素であり、
@sc{cdr}はリストを繋ぐために使います。
つまり、各コンスセルの@sc{cdr}は後続のコンスセルです。
最後のコンスセルの@sc{cdr}は@code{nil}です。
@sc{car}と@sc{cdr}の非対称性は単なる慣習によるものです。
コンスセルのレベルでは、@sc{car}と@sc{cdr}には同じ性質があります。
@c @cindex list structure
@cindex リスト構造
@c Because most cons cells are used as part of lists, the phrase
@c @dfn{list structure} has come to mean any structure made out of cons
@c cells.
ほとんどのコンスセルはリストの一部として使われるので、
@dfn{リスト構造}(list structure)という用語は、
コンスセルで構成した任意の構造を意味するようになりました。
@c The symbol @code{nil} is considered a list as well as a symbol; it is
@c the list with no elements. For convenience, the symbol @code{nil} is
@c considered to have @code{nil} as its @sc{cdr} (and also as its
@c @sc{car}).
シンボル@code{nil}は、シンボルであるとともにリストでもあるとみなします。
これは要素を持たないリストです。
慣習として、シンボル@code{nil}の@sc{cdr}(および@sc{car})は
@code{nil}であるとみなします。
@c The @sc{cdr} of any nonempty list @var{l} is a list containing all the
@c elements of @var{l} except the first.
空でない任意のリスト@var{l}の@sc{cdr}は、
@var{l}の先頭要素を除くすべての要素を含んだリストです。
@node Lists as Boxes
@comment node-name, next, previous, up
@c @section Lists as Linked Pairs of Boxes
@section 箱の対を連ねたリスト
@c @cindex box representation for lists
@c @cindex lists represented as boxes
@c @cindex cons cell as box
@cindex 箱表示、リスト
@cindex 箱表示によるリスト
@cindex コンスセルの箱表示
@c A cons cell can be illustrated as a pair of boxes. The first box
@c represents the @sc{car} and the second box represents the @sc{cdr}.
@c Here is an illustration of the two-element list, @code{(tulip lily)},
@c made from two cons cells:
コンスセルは1対の箱で図示できます。
最初の箱は@sc{car}を表し、2番目の箱は@sc{cdr}を表します。
つぎは、2つのコンスセルから成る
2要素のリスト@code{(tulip lily)}を図示したものです。
@example
@group
--------------- ---------------
| car | cdr | | car | cdr |
| tulip | o---------->| lily | nil |
| | | | | |
--------------- ---------------
@end group
@end example
@c Each pair of boxes represents a cons cell. Each box ``refers to'',
@c ``points to'' or ``contains'' a Lisp object. (These terms are
@c synonymous.) The first box, which describes the @sc{car} of the first
@c cons cell, contains the symbol @code{tulip}. The arrow from the
@c @sc{cdr} box of the first cons cell to the second cons cell indicates
@c that the @sc{cdr} of the first cons cell is the second cons cell.
各1対の箱がコンスセルを表します。
各箱は、Lispオブジェクトを『参照する』、『指す』、『含む』のです。
(これらの用語は同義語。)
最初のコンスセルの@sc{car}を表す最初の箱は、
シンボル@code{tulip}を含みます。
最初のコンスセルの@sc{cdr}箱から2番目のコンスセルへ向かう矢印は、
最初のコンスセルの@sc{cdr}が2番目のコンスセルであることを表します。
@c The same list can be illustrated in a different sort of box notation
@c like this:
同じリストは、つぎのような別の箱記法でも図示できます。
@example
@group
--- --- --- ---
| | |--> | | |--> nil
--- --- --- ---
| |
| |
--> tulip --> lily
@end group
@end example
@c Here is a more complex illustration, showing the three-element list,
@c @code{((pine needles) oak maple)}, the first element of which is a
@c two-element list:
つぎは、より複雑で、最初の要素が2要素リストであるような
3要素リストを図示したものです。
@example
@group
--- --- --- --- --- ---
| | |--> | | |--> | | |--> nil
--- --- --- --- --- ---
| | |
| | |
| --> oak --> maple
|
| --- --- --- ---
--> | | |--> | | |--> nil
--- --- --- ---
| |
| |
--> pine --> needles
@end group
@end example
@c The same list represented in the first box notation looks like this:
同じリストを最初の箱記法で表現するとつぎのようになります。
@example
@group
-------------- -------------- --------------
| car | cdr | | car | cdr | | car | cdr |
| o | o------->| oak | o------->| maple | nil |
| | | | | | | | | |
-- | --------- -------------- --------------
|
|
| -------------- ----------------
| | car | cdr | | car | cdr |
------>| pine | o------->| needles | nil |
| | | | | |
-------------- ----------------
@end group
@end example
@c @xref{Cons Cell Type}, for the read and print syntax of cons cells and
@c lists, and for more ``box and arrow'' illustrations of lists.
コンスセルとリストの入力構文と表示表現、および、
『箱と矢印』によるリストの図示については、@xref{Cons Cell Type}
@node List-related Predicates
@c @section Predicates on Lists
@section リスト向け述語
@c The following predicates test whether a Lisp object is an atom, is a
@c cons cell or is a list, or whether it is the distinguished object
@c @code{nil}. (Many of these predicates can be defined in terms of the
@c others, but they are used so often that it is worth having all of them.)
以下の述語は、Lispオブジェクトが、アトムであるか、
コンスセル、つまり、リストであるか、
特別なオブジェクト@code{nil}であるか調べます。
(これらの多く述語は、それぞれ残りの述語で定義可能である。
しかし、多用するため、これらすべてを用意しておく価値がある。)
@defun consp object
@c This function returns @code{t} if @var{object} is a cons cell, @code{nil}
@c otherwise. @code{nil} is not a cons cell, although it @emph{is} a list.
この関数は、@var{object}がコンスセルならば@code{t}を返し、
さもなければ@code{nil}を返す。
@code{nil}はコンスセルではないが、空リスト@emph{である}。
@end defun
@defun atom object
@c @cindex atoms
@cindex アトム
@c This function returns @code{t} if @var{object} is an atom, @code{nil}
@c otherwise. All objects except cons cells are atoms. The symbol
@c @code{nil} is an atom and is also a list; it is the only Lisp object
@c that is both.
この関数は、@var{object}がアトムならば@code{t}を返し、
さもなければ@code{nil}を返す。
コンスセルを除くすべてのオブジェクトはアトムである。
シンボル@code{nil}はアトムでもありリストでもある。
このようなLispオブジェクトは@code{nil}だけである。
@example
(atom @var{object}) @equiv{} (not (consp @var{object}))
@end example
@end defun
@defun listp object
@c This function returns @code{t} if @var{object} is a cons cell or
@c @code{nil}. Otherwise, it returns @code{nil}.
この関数は、@var{object}がコンスセルか@code{nil}ならば@code{t}を返す。
さもなければ@code{nil}を返す。
@example
@group
(listp '(1))
@result{} t
@end group
@group
(listp '())
@result{} t
@end group
@end example
@end defun
@defun nlistp object
@c This function is the opposite of @code{listp}: it returns @code{t} if
@c @var{object} is not a list. Otherwise, it returns @code{nil}.
この関数は、@code{listp}の反対である。
@var{object}がリストでなければ@code{t}を返す。
さもなければ@code{nil}を返す。
@example
(listp @var{object}) @equiv{} (not (nlistp @var{object}))
@end example
@end defun
@defun null object
@c This function returns @code{t} if @var{object} is @code{nil}, and
@c returns @code{nil} otherwise. This function is identical to @code{not},
@c but as a matter of clarity we use @code{null} when @var{object} is
@c considered a list and @code{not} when it is considered a truth value
@c (see @code{not} in @ref{Combining Conditions}).
この関数は、@var{object}が@code{nil}ならば@code{t}を返し、
さもなければ@code{nil}を返す。
この関数は、@code{not}と同一であるが、意図を明確にするために、
@var{object}をリストと考えるときには@code{null}を使い、
@var{object}を真理値と考えるときには@code{not}を使う
(@ref{Combining Conditions}の@code{not}を参照)
@example
@group
(null '(1))
@result{} nil
@end group
@group
(null '())
@result{} t
@end group
@end example
@end defun
@need 2000
@node List Elements
@c @section Accessing Elements of Lists
@section リストの要素の参照
@c @cindex list elements
@cindex リストの要素
@defun car cons-cell
@c This function returns the value pointed to by the first pointer of the
@c cons cell @var{cons-cell}. Expressed another way, this function
@c returns the @sc{car} of @var{cons-cell}.
この関数は、コンスセル@var{cons-cell}の最初のポインタが指す値を返す。
別のいい方をすれば、@var{cons-cell}の@sc{car}を返す。
@c As a special case, if @var{cons-cell} is @code{nil}, then @code{car}
@c is defined to return @code{nil}; therefore, any list is a valid argument
@c for @code{car}. An error is signaled if the argument is not a cons cell
@c or @code{nil}.
特別な場合として、@var{cons-cell}が@code{nil}のときには、
@code{car}は@code{nil}を返すと定義する。
したがって、任意のリストは@code{car}の正しい引数である。
引数がコンスセルでも@code{nil}でもなければエラーを通知する。
@example
@group
(car '(a b c))
@result{} a
@end group
@group
(car '())
@result{} nil
@end group
@end example
@end defun
@defun cdr cons-cell
@c This function returns the value pointed to by the second pointer of
@c the cons cell @var{cons-cell}. Expressed another way, this function
@c returns the @sc{cdr} of @var{cons-cell}.
この関数は、コンスセル@var{cons-cell}の2番目のポインタが指す値を返す。
別のいい方をすれば、@var{cons-cell}の@sc{cdr}を返す。
@c As a special case, if @var{cons-cell} is @code{nil}, then @code{cdr}
@c is defined to return @code{nil}; therefore, any list is a valid argument
@c for @code{cdr}. An error is signaled if the argument is not a cons cell
@c or @code{nil}.
特別な場合として、@var{cons-cell}が@code{nil}のときには、
@code{cdr}は@code{nil}を返すと定義する。
したがって、任意のリストは@code{cdr}の正しい引数である。
引数がコンスセルでも@code{nil}でもなければエラーを通知する。
@example
@group
(cdr '(a b c))
@result{} (b c)
@end group
@group
(cdr '())
@result{} nil
@end group
@end example
@end defun
@defun car-safe object
@c This function lets you take the @sc{car} of a cons cell while avoiding
@c errors for other data types. It returns the @sc{car} of @var{object} if
@c @var{object} is a cons cell, @code{nil} otherwise. This is in contrast
@c to @code{car}, which signals an error if @var{object} is not a list.
この関数は、コンスセルの@sc{car}を取り出すが、
他のデータ型に対するエラーを回避する。
@var{object}がコンスセルならば@var{object}の@sc{car}を返すが、
さもなければ@code{nil}を返す。
これは@code{car}と対照的であり、
@code{car}は@var{object}がリストでないとエラーを通知する。
@example
@group
(car-safe @var{object})
@equiv{}
(let ((x @var{object}))
(if (consp x)
(car x)
nil))
@end group
@end example
@end defun
@defun cdr-safe object
@c This function lets you take the @sc{cdr} of a cons cell while
@c avoiding errors for other data types. It returns the @sc{cdr} of
@c @var{object} if @var{object} is a cons cell, @code{nil} otherwise.
@c This is in contrast to @code{cdr}, which signals an error if
@c @var{object} is not a list.
この関数は、コンスセルの@sc{cdr}を取り出すが、
他のデータ型に対するエラーを回避する。
@var{object}がコンスセルならば@var{object}の@sc{cdr}を返すが、
さもなければ@code{nil}を返す。
これは@code{cdr}と対照的であり、
@code{cdr}は@var{object}がリストでないとエラーを通知する。
@example
@group
(cdr-safe @var{object})
@equiv{}
(let ((x @var{object}))
(if (consp x)
(cdr x)
nil))
@end group
@end example
@end defun
@defun nth n list
@c This function returns the @var{n}th element of @var{list}. Elements
@c are numbered starting with zero, so the @sc{car} of @var{list} is
@c element number zero. If the length of @var{list} is @var{n} or less,
@c the value is @code{nil}.
この関数は、@var{list}の@var{n}番目の要素を返す。
要素は0から数えるので、@var{list}の@sc{car}は要素番号0。
@var{list}の長さが@var{n}かそれ未満であると、値は@code{nil}になる。
@c If @var{n} is negative, @code{nth} returns the first element of
@c @var{list}.
@var{n}が負であると、@code{nth}は@var{list}の最初の要素を返す。
@example
@group
(nth 2 '(1 2 3 4))
@result{} 3
@end group
@group
(nth 10 '(1 2 3 4))
@result{} nil
@end group
@group
(nth -3 '(1 2 3 4))
@result{} 1
(nth n x) @equiv{} (car (nthcdr n x))
@end group
@end example
@c The function @code{elt} is similar, but applies to any kind of sequence.
@c For historical reasons, it takes its arguments in the opposite order.
@c @xref{Sequence Functions}.
関数@code{elt}も同様であるが、任意のシーケンスに適用できる。
歴史的な理由で引数の順序は逆である。
@pxref{Sequence Functions}。
@end defun
@defun nthcdr n list
@c This function returns the @var{n}th @sc{cdr} of @var{list}. In other
@c words, it skips past the first @var{n} links of @var{list} and returns
@c what follows.
この関数は、@var{list}の@var{n}番目の@sc{cdr}を返す。
いいかえれば、@var{list}の始めの@var{n}個のリンクを飛び越えて、
そのあとにあるものを返す。
@c If @var{n} is zero or negative, @code{nthcdr} returns all of
@c @var{list}. If the length of @var{list} is @var{n} or less,
@c @code{nthcdr} returns @code{nil}.
@var{n}が0か負であると、@code{nthcdr}は@var{list}全体を返す。
@var{list}の長さが@var{n}かそれ未満であると、
@code{nthcdr}は@code{nil}を返す。
@example
@group
(nthcdr 1 '(1 2 3 4))
@result{} (2 3 4)
@end group
@group
(nthcdr 10 '(1 2 3 4))
@result{} nil
@end group
@group
(nthcdr -3 '(1 2 3 4))
@result{} (1 2 3 4)
@end group
@end example
@end defun
@defun safe-length list
@tindex safe-length
@c This function returns the length of @var{list}, with no risk
@c of either an error or an infinite loop.
この関数は、エラーや無限ループを回避して、@var{list}の長さを返す。
@c If @var{list} is not really a list, @code{safe-length} returns 0. If
@c @var{list} is circular, it returns a finite value which is at least the
@c number of distinct elements.
@var{list}が実際にはリストでない場合には、@code{safe-length}は0を返す。
@var{list}に循環があると、少なくとも異なる要素の個数を表す有限値を返す。
@end defun
@c The most common way to compute the length of a list, when you are not
@c worried that it may be circular, is with @code{length}. @xref{Sequence
@c Functions}.
循環はないと思われるリストの長さを計算するもっとも一般的な方法は、
@code{length}です。
@xref{Sequence Functions}。
@defun caar cons-cell
@tindex caar
@c This is the same as @code{(car (car @var{cons-cell}))}.
これは@code{(car (car @var{cons-cell}))}と同じ。
@end defun
@defun cadr cons-cell
@tindex cadr
@c This is the same as @code{(car (cdr @var{cons-cell}))}
@c or @code{(nth 1 @var{cons-cell})}.
これは@code{(car (cdr @var{cons-cell}))}や
@code{(nth 1 @var{cons-cell})}と同じ。
@end defun
@defun cdar cons-cell
@tindex cdar
@c This is the same as @code{(cdr (car @var{cons-cell}))}.
これは@code{(cdr (car @var{cons-cell}))}と同じ。
@end defun
@defun cddr cons-cell
@tindex cddr
@c This is the same as @code{(cdr (cdr @var{cons-cell}))}
@c or @code{(nthcdr 2 @var{cons-cell})}.
これは@code{(cdr (cdr @var{cons-cell}))}や
@code{(nthcdr 2 @var{cons-cell})}と同じ。
@end defun
@node Building Lists
@comment node-name, next, previous, up
@c @section Building Cons Cells and Lists
@section コンスセルとリストの構築
@c @cindex cons cells
@c @cindex building lists
@cindex コンスセル
@cindex リストの構築
@c Many functions build lists, as lists reside at the very heart of Lisp.
@c @code{cons} is the fundamental list-building function; however, it is
@c interesting to note that @code{list} is used more times in the source
@c code for Emacs than @code{cons}.
リストはLispの中核なので、多くの関数はリストを構築します。
@code{cons}は基本的なリスト構築関数です。
しかし、Emacsのソースコードでは、@code{cons}より@code{list}を
多用していることは興味深いことです。
@defun cons object1 object2
@c This function is the fundamental function used to build new list
@c structure. It creates a new cons cell, making @var{object1} the
@c @sc{car}, and @var{object2} the @sc{cdr}. It then returns the new cons
@c cell. The arguments @var{object1} and @var{object2} may be any Lisp
@c objects, but most often @var{object2} is a list.
この関数は、新たなリスト構造を構築するために使う基本関数。
@var{object1}を@sc{car}、@var{object2}を@sc{cdr}とする
新たなコンスセルを作成し、このコンスセルを返す。
引数@var{object1}と@var{object2}はどんなLispオブジェクトでもよいが、
ほとんどの場合、@var{object2}はリストである。
@example
@group
(cons 1 '(2))
@result{} (1 2)
@end group
@group
(cons 1 '())
@result{} (1)
@end group
@group
(cons 1 2)
@result{} (1 . 2)
@end group
@end example
@c @cindex consing
@cindex コンスする
@c @code{cons} is often used to add a single element to the front of a
@c list. This is called @dfn{consing the element onto the list}. For
@c example:
@code{cons}は、リストの先頭に要素を1つ追加するために
しばしば使われる。
これを@dfn{要素をリストにコンスする}という。
たとえば、つぎのとおり。
@example
(setq list (cons newelt list))
@end example
@c Note that there is no conflict between the variable named @code{list}
@c used in this example and the function named @code{list} described below;
@c any symbol can serve both purposes.
この例における@code{list}という名前の変数と
以下に述べる@code{list}という名前の関数とは衝突しない。
任意のシンボルはどちらの目的にも使える。
@end defun
@defun list &rest objects
@c This function creates a list with @var{objects} as its elements. The
@c resulting list is always @code{nil}-terminated. If no @var{objects}
@c are given, the empty list is returned.
この関数は、@var{objects}を要素とするリストを作成する。
結果のリストはつねに@code{nil}終端になる。
@var{objects}を指定しないと空リストを返す。
@example
@group
(list 1 2 3 4 5)
@result{} (1 2 3 4 5)
@end group
@group
(list 1 2 '(3 4 5) 'foo)
@result{} (1 2 (3 4 5) foo)
@end group
@group
(list)
@result{} nil
@end group
@end example
@end defun
@defun make-list length object
@c This function creates a list of length @var{length}, in which all the
@c elements have the identical value @var{object}. Compare
@c @code{make-list} with @code{make-string} (@pxref{Creating Strings}).
この関数は、すべての要素が同一の値@var{object}であり
長さが@var{length}のリストを作成する。
@code{make-string}と比較してほしい(@pxref{Creating Strings})。
@example
@group
(make-list 3 'pigs)
@result{} (pigs pigs pigs)
@end group
@group
(make-list 0 'pigs)
@result{} nil
@end group
@end example
@end defun
@defun append &rest sequences
@c @cindex copying lists
@cindex リストのコピー
@c This function returns a list containing all the elements of
@c @var{sequences}. The @var{sequences} may be lists, vectors,
@c bool-vectors, or strings, but the last one should usually be a list.
@c All arguments except the last one are copied, so none of the arguments
@c is altered. (See @code{nconc} in @ref{Rearrangement}, for a way to join
@c lists with no copying.)
この関数は@var{sequences}のすべての要素から成るリストを返す。
@var{sequences}は、リスト、ベクトル、ブールベクトル、文字列のいずれかであるが、
普通、最後の要素はリストである。
最後の引数を除いてすべての引数をコピーするので、どの引数も変更しない
(コピーせずにリストを繋ぐ方法については、
@ref{Rearrangement}の@code{nconc}を参照。)
@c More generally, the final argument to @code{append} may be any Lisp
@c object. The final argument is not copied or converted; it becomes the
@c @sc{cdr} of the last cons cell in the new list. If the final argument
@c is itself a list, then its elements become in effect elements of the
@c result list. If the final element is not a list, the result is a
@c ``dotted list'' since its final @sc{cdr} is not @code{nil} as required
@c in a true list.
一般には、@code{append}の最後の引数はどんなLispオブジェクトでもよい。
最後の引数をコピーしたり変換したりしない。
それは、新たなリストの最後のコンスセルの@sc{cdr}になる。
最後の引数がそれ自体リストであれば、それらの要素は、実質的には、
結果のリストの要素になる。
最後の要素がリストでなければ、結果は『ドット対』になる。
なぜなら、結果の最後の@sc{cdr}は、
真のリストに必要とされる@code{nil}ではないからである。
@c The @code{append} function also allows integers as arguments. It
@c converts them to strings of digits, making up the decimal print
@c representation of the integer, and then uses the strings instead of the
@c original integers. @strong{Don't use this feature; we plan to eliminate
@c it. If you already use this feature, change your programs now!} The
@c proper way to convert an integer to a decimal number in this way is with
@c @code{format} (@pxref{Formatting Strings}) or @code{number-to-string}
@c (@pxref{String Conversion}).
関数@code{append}は、引数として整数も受け付ける。
整数を10進の表示表現の文字列に変換してから、
その文字列を整数のかわりに使う。
@strong{この機能を使わないでほしい。
削除する予定である。
読者がこの機能を使っていたら、今すぐプログラムを直すこと!}@code{ }
整数をこのような10進数に変換する正しい方法は、
@code{format}(@pxref{Formatting Strings})や
@code{number-to-string}(@pxref{String Conversion})を使うことである。
@end defun
@c Here is an example of using @code{append}:
@code{append}の使用例をつぎに示します。
@example
@group
(setq trees '(pine oak))
@result{} (pine oak)
(setq more-trees (append '(maple birch) trees))
@result{} (maple birch pine oak)
@end group
@group
trees
@result{} (pine oak)
more-trees
@result{} (maple birch pine oak)
@end group
@group
(eq trees (cdr (cdr more-trees)))
@result{} t
@end group
@end example
@c You can see how @code{append} works by looking at a box diagram. The
@c variable @code{trees} is set to the list @code{(pine oak)} and then the
@c variable @code{more-trees} is set to the list @code{(maple birch pine
@c oak)}. However, the variable @code{trees} continues to refer to the
@c original list:
箱表示を見れば@code{append}の動作を理解できるでしょう。
変数@code{trees}にリスト@code{(pine oak)}を設定し、ついで、
変数@code{more-trees}にはリスト@code{(maple birch pine oak)}を設定します。
しかし、変数@code{trees}はもとのリストを指し続けます。
@smallexample
@group
more-trees trees
| |
| --- --- --- --- -> --- --- --- ---
--> | | |--> | | |--> | | |--> | | |--> nil
--- --- --- --- --- --- --- ---
| | | |
| | | |
--> maple -->birch --> pine --> oak
@end group
@end smallexample
@c An empty sequence contributes nothing to the value returned by
@c @code{append}. As a consequence of this, a final @code{nil} argument
@c forces a copy of the previous argument:
空シーケンスは@code{append}が返す値にはまったく寄与しません。
この結果、最後の@code{nil}引数は直前の引数をコピーするように強制します。
@example
@group
trees
@result{} (pine oak)
@end group
@group
(setq wood (append trees nil))
@result{} (pine oak)
@end group
@group
wood
@result{} (pine oak)
@end group
@group
(eq wood trees)
@result{} nil
@end group
@end example
@noindent
@c This once was the usual way to copy a list, before the function
@c @code{copy-sequence} was invented. @xref{Sequences Arrays Vectors}.
この方法は、関数@code{copy-sequence}を導入するまでは、
リストをコピーする普通の方法でした。
@xref{Sequences Arrays Vectors}。
@c Here we show the use of vectors and strings as arguments to @code{append}:
@code{append}の引数にベクトルと文字列を使った例をつぎに示します。
@example
@group
(append [a b] "cd" nil)
@result{} (a b 99 100)
@end group
@end example
@c With the help of @code{apply} (@pxref{Calling Functions}), we can append
@c all the lists in a list of lists:
@code{apply}(@pxref{Calling Functions})の助けを借りれば、
リストのリストの中にあるすべてのリストを連結できます。
@example
@group
(apply 'append '((a b c) nil (x y z) nil))
@result{} (a b c x y z)
@end group
@end example
@c If no @var{sequences} are given, @code{nil} is returned:
@var{sequences}をまったく指定しないと@code{nil}を返します。
@example
@group
(append)
@result{} nil
@end group
@end example
@c Here are some examples where the final argument is not a list:
最後の引数がリストではない例をいくつか示します。
@example
(append '(x y) 'z)
@result{} (x y . z)
(append '(x y) [z])
@result{} (x y . [z])
@end example
@noindent
@c The second example shows that when the final argument is a sequence but
@c not a list, the sequence's elements do not become elements of the
@c resulting list. Instead, the sequence becomes the final @sc{cdr}, like
@c any other non-list final argument.
最後の引数がリストではなくシーケンスである2番目の例は、
シーケンスの要素が結果のリストの要素にはならないことを示しています。
そのかわりに、最後の引数がリストでない場合と同様に、
シーケンスが最後の@sc{cdr}になります。
@defun reverse list
@c This function creates a new list whose elements are the elements of
@c @var{list}, but in reverse order. The original argument @var{list} is
@c @emph{not} altered.
この関数は、@var{list}の要素を逆順にした新たなリストを作成する。
もとの引数@var{list}は変更@emph{しない}。
@example
@group
(setq x '(1 2 3 4))
@result{} (1 2 3 4)
@end group
@group
(reverse x)
@result{} (4 3 2 1)
x
@result{} (1 2 3 4)
@end group
@end example
@end defun
@node Modifying Lists
@c @section Modifying Existing List Structure
@section 既存のリスト構造の修正
@c @cindex destructive list operations
@cindex 破壊的なリスト操作
@c You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the
@c primitives @code{setcar} and @code{setcdr}. We call these ``destructive''
@c operations because they change existing list structure.
基本関数@code{setcar}や@code{setcdr}を使って、
コンスセルの@sc{car}や@sc{cdr}の内容を変更できます。
これらは、既存のリスト構造を変更するので、
『破壊的』な操作と呼びます。
@c @cindex CL note---@code{rplaca} vrs @code{setcar}
@cindex CLに関した注意−−@code{rplaca}と@code{setcar}
@quotation
@findex rplaca
@findex rplacd
@c @b{Common Lisp note:} Common Lisp uses functions @code{rplaca} and
@c @code{rplacd} to alter list structure; they change structure the same
@c way as @code{setcar} and @code{setcdr}, but the Common Lisp functions
@c return the cons cell while @code{setcar} and @code{setcdr} return the
@c new @sc{car} or @sc{cdr}.
@b{Common Lispに関した注意:}@code{ }
Common Lispでは、
リスト構造を変更するには@code{rplaca}や@code{rplacd}を使う。
これらは@code{setcar}や@code{setcdr}と同様に構造を変更する。
しかし、Common Lispの関数はコンスセルを返すが、
@code{setcar}や@code{setcdr}は新たな@sc{car}や@sc{cdr}を返す。
@end quotation
@menu
* Setcar:: Replacing an element in a list.
* Setcdr:: Replacing part of the list backbone.
This can be used to remove or add elements.
* Rearrangement:: Reordering the elements in a list; combining lists.
@end menu
@node Setcar
@c @subsection Altering List Elements with @code{setcar}
@subsection @code{setcar}によるリスト要素の変更
@c Changing the @sc{car} of a cons cell is done with @code{setcar}. When
@c used on a list, @code{setcar} replaces one element of a list with a
@c different element.
コンスセルの@sc{car}を変更するには、@code{setcar}を使います。
リストに対して使用すると、
@code{setcar}はリストの1つの要素を別の要素に置き換えます。
@defun setcar cons object
@c This function stores @var{object} as the new @sc{car} of @var{cons},
@c replacing its previous @sc{car}. In other words, it changes the
@c @sc{car} slot of @var{cons} to point to @var{object}. It returns the
@c value @var{object}. For example:
この関数は、@var{cons}の新たな@sc{car}として@var{object}を格納し、
以前の@sc{car}を置き換える。
いいかえれば、@var{cons}の@sc{car}スロットが@var{object}を指すように変更する。
この関数は値@var{object}を返す。
たとえば、つぎのようになる。
@example
@group
(setq x '(1 2))
@result{} (1 2)
@end group
@group
(setcar x 4)
@result{} 4
@end group
@group
x
@result{} (4 2)
@end group
@end example
@end defun
@c When a cons cell is part of the shared structure of several lists,
@c storing a new @sc{car} into the cons changes one element of each of
@c these lists. Here is an example:
コンスセルが複数のリストの共有構造の一部であるときには、
コンスセルに新たな@sc{car}を格納すると、
そのような各リストの1つの要素を変更することになります。
@example
@group
@c ;; @r{Create two lists that are partly shared.}
;; @r{共有部分がある2つのリストを作る}
(setq x1 '(a b c))
@result{} (a b c)
(setq x2 (cons 'z (cdr x1)))
@result{} (z b c)
@end group
@group
@c ;; @r{Replace the @sc{car} of a shared link.}
;; @r{共有部分の@sc{car}を置き換える}
(setcar (cdr x1) 'foo)
@result{} foo
@c x1 ; @r{Both lists are changed.}
x1 ; @r{両方のリストが変更されている}
@result{} (a foo c)
x2
@result{} (z foo c)
@end group
@group
@c ;; @r{Replace the @sc{car} of a link that is not shared.}
;; @r{非共有部分の@sc{car}を置き換える}
(setcar x1 'baz)
@result{} baz
@c x1 ; @r{Only one list is changed.}
x1 ; @r{1つのリストだけが変更されている}
@result{} (baz foo c)
x2
@result{} (z foo c)
@end group
@end example
@c Here is a graphical depiction of the shared structure of the two lists
@c in the variables @code{x1} and @code{x2}, showing why replacing @code{b}
@c changes them both:
変数@code{x1}と@code{x2}に入っている共有部分を持つ2つのリストを図示すると
つぎのようになります。
@code{b}を置き換えるとなぜ両者が変更されるのかわかるでしょう。
@example
@group
--- --- --- --- --- ---
x1---> | | |----> | | |--> | | |--> nil
--- --- --- --- --- ---
| --> | |
| | | |
--> a | --> b --> c
|
--- --- |
x2--> | | |--
--- ---
|
|
--> z
@end group
@end example
@c Here is an alternative form of box diagram, showing the same relationship:
同じ関係を別の箱表示で示します。
@example
@group
x1:
-------------- -------------- --------------
| car | cdr | | car | cdr | | car | cdr |
| a | o------->| b | o------->| c | nil |
| | | -->| | | | | |
-------------- | -------------- --------------