Книга: Practical Common Lisp
Counting Loops
Counting Loops
Arithmetic iteration clauses control the number of times the loop body will be executed by stepping a variable over a range of numbers, executing the body once per step. These clauses consist of from one to three of the following prepositional phrases after the for
(or as
): the from where phrase, the to where phrase, and the by how much phrase.
The from where phrase specifies the initial value of the clause's variable. It consists of one of the prepositions from
, downfrom
, or upfrom
followed by a form, which supplies the initial value (a number).
The to where phrase specifies a stopping point for the loop and consists of one of the prepositions to
, upto
, below
, downto
, or above
followed by a form, which supplies the stopping point. With upto
and downto
, the loop body will be terminated (without executing the body again) when the variable passes the stopping point; with below
and above
, it stops one iteration earlier.The by how much phrase consists of the prepositions by
and a form, which must evaluate to a positive number. The variable will be stepped (up or down, as determined by the other phrases) by this amount on each iteration or by one if it's omitted.
You must specify at least one of these prepositional phrases. The defaults are to start at zero, increment the variable by one at each iteration, and go forever or, more likely, until some other clause terminates the loop. You can modify any or all of these defaults by adding the appropriate prepositional phrases. The only wrinkle is that if you want decremental stepping, there's no default from where value, so you must specify one with either from
or downfrom
. So, the following:
(loop for i upto 10 collect i)
collects the first eleven integers (zero to ten), but the behavior of this:
(loop for i downto -10 collect i) ; wrong
is undefined. Instead, you need to write this:
(loop for i from 0 downto -10 collect i)
Also note that because LOOP
is a macro, which runs at compile time, it has to be able to determine the direction to step the variable based solely on the prepositions—not the values of the forms, which may not be known until runtime. So, the following:
(loop for i from 10 to 20 ...)
works fine since the default is incremental stepping. But this:
(loop for i from 20 to 10 ...)
won't know to count down from twenty to ten. Worse yet, it won't give you an error—it will just not execute the loop since i
is already greater than ten. Instead, you must write this:
(loop for i from 20 downto 10 ...)
or this:
(loop for i downfrom 20 to 10 ...)
Finally, if you just want a loop that repeats a certain number of times, you can replace a clause of the following form:
for i from 1 to number-form
with a repeat
clause like this:
repeat number-form
These clauses are identical in effect except the repeat
clause doesn't create an explicit loop variable.
- Loops
- Chapter 10. IP Accounting
- Configuring the Kernel for IP Accounting
- Configuring IP Accounting
- Accounting by Address
- Accounting by Service Port
- Accounting of ICMP Datagrams
- Accounting by Protocol
- Using IP Accounting Results
- Listing Accounting Data with ipfwadm
- Listing Accounting Data with ipchains
- Listing Accounting Data with iptables