-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkseque.tex
2669 lines (2304 loc) · 135 KB
/
kseque.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{KSeque, 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{Sequences}
\label{KSEQUE}
The type \cdf{sequence} encompasses both lists and vectors (one-dimensional
arrays).
While these are different data structures with different structural
properties leading to different algorithmic uses, they do have a common
property: each contains an ordered set of elements.
Note that {\nil} is considered to be a sequence of length zero.
Some operations are useful on both lists and arrays
because they deal with ordered sets of elements. One may ask the number
of elements, reverse the ordering, extract a subsequence, and so on. For
such purposes Common Lisp provides a set of generic functions on sequences.
Note that this remark, predating the design of the Common Lisp Object System,
uses the term ``generic'' in a generic sense, and not necessarily
in the technical sense used by CLOS
(see chapter~\ref{DTYPES}).
\begin{flushleft}
\cf
\begin{tabular*}{\textwidth}{@{}l@{\extracolsep{\fill}}lll@{}}
\cdf{elt}&\cdf{reverse}&\cdf{map}&\cdf{remove} \\
\cdf{length}&\cdf{nreverse}&\cdf{some}&\cdf{remove-duplicates} \\
\cdf{subseq}&\cdf{concatenate}&\cdf{every}&\cdf{delete} \\
\cdf{copy-seq}&\cdf{position}&\cdf{notany}&\cdf{delete-duplicates} \\
\cdf{fill}&\cdf{find}&\cdf{notevery}&\cdf{substitute} \\
\cdf{replace}&\cdf{sort}&\cdf{reduce}&\cdf{nsubstitute} \\
\cdf{count}&\cdf{merge}&\cdf{search}&\cdf{mismatch}
\end{tabular*}
\end{flushleft}
Some of these operations come in more than one version.
Such versions are indicated by adding a suffix (or occasionally a prefix)
to the basic name of the operation.
In addition, many operations accept one or more optional keyword
arguments that can modify the operation in various ways.
If the operation requires testing sequence elements according to
some criterion, then the criterion may be specified in one of two ways.
The basic operation accepts an item,
and elements are tested for being \cdf{eql} to that item.
(A test other than \cdf{eql} can be specified by the \cd{:test}
or \cd{:test-not} keyword. It is an error to use both
of these keywords in the same call.)
The variants formed by adding \cdf{-if} and \cdf{-if-not}
to the basic operation name do not take an item,
but instead a one-argument predicate,
and elements are tested for satisfying or not satisfying the predicate.
As an example,
\begin{lisp}
(remove \emph{item} \emph{sequence})
\end{lisp}
returns a copy of \emph{sequence} from which all elements \cdf{eql} to \emph{item}
have been removed;
\begin{lisp}
(remove \emph{item} \emph{sequence} \cd{:test} \#'equal)
\end{lisp}
returns a copy of \emph{sequence} from which all elements \cdf{equal} to \emph{item}
have been removed;
\begin{lisp}
(remove-if \#'numberp \emph{sequence})
\end{lisp}
returns a copy of \emph{sequence} from which all numbers have been removed.
If an operation tests elements of a sequence in any manner,
the keyword argument \cd{:key}, if not {\false}, should be a function
of one argument that will extract from an element the part to be tested
in place of the whole element.
For example, the effect of the MacLisp expression
\cd{(assq item seq)} could be obtained by
\begin{lisp}
(find \emph{item} \emph{sequence} \cd{:test} \#'eq \cd{:key} \#'car)
\end{lisp}
This searches for the first element of \emph{sequence} whose \emph{car} is \cdf{eq}
to \emph{item}.
\begin{newer}
X3J13 voted in June 1988 \issue{FUNCTION-TYPE} to allow the \cd{:key} function
to be only of type \cdf{symbol} or \cdf{function}; a lambda-expression
is no longer acceptable as a functional argument. One must use the
\cdf{function} special operator or the abbreviation \cd{\#'} before
a lambda-expression that appears as an explicit argument form.
\end{newer}
For some operations it can be useful to specify the direction
in which the sequence is conceptually processed. In this case the basic
operation normally processes the sequence in the forward direction,
and processing in the reverse direction is indicated by a non-{\false}
value for the keyword argument \cd{:from-end}. (The processing order
specified by the \cd{:from-end} is purely conceptual. Depending on
the object to be processed and on the implementation, the actual processing
order may be different. For this reason a user-supplied \emph{test} function
should be free of side effects.)
Many operations allow the specification of a subsequence to be operated
upon. Such operations have keyword arguments
called \cd{:start} and \cd{:end}. These arguments should be integer indices
into the sequence, with $\emph{start}\leq\emph{end}$
(it is an error if $\emph{start}>\emph{end}$). They indicate
the subsequence starting with and \emph{including} element \emph{start}
and up to but \emph{excluding} element \emph{end}. The length of the subsequence
is therefore $\emph{end}-\emph{start}$. If \emph{start} is omitted,
it defaults to zero; and if \emph{end} is omitted or {\false}, it defaults to
the length of the sequence.
Therefore if both \emph{start} and \emph{end} are omitted, the entire sequence
is processed by default.
For the most part, subsequence specification
is permitted purely for the sake of efficiency;
one could simply call \cdf{subseq} instead to extract the subsequence
before operating on it. Note, however, that operations that
calculate indices
return indices into the original sequence, not into the subsequence:
\begin{lisp}
(position \#{\Xbackslash}b "foobar" \cd{:start} 2 \cd{:end} 5) \EV\ 3 \\
(position \#{\Xbackslash}b (subseq "foobar" 2 5)) \EV\ 1
\end{lisp}
If two sequences are involved, then
the keyword arguments
\cd{:start1}, \cd{:end1}, \cd{:start2}, and \cd{:end2} are used to
specify separate subsequences for each sequence.
\begin{newer}
X3J13 voted in June 1988 \issue{SUBSEQ-OUT-OF-BOUNDS}
(and further clarification was voted in January 1989
\issue{RANGE-OF-START-AND-END-PARAMETERS})
to specify that these rules apply not
only to all built-in functions that have keyword parameters named
\cd{:start}, \cd{:start1}, \cd{:start2}, \cd{:end}, \cd{:end1},
or \cd{:end2} but also to functions such as \cdf{subseq}
that take required or optional parameters that are documented
as being named \emph{start} or \emph{end}.
\begin{itemize}
\item A ``start'' argument must always be a non-negative integer and
defaults to zero if not supplied; it is not permissible to pass \cdf{nil}
as a ``start'' argument.
\item An ``end'' argument must be either a
non-negative integer or \cdf{nil} (which indicates the end of the
sequence) and defaults to \cdf{nil}
if not supplied; therefore supplying \cdf{nil} is equivalent to
not supplying such an argument.
\item If the ``end'' argument is an integer, it must be no greater than the
active length of the corresponding sequence
(as returned by the function \cdf{length}).
\item The default value for the ``end'' argument is the active length
of the corresponding sequence.
\item The ``start'' value (after defaulting, if necessary) must not be greater than the
corresponding ``end'' value (after defaulting, if necessary).
\end{itemize}
This may be summarized as follows.
Let \emph{x} be the sequence within which indices are to be considered. Let \emph{s} be
the ``start'' argument for that sequence of any standard function,
whether explicitly specified or defaulted, through omission, to
zero. Let \emph{e} be the ``end'' argument for that sequence
of any standard function, whether explicitly specified or defaulted, through
omission or an explicitly passed \cdf{nil} value, to the active length of \emph{x}, as
returned by \cdf{length}. Then it is an error if the test
\cd{(<=~0~\emph{s}~\emph{e}~(length \emph{x}))}
is not true.
\end{newer}
For some functions, notably \cdf{remove} and \cdf{delete}, the keyword argument
\cd{:count} is used to specify how many occurrences of the item should
be affected. If this is {\false} or is not supplied, all matching items are
affected.
In the following function descriptions, an element \emph{x} of a sequence
``satisfies the test'' if any of the following holds:
\begin{itemize}
\item
A basic function was called,
\emph{testfn} was specified by the keyword \cd{:test}, and
\cd{(funcall \emph{testfn} \emph{item} (\emph{keyfn} \emph{x}))} is true.
\item
A basic function was called,
\emph{testfn} was specified by the keyword \cd{:test-not}, and
\cd{(funcall \emph{testfn} \emph{item} (\emph{keyfn} \emph{x}))} is false.
\item
An \cdf{-if} function was called, and
\cd{(funcall \emph{predicate} (\emph{keyfn} \emph{x}))} is true.
\item
An \cdf{-if-not} function was called, and
\cd{(funcall \emph{predicate} (\emph{keyfn} \emph{x}))} is false.
\end{itemize}
In each case \emph{keyfn} is the
value of the \cd{:key} keyword argument (the default being the identity
function). See, for example, \cdf{remove}.
In the following function descriptions,
two elements \emph{x} and \emph{y} taken from sequences ``match'' if
either of the following holds:
\begin{itemize}
\item
\emph{testfn} was specified by the keyword \cd{:test}, and
\cd{(funcall \emph{testfn} (\emph{keyfn} \emph{x}) (\emph{keyfn} \emph{y}))} is true.
\item
\emph{testfn} was specified by the keyword \cd{:test-not}, and
\cd{(funcall \emph{testfn} (\emph{keyfn} \emph{x}) (\emph{keyfn} \emph{y}))} is false.
\end{itemize}
See, for example, \cdf{search}.
\begin{newer}
X3J13 voted in June 1988 \issue{FUNCTION-TYPE} to allow the \emph{testfn}
or \cdf{predicate}
to be only of type \cdf{symbol} or \cdf{function}; a lambda-expression
is no longer acceptable as a functional argument. One must use the
\cdf{function} special operator or the abbreviation \cd{\#'} before
a lambda-expression that appears as an explicit argument form.
\end{newer}
You may depend on the order in which arguments
are given to \emph{testfn}; this permits the use of non-commutative
test functions in a predictable manner.
The order of the arguments to \emph{testfn} corresponds
to the order in which those arguments (or the sequences containing
those arguments)
were given to the sequence function in question.
If a sequence function gives two elements from the same
sequence argument to \emph{testfn}, they are given in the same order in
which they appear in the sequence.
Whenever a sequence function must construct and return
a new vector, it always returns a \emph{simple}
vector (see section~\ref{ARRAY-TYPE-SECTION}).
Similarly, any strings constructed will be simple strings.
\begin{defun}[Function]
complement fn
Returns a function whose value is the same as that of \cdf{not}
applied to the result of applying the function \emph{fn} to the same
arguments. One could define \cdf{complement} as follows:
\begin{lisp}
(defun complement (fn) \\*
~~\#'(lambda (\&rest arguments) \\*
~~~~~~(not (apply fn arguments))))
\end{lisp}
One intended use of \cdf{complement} is to supplant the use of
\cd{:test-not} arguments and \cdf{-if-not} functions.
\begin{lisp}
(remove-if-not \#'virtuous senators) {\EQ} \\*
~~~(remove-if (complement \#'virtuous) senators) \\
\\
(remove-duplicates telephone-book \\*
~~~~~~~~~~~~~~~~~~~:test-not \#'mismatch) {\EQ} \\*
~~~(remove-duplicates telephone-book \\*
~~~~~~~~~~~~~~~~~~~~~~:test (complement \#'mismatch))
\end{lisp}
\end{defun}
\section{Simple Sequence Functions}
Most of the following functions perform simple operations on a single
sequence; \cdf{make-sequence} constructs a new sequence.
\begin{defun}[Function]
elt sequence index
This returns the element of \emph{sequence} specified by \emph{index},
which must be a non-negative integer less than the length of the \emph{sequence}
as returned by \cdf{length}.
The first element of a sequence has index \cd{0}.
(Note that \cdf{elt} observes the fill pointer in those vectors that have
fill pointers. The array-specific function \cdf{aref} may be used
to access vector elements that are beyond the vector's fill pointer.)
\cdf{setf} may be used with \cdf{elt} to destructively replace
a sequence element with a new value.
\end{defun}
\begin{defun}[Function]
subseq sequence start &optional end
This returns the subsequence of \emph{sequence} specified by \emph{start} and
\emph{end}.
\cdf{subseq} \emph{always} allocates a new sequence for a result; it never
shares storage with an old sequence. The result subsequence is always of
the same type as the argument \emph{sequence}.
\cdf{setf} may be used with \cdf{subseq} to destructively replace
a subsequence with a sequence of new values; see also \cdf{replace}.
\end{defun}
\begin{defun}[Function]
copy-seq sequence
A copy is made of the argument \emph{sequence}; the result is \cdf{equalp}
to the argument but not \cdf{eq} to it.
\begin{lisp}
(copy-seq \emph{x}) \EQ\ (subseq \emph{x} 0)
\end{lisp}
but the name \cdf{copy-seq} is more perspicuous when applicable.
\end{defun}
\begin{defun}[Function]
length sequence
The number of elements in \emph{sequence} is returned as a non-negative integer.
If the sequence is a vector with a fill pointer,
the ``active length'' as specified by the fill pointer is returned
(see section~\ref{FILL-POINTER}).
\end{defun}
\begin{defun}[Function]
reverse sequence
The result is a new sequence of the same kind as \emph{sequence},
containing the same elements but in reverse order.
The argument is not modified.
\end{defun}
\begin{defun}[Function]
nreverse sequence
The result is a sequence containing the same elements as \emph{sequence}
but in reverse order. The argument may be destroyed and re-used to
produce the result. The result may or may not be \cdf{eq} to the
argument, so it is usually wise to say something like
\cd{(setq x (nreverse x))}, because simply \cd{(nreverse x)} is not
guaranteed to leave a reversed value in \cdf{x}.
\begin{newer}
X3J13 voted in March 1989 \issue{REMF-DESTRUCTION-UNSPECIFIED}
to clarify the permissible side effects of certain operations.
When the \emph{sequence} is a list,
\cdf{nreverse} is permitted to perform a \cdf{setf} on any part,
\emph{car} or \emph{cdr}, of the top-level list structure of that list.
When the \emph{sequence} is an array,
\cdf{nreverse} is permitted to re-order the elements of the given array
in order to produce the resulting array.
\end{newer}
\end{defun}
\begin{defun}[Function]
make-sequence type size &key :initial-element
This returns a sequence of type \emph{type} and of length \emph{size}, each of
whose elements
has been initialized to the \cd{:initial-element} argument.
If specified, the \cd{:initial-element} argument must be an object that
can be an element of a sequence of type \emph{type}.
For example:
\begin{lisp}
(make-sequence '(vector double-float) \\*
~~~~~~~~~~~~~~~100 \\*
~~~~~~~~~~~~~~~:initial-element 1d0)
\end{lisp}
If an \cd{:initial-element} argument is not specified, then the sequence will
be initialized in an implementation-dependent way.
\begin{new}
X3J13 voted in January 1989
\issue{ARGUMENTS-UNDERSPECIFIED}
to clarify that the \emph{type} argument
must be a type specifier, and the \emph{size} argument
must be a non-negative integer less than the value of
\cdf{array-dimension-limit}.
\end{new}
\begin{newer}
X3J13 voted in June 1989 \issue{SEQUENCE-TYPE-LENGTH} to specify that
\cdf{make-sequence} should signal an error if the sequence \emph{type} specifies the
number of elements and the \emph{size} argument is different.
\end{newer}
\begin{newer}
X3J13 voted in March 1989 \issue{CHARACTER-PROPOSAL}
to specify that if \emph{type} is \cdf{string}, the result is the same
as if \cdf{make-string} had been called with the same \emph{size}
and \cd{:initial-element} arguments.
\end{newer}
\end{defun}
\section{Concatenating, Mapping, and Reducing Sequences}
The functions in this section each operate on an arbitrary number of
sequences except for \cdf{reduce}, which is included here because
of its conceptual relationship to the mapping functions.
\begin{defun}[Function]
concatenate result-type &rest sequences
The result is a new sequence that contains all the elements of all the
sequences in order. All of the sequences are copied from; the result
does not share any structure with any of the argument sequences (in this
\cdf{concatenate} differs from \cdf{append}). The type of the result is
specified by \emph{result-type}, which must be a subtype of \cdf{sequence},
as for the function \cdf{coerce}.
It must be possible for every element of the argument sequences to be an
element of a sequence of type \emph{result-type}.
If only one \emph{sequence} argument is provided
and it has the type specified by \emph{result-type},
\cdf{concatenate} is required to copy the argument rather than simply
returning it. If a copy is not required, but only possibly type conversion,
then the \cdf{coerce} function may be appropriate.
\begin{newer}
X3J13 voted in June 1989 \issue{SEQUENCE-TYPE-LENGTH} to specify that
\cdf{concatenate} should signal an error if the sequence type specifies the
number of elements and the sum of the argument lengths is different.
\end{newer}
\end{defun}
\begin{defun}[Function]
map result-type function sequence &rest more-sequences
The \emph{function} must take as many arguments as there are sequences
provided; at least one sequence must be provided.
The result of \cdf{map} is a sequence such that element \emph{j} is the result
of applying \emph{function} to element \emph{j} of each of the argument
sequences. The result sequence is as long as the shortest of the
input sequences.
If the \emph{function} has side effects, it can count on being called
first on all the elements numbered \cd{0}, then on all those
numbered \cd{1}, and so on.
The type of the result sequence is specified by the argument \emph{result-type}
(which must be a subtype of the type \cdf{sequence}),
as for the function \cdf{coerce}.
In addition, one may specify {\nil} for the result type, meaning that no
result sequence is to be produced; in this case the \emph{function} is invoked
only for effect, and \cdf{map} returns {\nil}. This gives an effect similar
to that of \cdf{mapc}.
\begin{newer}
X3J13 voted in June 1989 \issue{SEQUENCE-TYPE-LENGTH} to specify that
\cdf{map} should signal an error if the sequence type specifies the number of
elements and the minimum of the argument lengths is different.
\end{newer}
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\noindent
For example:
\begin{lisp}
(map 'list \#'- '(1 2 3 4)) \EV\ (-1 -2 -3 -4) \\
(map 'string \\
~~~~~\#'(lambda (x) (if (oddp x) \#{\Xbackslash}1 \#{\Xbackslash}0)) \\
~~~~~'(1 2 3 4)) \\
~~~\EV\ "1010"
\end{lisp}
\end{defun}
\begin{defun}[Function]
map-into result-sequence function &rest sequences
Function \cdf{map-into} destructively modifies the \emph{result-sequence} to
contain the results of applying \emph{function} to corresponding elements of
the argument \emph{sequences} in turn; it then
returns \emph{result-sequence}.
The arguments \emph{result-sequence}
and each element of \emph{sequences} can each be
either a list or a vector (one-dimensional array).
The \emph{function} must accept at least as many arguments as the
number of argument \emph{sequences} supplied to \cdf{map-into}.
If \emph{result-sequence} and
the other argument \emph{sequences} are not all the same length, the iteration
terminates when the shortest sequence is exhausted. If \emph{result-sequence}
is a vector with a fill pointer, the fill pointer is ignored when
deciding how many iterations to perform, and afterwards the
fill pointer is set to the number of times the \emph{function} was applied.
If the \emph{function} has side effects, it can count on being called
first on all the elements numbered \cd{0}, then on all those
numbered \cd{1}, and so on.
If \emph{result-sequence} is longer than the shortest element of \emph{sequences},
extra elements at the end of \emph{result-sequence} are unchanged.
The function \cdf{map-into} differs from \cdf{map} in that it modifies
an existing sequence rather than creating a new one. In addition,
\cdf{map-into} can be called with only two arguments (\emph{result-sequence}
and \emph{function}), while \cdf{map} requires at least three arguments.
If \emph{result-sequence} is \cdf{nil}, \cdf{map-into} immediately returns
\cdf{nil}, because \cdf{nil} is a sequence of length zero.
\end{defun}
\begin{defun}[Function]
some predicate sequence &rest more-sequences \\
every predicate sequence &rest more-sequences \\
notany predicate sequence &rest more-sequences \\
notevery predicate sequence &rest more-sequences
These are all predicates.
The \emph{predicate} must take as many arguments as there are sequences
provided. The \emph{predicate} is first applied to the elements
with index \cd{0} in each of the sequences, and possibly then to
the elements with index \cd{1}, and so on, until a termination
criterion is met or the end of the shortest of the \emph{sequences} is reached.
If the \emph{predicate} has side effects, it can count on being called
first on all the elements numbered \cd{0}, then on all those
numbered \cd{1}, and so on.
\cdf{some} returns as soon as any invocation of \emph{predicate}
returns a non-{\false} value; \cdf{some} returns that value.
If the end of a sequence is reached, \cdf{some} returns {\false}.
Thus, considered as a predicate, it is true if \emph{some} invocation of
\emph{predicate} is true.
\cdf{every} returns {\false} as soon as any invocation of \emph{predicate}
returns {\false}.
If the end of a sequence is reached, \cdf{every} returns a non-{\false} value.
Thus, considered as a predicate, it is true if \emph{every} invocation of
\emph{predicate} is true.
\cdf{notany} returns {\false} as soon as any invocation of \emph{predicate}
returns a non-{\false} value.
If the end of a sequence is reached, \cdf{notany} returns a non-{\false} value.
Thus, considered as a predicate, it is true if \emph{no} invocation of
\emph{predicate} is true.
\cdf{notevery} returns a non-{\false} value as soon as any invocation
of \emph{predicate} returns {\false}. If the end of a sequence is reached,
\cdf{notevery} returns
{\false}. Thus, considered as a predicate, it is true if \emph{not every} invocation of
\emph{predicate} is true.
\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]
reduce function sequence &key :from-end :start :end :initial-value
The \cdf{reduce} function combines all the elements of a sequence using
a binary operation; for example, using \cdf{+} one can add up all the
elements.
The specified subsequence of the \emph{sequence} is combined
or ``reduced'' using the
\emph{function}, which must accept two arguments. The reduction is
left-associative, unless the \cd{:from-end} argument is true (it defaults
to {\nil}), in which case it is right-associative. If an
\cd{:initial-value} argument is given, it is logically placed before the
subsequence (after it if \cd{:from-end} is true) and included in the
reduction operation.
If the specified subsequence contains exactly one element
and the keyword argument \cd{:initial-value}
is not given, then that element
is returned and the \emph{function} is not called.
If the specified subsequence is empty and an \cd{:initial-value} is given,
then the \cd{:initial-value} is returned
and the \emph{function} is not called.
If the specified subsequence is empty and no \cd{:initial-value} is given,
then the \emph{function} is called with zero
arguments, and \cdf{reduce} returns whatever the function does. (This is
the only case where the \emph{function} is called with other than two
arguments.)
\begin{lisp}
(reduce \#'+ '(1 2 3 4)) \EV\ 10 \\
(reduce \#'- '(1 2 3 4)) \EQ\ (- (- (- 1 2) 3) 4) \EV\ -8 \\
(reduce \#'- '(1 2 3 4) :from-end t)~~~~~;\textrm{Alternating sum} \\
~~~\EQ\ (- 1 (- 2 (- 3 4))) \EV\ -2 \\
(reduce \#'+ '()) \EV\ 0 \\
(reduce \#'+ '(3)) \EV\ 3 \\
(reduce \#'+ '(foo)) \EV\ foo \\
(reduce \#'list '(1 2 3 4)) \EV\ (((1 2) 3) 4) \\
(reduce \#'list '(1 2 3 4) :from-end t) \EV\ (1 (2 (3 4))) \\
(reduce \#'list '(1 2 3 4) :initial-value 'foo) \\
~~~\EV\ ((((foo 1) 2) 3) 4) \\
(reduce \#'list '(1 2 3 4) \\
~~~~~~~~:from-end t :initial-value 'foo) \\
~~~\EV\ (1 (2 (3 (4 foo))))
\end{lisp}
If the \emph{function} produces side effects, the order of the calls
to the \emph{function} can be correctly predicted from the reduction ordering
demonstrated above.
The name ``reduce'' for this function is borrowed from {APL}.
\begin{new}
X3J13 voted in March 1988 \issue{REDUCE-ARGUMENT-EXTRACTION}
to extend the \cdf{reduce} function to take
an additional keyword argument named \cd{:key}. As usual, this argument
defaults to the identity function. The value of this argument must be
a function that accepts at least one argument. This function is applied once
to each element of the
sequence that is to participate in the reduction operation, in the order
implied by the \cd{:from-end} argument; the values returned by this
function are combined by the reduction \emph{function}.
However, the \cd{:key} function is \emph{not} applied
to the \cd{:initial-value} argument (if any).
\end{new}
\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{Modifying Sequences}
Each of these functions alters the contents of a sequence or produces
an altered copy of a given sequence.
\begin{defun}[Function]
fill sequence item &key :start :end
The \emph{sequence} is destructively modified by replacing each element of
the subsequence specified by the \cd{:start} and \cd{:end} parameters
with the \emph{item}. The \emph{item} may be any Lisp object but must be a
suitable element for the \emph{sequence}. The \emph{item} is stored into all
specified components of the \emph{sequence}, beginning at the one specified
by the \cd{:start} index (which defaults to zero), up to but not
including the one specified by the \cd{:end} index (which defaults to the
length of the sequence). \cdf{fill} returns the modified \emph{sequence}.
For example:
\begin{lisp}
(setq x (vector 'a 'b 'c 'd 'e)) \EV\ \#(a b c d e) \\
(fill x 'z \cd{:start} 1 \cd{:end} 3) \EV\ \#(a z z d e) \\
~~\textrm{and now} x \EV\ \#(a z z d e) \\
(fill x 'p) \EV\ \#(p p p p p) \\
~~\textrm{and now} x \EV\ \#(p p p p p)
\end{lisp}
\end{defun}
\begin{defun}[Function]
replace sequence1 sequence2 &key :start1 :end1 :start2~:end2
The sequence \emph{sequence1} is destructively modified by copying successive
elements into it from \emph{sequence2}. The elements of
\emph{sequence2} must be of a type that may be stored into
\emph{sequence1}. The subsequence of \emph{sequence2}
specified by \cd{:start2} and \cd{:end2} is copied into the
subsequence of \emph{sequence1} specified by \cd{:start1} and \cd{:end1}.
(The arguments \cd{:start1} and \cd{:start2} default to zero.
The arguments \cd{:end1} and \cd{:end2} default
to {\false}, meaning the end of the appropriate sequence.)
If these subsequences are not of the same length, then
the shorter length determines how many elements are copied; the extra
elements near the end of the longer subsequence are not involved in the
operation.
The number of elements copied may be expressed as:
\begin{lisp}
(min (- \emph{end1} \emph{start1}) (- \emph{end2} \emph{start2}))
\end{lisp}
The value returned by \cdf{replace} is the modified \emph{sequence1}.
If \emph{sequence1} and \emph{sequence2} are the same (\cdf{eq}) object
and the region being modified overlaps the region being copied
from, then it is as if the entire source region were copied to another
place and only then copied back into the target region.
However, if \emph{sequence1} and \emph{sequence2} are \emph{not} the same,
but the region being modified overlaps the region being copied from
(perhaps because of shared list structure or displaced arrays),
then after the \cdf{replace} operation
the subsequence of \emph{sequence1} being modified will have
unpredictable contents.
\end{defun}
\begin{defun}[Function]
remove item sequence &key :from-end :test :test-not :start :end :count :key \\
remove-if predicate sequence &key :from-end :start :end :count :key \\
remove-if-not predicate sequence &key :from-end :start :end :count :key
The result is a sequence of the same kind as the argument \emph{sequence}
that has the same elements except that those in the subsequence
delimited by \cd{:start} and \cd{:end} and satisfying the test (see
above) have been removed. This is a non-destructive operation; the result
is a copy of the input \emph{sequence}, save that some elements are not
copied. Elements not removed occur in the same order in the result
as they did in the argument.
The \cd{:count} argument, if supplied, limits the number of elements
removed; if more than \cd{:count} elements satisfy the test,
then of these elements only the leftmost are removed,
as many as specified by \cd{:count}.
\begin{new}
X3J13 voted in January 1989
\issue{RANGE-OF-COUNT-KEYWORD}
to clarify that the \cd{:count} argument must be either \cdf{nil}
or an integer, and that supplying a negative integer produces the
same behavior as supplying zero.
\end{new}
A non-{\false} \cd{:from-end} specification
matters only when the \cd{:count} argument
is provided; in that case only the rightmost \cd{:count} elements satisfying
the test are removed.
For example:
\begin{lisp}
(remove 4 '(1 2 4 1 3 4 5)) \EV\ (1 2 1 3 5) \\
(remove 4 '(1 2 4 1 3 4 5) \cd{:count} 1) \EV\ (1 2 1 3 4 5) \\
(remove 4 '(1 2 4 1 3 4 5) \cd{:count} 1 \cd{:from-end} t) \\
~~~\EV\ (1 2 4 1 3 5) \\
(remove 3 '(1 2 4 1 3 4 5) \cd{:test} \#'>) \EV\ (4 3 4 5) \\
(remove-if \#'oddp '(1 2 4 1 3 4 5)) \EV\ (2 4 4) \\
(remove-if \#'evenp '(1 2 4 1 3 4 5) \cd{:count} 1 \cd{:from-end} t) \\
~~~\EV\ (1 2 4 1 3 5)
\end{lisp}
The result of \cdf{remove} may share
with the argument \emph{sequence}; a list result may share a tail
with an input list, and the result may be \cdf{eq} to the input \emph{sequence}
if no elements need to be removed.
\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]
delete item sequence &key :from-end :test :test-not :start~:end~:count~:key \\
delete-if predicate sequence &key :from-end :start~:end~:count~:key \\
delete-if-not predicate sequence &key :from-end :start~:end~:count~:key
This is the destructive counterpart to \cdf{remove}.
The result is a sequence of the same kind as the argument \emph{sequence}
that has the same elements except that those in the subsequence
delimited by \cd{:start} and \cd{:end} and satisfying the test (see
above) have been deleted. This is a destructive operation.
The argument \emph{sequence} may be destroyed and used to construct
the result; however, the result may or may not be \cdf{eq} to \emph{sequence}.
Elements not deleted occur in the same order in the result
as they did in the argument.
The \cd{:count} argument, if supplied, limits the number of elements
deleted; if more than \cd{:count} elements satisfy the test,
then of these elements only the leftmost are deleted,
as many as specified by \cd{:count}.
\begin{new}
X3J13 voted in January 1989
\issue{RANGE-OF-COUNT-KEYWORD}
to clarify that the \cd{:count} argument must be either \cdf{nil}
or an integer, and that supplying a negative integer produces the
same behavior as supplying zero.
\end{new}
A non-{\false} \cd{:from-end} specification
matters only when the \cd{:count} argument
is provided; in that case only the rightmost \cd{:count} elements satisfying
the test are deleted.
For example:
\begin{lisp}
(delete 4 '(1 2 4 1 3 4 5)) \EV\ (1 2 1 3 5) \\
(delete 4 '(1 2 4 1 3 4 5) \cd{:count} 1) \EV\ (1 2 1 3 4 5) \\
(delete 4 '(1 2 4 1 3 4 5) \cd{:count} 1 \cd{:from-end} t) \\
~~~\EV\ (1 2 4 1 3 5) \\
(delete 3 '(1 2 4 1 3 4 5) \cd{:test} \#'>) \EV\ (4 3 4 5) \\
(delete-if \#'oddp '(1 2 4 1 3 4 5)) \EV\ (2 4 4) \\
(delete-if \#'evenp '(1 2 4 1 3 4 5) \cd{:count} 1 \cd{:from-end} t) \\
~~~\EV\ (1 2 4 1 3 5)
\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}
\begin{newer}
X3J13 voted in March 1989 \issue{REMF-DESTRUCTION-UNSPECIFIED}
to clarify the permissible side effects of certain operations.
When the \emph{sequence} is a list,
\cdf{delete} is permitted to perform a \cdf{setf} on any part,
\emph{car} or \emph{cdr}, of the top-level list structure of that list.
When the \emph{sequence} is an array,
\cdf{delete} is permitted to alter the dimensions of the given array
and to slide some of its elements into new positions without permuting them
in order to produce the resulting array.
Furthermore, \cd{(delete-if \emph{predicate} \emph{sequence}~...)}
is required to behave exactly like
\begin{lisp}
(delete nil \emph{sequence} \\*
~~~~~~~~:test \#'(lambda (unused item) \\*
~~~~~~~~~~~~~~~~~~~(declare (ignore unused)) \\*
~~~~~~~~~~~~~~~~~~~(funcall \emph{predicate} item)) \\*
~~~~~~~~...)
\end{lisp}
\end{newer}
\end{defun}
\begin{defun}[Function]
remove-duplicates sequence &key :from-end :test :test-not :start :end :key \\
delete-duplicates sequence &key :from-end :test :test-not :start :end :key
The elements of \emph{sequence} are compared pairwise, and if any two match,
then the one occurring earlier in the sequence
is discarded (but if the \cd{:from-end} argument is true, then the one
later in the sequence is discarded).
The result is a sequence of the same kind as the
argument sequence with enough elements removed so that no two of the remaining
elements match. The order of the elements remaining in the result
is the same as the order in which they appear in \emph{sequence}.
\cdf{remove-duplicates} is the non-destructive version
of this operation.
The result of \cdf{remove-duplicates} may share
with the argument \emph{sequence}; a list result may share a tail
with an input list, and the result may be \cdf{eq} to the input \emph{sequence}
if no elements need to be removed.
\cdf{delete-duplicates} may destroy the argument \emph{sequence}.
Some examples:
\begin{lisp}
(remove-duplicates '(a b c b d d e)) \EV\ (a c b d e) \\
(remove-duplicates '(a b c b d d e) \cd{:from-end} t) \EV\ (a b c d e) \\
(remove-duplicates '((foo \#{\Xbackslash}a) (bar \#{\Xbackslash}\%) (baz \#{\Xbackslash}A)) \\
~~~~~~~~~~~~~~~~~~~\cd{:test} \#'char-equal \cd{:key} \#'cadr) \\
~~~\EV\ ((bar \#{\Xbackslash}\%) (baz \#{\Xbackslash}A)) \\
(remove-duplicates '((foo \#{\Xbackslash}a) (bar \#{\Xbackslash}\%) (baz \#{\Xbackslash}A)) \\
~~~~~~~~~~~~~~~~~~~\cd{:test} \#'char-equal \cd{:key} \#'cadr \cd{:from-end} t) \\
~~~\EV\ ((foo \#{\Xbackslash}a) (bar \#{\Xbackslash}\%))
\end{lisp}
These functions are useful for converting a sequence into a canonical
form suitable for representing a set.
\begin{new}
X3J13 voted in January 1989
\issue{MAPPING-DESTRUCTIVE-INTERACTION}
to restrict user side effects; see section~\ref{STRUCTURE-TRAVERSAL-SECTION}.
\end{new}
\begin{newer}
X3J13 voted in March 1989 \issue{REMF-DESTRUCTION-UNSPECIFIED}
to clarify the permissible side effects of certain operations.
When the \emph{sequence} is a list,
\cdf{delete-duplicates} is permitted to perform a \cdf{setf} on any part,
\emph{car} or \emph{cdr}, of the top-level list structure of that list.
When the \emph{sequence} is an array,
\cdf{delete-duplicates} is permitted to alter the dimensions of the given array
and to slide some of its elements into new positions without permuting them
in order to produce the resulting array.
\end{newer}
\end{defun}
\begin{defun}[Function]
substitute newitem olditem sequence &key :from-end :test :test-not :start :end :count :key \\
substitute-if newitem test sequence &key :from-end :start~:end :count :key \\
substitute-if-not newitem test sequence &key :from-end :start :end :count :key
The result is a sequence of the same kind as the argument \emph{sequence}
that has the same elements except that those in the subsequence
delimited by \cd{:start} and \cd{:end} and satisfying the test (see
above) have been replaced by \emph{newitem}. This is a non-destructive
operation; the result is a copy of the input \emph{sequence}, save that some
elements are changed.
The \cd{:count} argument, if supplied, limits the number of elements
altered; if more than \cd{:count} elements satisfy the test,
then of these elements only the leftmost are replaced,
as many as specified by \cd{:count}.
\begin{new}
X3J13 voted in January 1989
\issue{RANGE-OF-COUNT-KEYWORD}
to clarify that the \cd{:count} argument must be either \cdf{nil}
or an integer, and that supplying a negative integer produces the
same behavior as supplying zero.
\end{new}
A non-{\false} \cd{:from-end} specification
matters only when the \cd{:count} argument
is provided; in that case only the rightmost \cd{:count} elements satisfying
the test are replaced.
For example:
\begin{lisp}
(substitute 9 4 '(1 2 4 1 3 4 5)) \EV\ (1 2 9 1 3 9 5) \\
(substitute 9 4 '(1 2 4 1 3 4 5) \cd{:count} 1) \EV\ (1 2 9 1 3 4 5) \\
(substitute 9 4 '(1 2 4 1 3 4 5) \cd{:count} 1 \cd{:from-end} t) \\
~~~\EV\ (1 2 4 1 3 9 5) \\
(substitute 9 3 '(1 2 4 1 3 4 5) \cd{:test} \#'>) \EV\ (9 9 4 9 3 4 5) \\
(substitute-if 9 \#'oddp '(1 2 4 1 3 4 5)) \EV\ (9 2 4 9 9 4 9) \\
(substitute-if 9 \#'evenp '(1 2 4 1 3 4 5) \cd{:count} 1 \cd{:from-end} t) \\
~~~\EV\ (1 2 4 1 3 9 5)
\end{lisp}
The result of \cdf{substitute} may share
with the argument \emph{sequence}; a list result may share a tail
with an input list, and the result may be \cdf{eq} to the input \emph{sequence}
if no elements need to be changed.
See also \cdf{subst}, which performs substitutions throughout a 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}
\begin{defun}[Function]
nsubstitute newitem olditem sequence &key :from-end :test :test-not :start :end :count :key \\
nsubstitute-if newitem test sequence &key :from-end :start~:end :count :key \\
nsubstitute-if-not newitem test sequence &key :from-end :start :end :count :key
This is the destructive counterpart to \cdf{substitute}.
The result is a sequence of the same kind as the argument \emph{sequence}
that has the same elements except that those in the subsequence
delimited by \cd{:start} and \cd{:end} and satisfying the test (see
above) have been replaced by \emph{newitem}. This is a destructive operation.
The argument \emph{sequence} may be destroyed and used to construct
the result; however, the result may or may not be \cdf{eq} to \emph{sequence}.
See also \cdf{nsubst}, which performs destructive
substitutions throughout a 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}
\begin{newer}
X3J13 voted in March 1989 \issue{REMF-DESTRUCTION-UNSPECIFIED}
to clarify the permissible side effects of certain operations.
When the \emph{sequence} is a list,
\cdf{nsubstitute} or \cdf{nsubstitute-if}
is required to perform a \cdf{setf} on any
\emph{car} of the top-level list structure of that list
whose old contents must be replaced with \emph{newitem}
but is forbidden to perform a \cdf{setf} on any \cdf{cdr} of the list.
When the \emph{sequence} is an array,
\cdf{nsubstitute} or \cdf{nsubstitute-if}
is required to perform a \cdf{setf} on any element of the array
whose old contents must be replaced with \emph{newitem}.
These functions, therefore, may successfully be
used solely for effect, the caller discarding the returned value
(though some programmers find this stylistically distasteful).
\end{newer}
\end{defun}
\section{Searching Sequences for Items}
Each of these functions searches a sequence to locate one or more
elements satisfying some test.
\begin{defun}[Function]
find item sequence &key :from-end :test :test-not :start~:end :key \\
find-if predicate sequence &key :from-end :start :end :key \\
find-if-not predicate sequence &key :from-end :start~:end~:key
If the \emph{sequence} contains an element satisfying the test,
then the leftmost such element
is returned; otherwise {\false} is returned.
If \cd{:start} and \cd{:end} keyword arguments are given,
only the specified subsequence of \emph{sequence} is searched.
If a non-{\false} \cd{:from-end} keyword argument is specified, then the result is
the \emph{rightmost} element satisfying 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]
position item sequence &key :from-end :test :test-not :start~:end~:key \\
position-if predicate sequence &key :from-end :start~:end~:key \\
position-if-not predicate sequence &key :from-end :start~:end~:key
If the \emph{sequence} contains an element satisfying the test,
then the index within the sequence of the leftmost such element
is returned as a non-negative integer; otherwise {\false} is returned.
If \cd{:start} and \cd{:end} keyword arguments are given,
only the specified subsequence of \emph{sequence} is searched.
However, the index returned is relative to the entire sequence,
not to the subsequence.
If a non-{\false} \cd{:from-end} keyword argument is specified, then the result is
the index of the \emph{rightmost} element satisfying the test. (The index
returned, however, is an index from the left-hand end, as usual.)