Sequence Basics
Sequences
Sequence Functions
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
.
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.
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
).
cdr
s 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
car
s of the advancing pointers.
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.
maplist
, except that it throws away
the values returned by function.
mapcar*
, except that it concatenates
the return values (which must be lists) using nconc
,
rather than simply collecting them into a list.
maplist
, except that it concatenates
the return values using nconc
.
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
.
nil
as soon as predicate returns
nil
for any element, or t
if the predicate was true
for all elements.
nil
as soon as predicate returns
a non-nil
value for any element, or t
if the predicate
was nil
for all elements.
nil
value as soon as predicate
returns nil
for any element, or t
if the predicate was
true for all elements.
&key :from-end :start :end :initial-value :key
*
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.