Loop Examples
Loop Facility
Iteration Clauses
Most loops are governed by one or more for
clauses.
A for
clause simultaneously describes variables to be
bound, how those variables are to be stepped during the loop,
and usually an end condition based on those variables.
The word as
is a synonym for the word for
. This
word is followed by a variable name, then a word like from
or across
that describes the kind of iteration desired.
In Common Lisp, the phrase being the
sometimes precedes
the type of iteration; in this package both being
and
the
are optional. The word each
is a synonym
for the
, and the word that follows it may be singular
or plural: `for x being the elements of y
' or
`for x being each element of y
'. Which form you use
is purely a matter of style.
The variable is bound around the loop as if by let
:
(setq i 'happy) (loop for i from 1 to 10 do (do-something-with i)) i => happy
for var from expr1 to expr2 by expr3
|
This type of
The three expressions are the starting value, the ending value, and
the step value, respectively, of the variable. The loop counts
upwards by default (expr3 must be positive), from expr1
to expr2 inclusively. If you omit the
You can replace the word
The |
for var in list by function
|
This clause iterates var over all the elements of list,
in turn. If you specify the
|
for var on list by function
|
This clause iterates var over all the cons cells of list.
With
where |
for var in-ref list by function
|
This is like a regular
increments every element of |
for var across array
|
This clause iterates var over all the elements of array, which may be a vector or a string.
|
for var across-ref array
|
This clause iterates over an array, with var a |
for var being the elements of sequence
|
This clause iterates over the elements of sequence, which may
be a list, vector, or string. Since the type must be determined
at run-time, this is somewhat less efficient than
This clause type is taken from older versions of the |
for var being the elements of-ref sequence
|
This clause iterates over a sequence, with var a |
for var being the symbols [of obarray]
|
This clause iterates over symbols, either over all interned symbols or over all symbols in obarray. The loop is executed with var bound to each symbol in turn. The symbols are visited in an unspecified order. As an example,
returns a list of all the functions whose names begin with `
The Common Lisp words
Due to a minor implementation restriction, it will not work to have
more than one |
for var being the hash-keys of hash-table
|
This clause iterates over the entries in hash-table. For each
hash table entry, var is bound to the entry's key. If you write
` |
for var being the key-codes of keymap
|
This clause iterates over the entries in keymap. In GNU Emacs
18 and 19, keymaps are either alists or vectors, and key-codes are
integers or symbols. In Lucid Emacs 19, keymaps are a special new
data type, and key-codes are symbols or lists of symbols. The
iteration does not enter nested keymaps or inherited (parent) keymaps.
You can use ` |
for var being the key-seqs of keymap
|
This clause iterates over all key sequences defined by keymap
and its nested keymaps, where var takes on values which are
strings in Emacs 18 or vectors in Emacs 19. The strings or vectors
are reused for each iteration, so you must copy them if you wish to keep
them permanently. You can add a ` |
for var being the overlays [of buffer] ...
|
This clause iterates over the Emacs 19 ``overlays'' or Lucid
Emacs ``extents'' of a buffer (the clause |
for var being the intervals [of buffer] ...
|
This clause iterates over all intervals of a buffer with constant
text properties. The variable var will be bound to conses
of start and end positions, where one start position is always equal
to the previous end position. The clause allows |
for var being the frames
|
This clause iterates over all frames, i.e., X window system windows
open on Emacs files. This clause works only under Emacs 19. The
clause |
for var being the windows [of frame]
|
This clause iterates over the windows (in the Emacs sense) of
the current frame, or of the specified frame. (In Emacs 18
there is only ever one frame, and the |
for var being the buffers
|
This clause iterates over all buffers in Emacs. It is equivalent
to ` |
for var = expr1 then expr2
|
This clause does a general iteration. The first time through the loop, var will be bound to expr1. On the second and successive iterations it will be set by evaluating expr2 (which may refer to the old value of var). For example, these two loops are effectively the same:
Note that this type of
If you omit the
This loop keeps taking random numbers from the |
If you include several for
clauses in a row, they are
treated sequentially (as if by let*
and setq
).
You can instead use the word and
to link the clauses,
in which case they are processed in parallel (as if by let
and psetq
).
(loop for x below 5 for y = nil then x collect (list x y)) => ((0 nil) (1 1) (2 2) (3 3) (4 4)) (loop for x below 5 and y = nil then x collect (list x y)) => ((0 nil) (1 0) (2 1) (3 2) (4 3))
In the first loop, y
is set based on the value of x
that was just set by the previous clause; in the second loop,
x
and y
are set simultaneously so y
is set
based on the value of x
left over from the previous time
through the loop.
Another feature of the loop
macro is destructuring,
similar in concept to the destructuring provided by defmacro
.
The var part of any for
clause can be given as a list
of variables instead of a single variable. The values produced
during loop execution must be lists; the values in the lists are
stored in the corresponding variables.
(loop for (x y) in '((2 3) (4 5) (6 7)) collect (+ x y)) => (5 9 13)
In loop destructuring, if there are more values than variables
the trailing values are ignored, and if there are more variables
than values the trailing variables get the value nil
.
If nil
is used as a variable name, the corresponding
values are ignored. Destructuring may be nested, and dotted
lists of variables like (x . y)
are allowed.