Next: Finders, Previous: Reductions, Up: Gathering Clauses
All the predefined accumulation clauses add values to a sequence. If the sequence is a list, they all have the property that the partial list is kept in the correct order and available for inspection at any point in the loop.
&optional
into
varat
placeresult-type
typeProduces a sequence of the values of exptr on each iteration. place indicates where the next value of exptr is added to the list and may be one of the symbols
start
,beginning
(a synonym forstart
) orend
. The symbol may be quoted, but need not be. The default isend
. For example,(iter (for i from 1 to 5) (collect i))produces
(1 2 3 4 5)
, whereas(iter (for i from 1 to 5) (collect i at beginning))produces
(5 4 3 2 1)
(and is likely to be faster in most Common Lisp implementations).If type is provided, it should be a subtype of
sequence
. The default islist
. Specifying a type other thanlist
will result incollect
returning a sequence of that type. However, the type of the sequence being constructed when inside the loop body is undefined when a non-list
type is specified. (As with place, quoting type is optional.)
&optional
into
var test
test at
place result-type
typeLike
collect
, but only adds the value of exptr if it is not already present. test, which defaults to#'eql
, is the test to be used withmember
.
&optional
into
var at
place&optional
into
var at
place&optional
into
var test
test at
place&optional
into
var test
test at
placeThese are like
collect
, but behave like the Common Lisp functionsappend
,nconc
,union
ornunion
. As in Common Lisp, they work only on lists. Also as in Common Lisp,unioning
andnunioning
assume that the value of expr contains no duplicates.
by
func &optional
initial-value
init-val into
varThis is a general-purpose accumulation clause. func should be a function of two arguments, the value of expr and the value accumulated so far in the iteration, and it should return the updated value. If no initial value is supplied,
nil
is used.The differences between
accumulate
andreducing
are slight. One difference is that the functions take their arguments in a different order. Another is that in the absence of init-val,accumulate
will usenil
, whereasreducing
will generate different code that avoids any dependence on the initial value. The reason for having both clauses is that one usually thinks of reductions (likesum
) and accumulations (likecollect
) as different beasts.