Next: , Up: Value Accumulation   [Contents][Index]


15.25.8.1 collect - collecting

collect expr [into var]
collecting expr [into var]

During each iteration, these constructs collect the value of the specified expression into a list.

var

is set to the list of collected values. When iteration terminates, the list is returned, except if var is specified, the loop does not return the final list automatically.

If var is not specified, it is equivalent to specifying an internal name for var and returning its value in a ‘finally’ clause.

The var argument is bound as if by the construct ‘with’.

You cannot specify a data type for var; it must be of type ‘list’.

Examples

;;; Collect all the symbols in a list.
(loop for i in '(bird 3 4 turtle (1 . 4) horse cat)
      when (symbolp i) collect i)
;;; Collect and return odd numbers.
(loop for i from 1 to 10n
      if (oddp i) collect i)
;;; Collect items into local variable, but don't return them.
(loop for i in '(a b c d) by #'cddr
      collect i into my-list
      finally (print my-list)) ;Prints 1 line