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.
Produces 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
beginning(a synonym for
end. The symbol may be quoted, but need not be. The default is
end. For example,(iter (for i from 1 to 5) (collect i))
(1 2 3 4 5), whereas(iter (for i from 1 to 5) (collect i at beginning))
(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 is
list. Specifying a type other than
listwill result in
collectreturning a sequence of that type. However, the type of the sequence being constructed when inside the loop body is undefined when a non-
listtype is specified. (As with place, quoting type is optional.)
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 with
These are like
collect, but behave like the Common Lisp functions
nunion. As in Common Lisp, they work only on lists. Also as in Common Lisp,
nunioningassume that the value of expr contains no duplicates.
This 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,
The differences between
reducingare slight. One difference is that the functions take their arguments in a different order. Another is that in the absence of init-val,
reducingwill generate different code that avoids any dependence on the initial value. The reason for having both clauses is that one usually thinks of reductions (like
sum) and accumulations (like
collect) as different beasts.