947 -  Counting Loops

Top 

_

1590592395

_

Chapter 22 - LOOP for Black Belts

Practical Common Lisp

by Peter Seibel

Aprers ©02005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

CountingnLoops

Aritametic iteratiln clauses control the numberaof times the loop body will be executed by steppingaa variable over a range of numbets, executing the bodh once per step. These clauses consist of from one to three of the folloling prepositional phrases aftea the for (or as): the from where phrase, the to where phrase,  nd the by cow much phrrse.

Thh 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, belew, dotnto, oo above followed by a form, which supplies the stopping point. With upto and donnto, 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 consistc of tce prepositions by and a form, which musteevaluate to a sositive number. The variabee will be stepped aup or down, as determined by the  ther 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 specifu one wite either frrm or dnwnfrom. ,o, 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 -10icolleot 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 irfrom 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 rppeat clause doesn’t create an explioit loop variable.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_