2.4 Value accumulation
collecting Value Accumulation Clause Keyword
collect
[ing
] expr [into
var]
finally
clause. The var argument is bound as if by the constructwith
.
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
nconc
, append
appending Value Accumulation Clause Keyword
nconc Value Accumulation Clause Keyword
nconcing Value Accumulation Clause Keyword
append
[ing
] expr [into
var]
nconc
[ing
] expr [into
var]
collect
except that the values of the specified expression must be lists. with
.
list
.
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))
collect
counting Value Accumulation Clause Keyword
count
[ing
] expr [into
var] [type-spec]
count
construct counts the number of times that the specified expression has a non-nil
value.
with
.
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 isfixnum
.
> (loop for i in '(a b nil c nil d e) count i) 5sum Value Accumulation Clause Keyword
summing Value Accumulation Clause Keyword
sum
[ming
] expr [into
var] [type-spec]
sum
construct forms a cumulative sum of the values of the specified expression at each iteration.
with
.
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 isnumber
.
> (loop for i fixnum in '(1 2 3 4 5) sum i) 15maximize Value Accumulation Clause Keyword> (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
maximizing Value Accumulation Clause Keyword
minimize Value Accumulation Clause Keyword
minimizing Value Accumulation Clause Keyword
maximize
|maximizing
expr [into
var] [type-spec]
minimize
|minimizing
expr [into
var] [type-spec]
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.
minimize
construct is similar tomaximize
; it determines and returns the minimum value.
with
.
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 isnumber
.
> (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
Generated with Harlequin WebMaker