argument, CONCATENATE
must be told explicitly what kind of sequence to produce in case the arguments are of different types. Its first argument is a type descriptor, like the :element-type
argument to MAKE-ARRAY
. In this case, the type descriptors you'll most likely use are the symbols VECTOR
, LIST
, or STRING
.[127] For example:
(concatenate 'vector #(1 2 3) '(4 5 6)) ==> #(1 2 3 4 5 6)
(concatenate 'list #(1 2 3) '(4 5 6)) ==> (1 2 3 4 5 6)
(concatenate 'string 'abc' '(#d #e #f)) ==> 'abcdef'
The functions SORT
and STABLE- SORT
provide two ways of sorting a sequence. They both take a sequence and a two-argument predicate and return a sorted version of the sequence.
(sort (vector 'foo' 'bar' 'baz') #'string<) ==> #('bar' 'baz' 'foo')
The difference is that STABLE-SORT
is guaranteed to not reorder any elements considered equivalent by the predicate while SORT
guarantees only that the result is sorted and may reorder equivalent elements.
Both these functions are examples of what are called
Typically you won't care about the unsorted version of a sequence after you've sorted it, so it makes sense to allow SORT
and STABLE-SORT
to destroy the sequence in the course of sorting it. But it does mean you need to remember to write the following:[128]
(setf my-sequence (sort my-sequence #'string<))
rather than just this:
(sort my-sequence #'string<)
Both these functions also take a keyword argument, :key
, which, like the :key
argument in other sequence functions, should be a function and will be used to extract the values to be passed to the sorting predicate in the place of the actual elements. The extracted keys are used only to determine the ordering of elements; the sequence returned will contain the actual elements of the argument sequence.
The MERGE
function takes two sequences and a predicate and returns a sequence produced by merging the two sequences, according to the predicate. It's related to the two sorting functions in that if each sequence is already sorted by the same predicate, then the sequence returned by MERGE
will also be sorted. Like the sorting functions, MERGE
takes a :key
argument. Like CONCATENATE
, and for the same reason, the first argument to MERGE
must be a type descriptor specifying the type of sequence to produce.
(merge 'vector #(1 3 5) #(2 4 6) #'<) ==> #(1 2 3 4 5 6)
(merge 'list #(1 3 5) #(2 4 6) #'<) ==> (1 2 3 4 5 6)
Another set of functions allows you to manipulate subsequences of existing sequences. The most basic of these is SUBSEQ
, which extracts a subsequence starting at a particular index and continuing to a particular ending index or the end of the sequence. For instance:
(subseq 'foobarbaz' 3) ==> 'barbaz'
(subseq 'foobarbaz' 3 6) ==> 'bar'
SUBSEQ
is also SETF
able, but it won't extend or shrink a sequence; if the new value and the subsequence to be replaced are different lengths, the shorter of the two determines how many characters are actually changed.
(defparameter *x* (copy-seq 'foobarbaz'))
(setf (subseq *x* 3 6) 'xxx') ; subsequence and new value are same length
*x* ==> 'fooxxxbaz'
(setf (subseq *x* 3 6) 'abcd') ; new value too long, extra character ignored.
*x* ==> 'fooabcbaz'
(setf (subseq *x* 3 6) 'xx') ; new value too short, only two characters changed
*x* ==> 'fooxxcbaz'
You can use the FILL
function to set multiple elements of a sequence to a single value. The required arguments are a sequence and the value with which to fill it. By default every element of the sequence is set to the value; :start
and :end
keyword arguments can limit the effects to a given subsequence.
If you need to find a subsequence within a sequence, the SEARCH
function works like POSITION
except the first argument is a sequence rather than a single item.
(position # 'foobarbaz') ==> 3
(search 'bar' 'foobarbaz') ==> 3
On the other hand, to find where two sequences with a common prefix first diverge, you can use the MISMATCH
function. It takes two sequences and returns the index of the first pair of mismatched elements.
(mismatch 'foobarbaz' 'foom') ==> 3
It returns NIL
if the strings match. MISMATCH
also takes many of the standard keyword arguments: a :key
argument for specifying a function to use to extract the values to be compared; a :test
argument to specify the comparison function; and :start1
, :end1
, :start2
, and :end2
arguments to specify subsequences within the two sequences. And a :from-end
argument of T
specifies the sequences should be searched in reverse order, causing MISMATCH
to return the index, in the first sequence, where whatever common suffix the two sequences share begins.
(mismatch 'foobar' 'bar' :from-end t) ==> 3