2.4 Value accumulation

# 2.4.2 Reference pages

collect Value Accumulation Clause Keyword

collecting Value Accumulation Clause Keyword

Syntax:`collect`[`ing`] expr [`into` var]

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

The argument var is set to the list of collected values; 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`.

```;; Collect all the symbols in a list.
> (loop for i in '(bird 3 4 turtle (1 . 4) horse cat)
when (symbolp i) collect i)
(BIRD TURTLE HORSE CAT)

;; Collect and return odd numbers.
> (loop for i from 1 to 10
if (oddp i) collect i)
(1 3 5 7 9)

;; 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))
(A C)
NIL

```
See Also: `nconc`, `append`

append Value Accumulation Clause Keyword

appending Value Accumulation Clause Keyword

nconc Value Accumulation Clause Keyword

nconcing Value Accumulation Clause Keyword

Syntax:`append`[`ing`] expr [`into` var]

Syntax:`nconc`[`ing`] expr [`into` var]

These constructs are similar to`collect` except that the values of the specified expression must be lists.

• The`append` keyword causes its list values to be concatenated into a single list, as if they were arguments to the Common Lisp function`append`.

• The`nconc` keyword causes its lists values to be concatenated into a single list, as if they were arguments to the Common Lisp function`nconc`.

The argument var is set to the list of concatenated values; if you specify var, the loop does not return the final list automatically. 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`.

Note:`nconc` destructively modifies its argument lists.

```;; Use APPEND to concatenate some sublists.
>  (loop for x in '((a) (b) ((c)))
append x)
(A B (C))

;; NCONC some sublists together.  Note that only lists made by the
;; call to LIST are modified.
>  (loop for i upfrom 0
as x in '(a b (c))
nconc (if (evenp i) (list x) nil))
(A (C))

```
See Also: `collect`

count Value Accumulation Clause Keyword

counting Value Accumulation Clause Keyword

Syntax:`count`[`ing`] expr [`into` var] [type-spec]

The`count` construct counts the number of times that the specified expression has a non-`nil` value.

The argument var accumulates the number of occurrences; if you specify var, the loop does not return the final count automatically. The var argument is bound as if by the construct`with`.

If you use`into` var, you can specify a data type for var with the type-spec argument; it is an error to specify a nonnumeric data type. Otherwise, you can apply the type-spec argument to the value that is counted, which is equivalent to using the form`(the type-spec expr)`. The default type is`fixnum`.

```> (loop for i in '(a b nil c nil d e)
count i)
5

```
sum Value Accumulation Clause Keyword

summing Value Accumulation Clause Keyword

Syntax:`sum`[`ming`] expr [`into` var] [type-spec]

The`sum` construct forms a cumulative sum of the values of the specified expression at each iteration.

The argument var is used to accumulate the sum; if you specify var, the loop does not return the final sum automatically. The var argument is bound as if by the construct`with`.

If you use`into` var, you can specify a data type for var with the type-spec argument; it is an error to specify a nonnumeric data type. Otherwise, you can apply the type-spec argument to the value that is summed, which is equivalent to using the form`(the type-spec expr)`. The default type is`number`.

```> (loop for i fixnum in '(1 2 3 4 5)
sum i)
15

> (setq series '(1.2 4.3 5.7))
(1.2 4.3 5.7)

> (loop for v in series
sum (* 2.0 v))
22.4
```
maximize Value Accumulation Clause Keyword

maximizing Value Accumulation Clause Keyword

minimize Value Accumulation Clause Keyword

minimizing Value Accumulation Clause Keyword

Syntax:`maximize`|`maximizing` expr [`into` var] [type-spec]

Syntax:`minimize`|`minimizing` expr [`into` var] [type-spec]

The`maximize` construct compares the value of the specified expression obtained during the first iteration with values obtained in successive iterations. The maximum value encountered is determined and returned. If the loop never executes the body, the returned value is not meaningful.

The`minimize` construct is similar to`maximize`; it determines and returns the minimum value.

The argument var accumulates the maximum or minimum value; if you specify var, the loop does not return the maximum or minimum automatically. The var argument is bound as if by the construct`with`.

If you use`into` var, you can specify a data type for var with the type-spec argument; it is an error to specify a nonnumeric data type. Otherwise, you can apply the type-spec argument to the value that is accumulated, which is equivalent to using the form`(the type-spec expr)`. The default type is`number`.

```> (loop for i in '(2 1 5 3 4)
maximize i)
5

> (loop for i in '(2 1 5 3 4)
minimize i)
1

;; In this example, FIXNUM applies to the internal variable that
;; holds the maximum value.
> (setq series '(1.2 4.3 5.7))
(1.2 4.3 5.7)

> (loop for v in series
maximize (round v) fixnum)
6

;; In this example, FIXNUM applies to the variable RESULT.
> (loop for v float in series
minimize (round v) into result fixnum
finally (return result))
1

```

The Loop Facility - 9 SEP 1996

Generated with Harlequin WebMaker