| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Common Lisp defines a number of functions that operate on
sequences, which are either lists, strings, or vectors.
Emacs Lisp includes a few of these, notably elt and
length; this package defines most of the rest.
| 9.1 Sequence Basics | Arguments shared by all sequence functions | |
| 9.2 Mapping over Sequences | ‘mapcar*’, ‘mapcan’, ‘map’, ‘every’, etc. | |
| 9.3 Sequence Functions | ‘subseq’, ‘remove*’, ‘substitute’, etc. | |
| 9.4 Searching Sequences | ‘find’, ‘position’, ‘count’, ‘search’, etc. | |
| 9.5 Sorting Sequences | ‘sort*’, ‘stable-sort’, ‘merge’ |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Many of the sequence functions take keyword arguments; see section Argument Lists. All keyword arguments are optional and, if specified, may appear in any order.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions “map” the function you specify over the elements
of lists or arrays. They are all variations on the theme of the
built-in function mapcar.
This function calls function on successive parallel sets of
elements from its argument sequences. Given a single seq
argument it is equivalent to mapcar; given n sequences,
it calls the function with the first elements of each of the sequences
as the n arguments to yield the first element of the result
list, then with the second elements, and so on. The mapping stops as
soon as the shortest sequence runs out. The argument sequences may
be any mixture of lists, strings, and vectors; the return sequence
is always a list.
Common Lisp’s mapcar accepts multiple arguments but works
only on lists; Emacs Lisp’s mapcar accepts a single sequence
argument. This package’s mapcar* works as a compatible
superset of both.
This function maps function over the argument sequences,
just like mapcar*, but it returns a sequence of type
result-type rather than a list. result-type must
be one of the following symbols: vector, string,
list (in which case the effect is the same as for
mapcar*), or nil (in which case the results are
thrown away and map returns nil).
This function calls function on each of its argument lists,
then on the cdrs of those lists, and so on, until the
shortest list runs out. The results are returned in the form
of a list. Thus, maplist is like mapcar* except
that it passes in the list pointers themselves rather than the
cars of the advancing pointers.
This function is like mapcar*, except that the values
returned by function are ignored and thrown away rather
than being collected into a list. The return value of mapc
is seq, the first sequence.
This function is like maplist, except that it throws away
the values returned by function.
This function is like mapcar*, except that it concatenates
the return values (which must be lists) using nconc,
rather than simply collecting them into a list.
This function is like maplist, except that it concatenates
the return values using nconc.
This function calls predicate on each element of seq
in turn; if predicate returns a non-nil value,
some returns that value, otherwise it returns nil.
Given several sequence arguments, it steps through the sequences
in parallel until the shortest one runs out, just as in
mapcar*. You can rely on the left-to-right order in which
the elements are visited, and on the fact that mapping stops
immediately as soon as predicate returns non-nil.
This function calls predicate on each element of the sequence(s)
in turn; it returns nil as soon as predicate returns
nil for any element, or t if the predicate was true
for all elements.
This function calls predicate on each element of the sequence(s)
in turn; it returns nil as soon as predicate returns
a non-nil value for any element, or t if the predicate
was nil for all elements.
This function calls predicate on each element of the sequence(s)
in turn; it returns a non-nil value as soon as predicate
returns nil for any element, or t if the predicate was
true for all elements.
This function combines the elements of seq using an associative
binary operation. Suppose function is * and seq is
the list (2 3 4 5). The first two elements of the list are
combined with (* 2 3) = 6; this is combined with the next
element, (* 6 4) = 24, and that is combined with the final
element: (* 24 5) = 120. Note that the * function happens
to be self-reducing, so that (* 2 3 4 5) has the same effect as
an explicit call to reduce.
If :from-end is true, the reduction is right-associative instead
of left-associative:
(reduce '- '(1 2 3 4))
≡ (- (- (- 1 2) 3) 4) ⇒ -8
(reduce '- '(1 2 3 4) :from-end t)
≡ (- 1 (- 2 (- 3 4))) ⇒ -2
|
If :key is specified, it is a function of one argument which
is called on each of the sequence elements in turn.
If :initial-value is specified, it is effectively added to the
front (or rear in the case of :from-end) of the sequence.
The :key function is not applied to the initial value.
If the sequence, including the initial value, has exactly one element then that element is returned without ever calling function. If the sequence is empty (and there is no initial value), then function is called with no arguments to obtain the return value.
All of these mapping operations can be expressed conveniently in
terms of the loop macro. In compiled code, loop will
be faster since it generates the loop as in-line code with no
function calls.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes a number of Common Lisp functions for operating on sequences.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions search for elements or subsequences in a sequence.
(See also member* and assoc*; see section Lists.)
This function searches seq for an element matching item.
If it finds a match, it returns the matching element. Otherwise,
it returns nil. It returns the leftmost match, unless
:from-end is true, in which case it returns the rightmost
match. The :start and :end arguments may be used to
limit the range of elements that are searched.
This function is like find, except that it returns the
integer position in the sequence of the matching item rather than
the item itself. The position is relative to the start of the
sequence as a whole, even if :start is non-zero. The function
returns nil if no matching element was found.
This function returns the number of elements of seq which match item. The result is always a nonnegative integer.
The find-if, find-if-not, position-if,
position-if-not, count-if, and count-if-not
functions are defined similarly.
This function compares the specified parts of seq1 and
seq2. If they are the same length and the corresponding
elements match (according to :test, :test-not,
and :key), the function returns nil. If there is
a mismatch, the function returns the index (relative to seq1)
of the first mismatching element. This will be the leftmost pair of
elements which do not match, or the position at which the shorter of
the two otherwise-matching sequences runs out.
If :from-end is true, then the elements are compared from right
to left starting at (1- end1) and (1- end2).
If the sequences differ, then one plus the index of the rightmost
difference (relative to seq1) is returned.
An interesting example is (mismatch str1 str2 :key 'upcase),
which compares two strings case-insensitively.
This function searches seq2 for a subsequence that matches
seq1 (or part of it specified by :start1 and
:end1.) Only matches which fall entirely within the region
defined by :start2 and :end2 will be considered.
The return value is the index of the leftmost element of the
leftmost match, relative to the start of seq2, or nil
if no matches were found. If :from-end is true, the
function finds the rightmost matching subsequence.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function sorts seq into increasing order as determined
by using predicate to compare pairs of elements. predicate
should return true (non-nil) if and only if its first argument
is less than (not equal to) its second argument. For example,
< and string-lessp are suitable predicate functions
for sorting numbers and strings, respectively; > would sort
numbers into decreasing rather than increasing order.
This function differs from Emacs’ built-in sort in that it
can operate on any type of sequence, not just lists. Also, it
accepts a :key argument which is used to preprocess data
fed to the predicate function. For example,
(setq data (sort data 'string-lessp :key 'downcase)) |
sorts data, a sequence of strings, into increasing alphabetical
order without regard to case. A :key function of car
would be useful for sorting association lists.
The sort* function is destructive; it sorts lists by actually
rearranging the cdr pointers in suitable fashion.
This function sorts seq stably, meaning two elements which are equal in terms of predicate are guaranteed not to be rearranged out of their original order by the sort.
In practice, sort* and stable-sort are equivalent
in Emacs Lisp because the underlying sort function is
stable by default. However, this package reserves the right to
use non-stable methods for sort* in the future.
This function merges two sequences seq1 and seq2 by
interleaving their elements. The result sequence, of type type
(in the sense of concatenate), has length equal to the sum
of the lengths of the two input sequences. The sequences may be
modified destructively. Order of elements within seq1 and
seq2 is preserved in the interleaving; elements of the two
sequences are compared by predicate (in the sense of
sort) and the lesser element goes first in the result.
When elements are equal, those from seq1 precede those from
seq2 in the result. Thus, if seq1 and seq2 are
both sorted according to predicate, then the result will be
a merged sequence which is (stably) sorted according to
predicate.
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on September 25, 2026 using texi2html 1.82.