2.2 Iteration control
as Iteration Control Clause Keyword
{for
|as
} var [type-spec] [{from
|downfrom
|upfrom
} expr1]
[{to
|downto
|upto
|below
|above
} expr2]
[by
expr3]
for
andas
clauses iterate by using one or more local loop variables that are initialized to some value and that can be modified or stepped after each iteration. For these clauses, iteration terminates when a local variable reaches some specified value or when some other loop clause terminates iteration. At each iteration, variables can be stepped by an increment or a decrement or can be assigned a new value by the evaluation of an expression. Destructuring can be used to assign initial values to variables during iteration.
for
andas
keywords are synonyms; you can use them interchangeably. There are five syntactic representations for these constructs, which are described in the following pages. In each syntactic description, the data type of var can be specified by the optional type-spec argument. If var is a destructuring list, the data type specified by the type-spec argument must appropriately match the elements of the list. See Section 2.8.1 on page 38 and Section 2.8.2 on page 39 for more information.
for
oras
construct iterates from the value specified by expr1 to the value specified by expr2 in increments or decrements denoted by expr3. Each expression is evaluated only once and must evaluate to a number.
from
from
marks the value from which stepping begins, as specified by expr1. Stepping is incremental by default. If you want decremental stepping, you must use either the prepositiondownto
or the prepositionabove
with expr2. For incremental stepping, the defaultfrom
value is 0.
downfrom
,upfrom
downfrom
indicates that the variable var is decreased in decrements specified by expr3; the loop keywordupfrom
indicates that var is increased in increments specified by expr3.
to
to
marks the end value for stepping specified in expr2. Stepping is incremental by default. If you want decremental stepping, you must use either the prepositiondownto
,downfrom
, orabove
with expr2.
downto
,upto
downto
allows iteration to proceed from a larger number to a smaller number by the decrement expr3. The loop keywordupto
allows iteration to proceed from a smaller number to a larger number by the increment expr3. Since there is no default for expr1 in decremental stepping, you must supply a value withdownto
.
below
,above
below
andabove
are analogous toupto
anddownto
respectively. These keywords stop iteration just before the value of the variable var reaches the value specified by expr2; the end value of expr2 is not included. Since there is no default for expr1 in decremental stepping, you must supply a value withabove
.
by
by
marks the increment or decrement specified by expr3. The value of expr3 can be any positive number. The default value is 1.
for
oras
construct causes termination when the specified limit is reached. That is, iteration continues until the value var is stepped to the exclusive limit or inclusive limit specified by expr2. The range is exclusive if expr3 increases or decreases var to the value of expr2 without reaching that value; the loop keywordsbelow
andabove
provide exclusive limits. An inclusive limit allows var to attain the value of expr2;to
,downto
, andupto
provide inclusive limits.
for
introduces new iterations andas
introduces iterations that depend on a previous iteration specification.
;; Print some numbers. > (loop as i from 1 to 3 do (print i)) 1 2 3 NILSyntax 2;; Print every third number. > (loop for i from 10 downto 1 by 3 do (print i)) 10 7 4 1 NIL
;; Step incrementally from the default starting value. > (loop as i below 3 do (print i)) 0 1 2 NIL
for
|as
} var [type-spec]in expr1
[by step-fun
]
endp.
The variable var is bound to the successive elements of the list expr1 before each iteration. At the end of each iteration, the function step-fun is applied to the list; the default value for step-fun is thecdr
function.
in
andby
serve as valid prepositions in this syntax.
for
oras
construct causes termination when the end of the list is reached.
;; Print every item in a list. > (loop for item in '(1 2 3) do (print item)) 1 2 3 NIL;; Print every other item in a list. > (loop for item in '(1 2 3 4 5) by #'cddr do (print item)) 1 3 5 NIL
;; Destructure a list, and sum the x values using fixnum ;; arithmetic. > (loop for (item . x) (t . fixnum) in '((A . 1) (B . 2) (C . 3)) unless (eq item 'B) sum x) 4
for
/as, Syntax 3
for
|as
} var [type-spec]on expr1
[by step-fun
]
atom
. The variable var is bound to the successive tails of the list expr1. At the end of each iteration, the function step-fun is applied to the list; the default value for step-fun is thecdr
function.
on
andby
serve as valid prepositions in this syntax.
for
oras
construct causes termination when the end of the list is reached.
;; Collect successive tails of a list. > (loop for sublist on '(a b c d) collect sublist) ((A B C D) (B C D) (C D) (D));; Print a list by using destructuring with the loop keyword ON. > (loop for (item) on '(1 2 3) do (print item)) 1 2 3 NIL
;; Print items in a list without using destructuring. > (loop for item in '(1 2 3) do (print item)) 1 2 3 NIL
for
/as, Syntax 2
for
|as
} var [type-spec]= expr1
[then expr2
]
then
clause, expr1 is evaluated one time only. At the end of each nonterminating iteration, var is bound to the value of expr2, if such an expression is specified.
=
andthen
serve as valid prepositions in this syntax.
;; Collect some numbers. > (loop for item = 1 then (+ item 10) for iteration from 1 to 5 collect item) (1 11 21 31 41)
for
/as, Syntax 5
for
|as
} var [type-spec]first expr1 then expr2
first
first
marks the expression expr1 that is evaluated in the first iteration.
first
rather than=
in cases where expr1 depends on a previous iteration value.
then
marks the expression expr2.
;; Collect birthdays. > (loop for b-day in '(birthdays (joe jan) (alice dec) (curly jul)) for bday-info first b-day then (car b-day) collect bday-info) (BIRTHDAYS JOE ALICE CURLY)
for
/as, Syntax 4
repeat
expr
repeat
construct causes iteration to terminate after a specified number of times.
> (loop repeat 3 do (format t "What I say three times is true~%")) What I say three times is true What I say three times is true What I say three times is true NIL> (loop repeat -15 do (format t "What you see is what you expect~%")) NIL
Generated with Harlequin WebMaker