Next: , Previous: , Up: Loop Facility   [Contents][Index]

4.7.4 Iteration Clauses

Aside from for clauses, there are several other loop clauses that control the way the loop operates. They might be used by themselves, or in conjunction with one or more for clauses.

repeat integer

This clause simply counts up to the specified number using an internal temporary variable. The loops

(cl-loop repeat (1+ n) do …)
(cl-loop for temp to n do …)

are identical except that the second one forces you to choose a name for a variable you aren’t actually going to use.

while condition

This clause stops the loop when the specified condition (any Lisp expression) becomes nil. For example, the following two loops are equivalent, except for the implicit nil block that surrounds the second one:

(while cond forms…)
(cl-loop while cond do forms…)
until condition

This clause stops the loop when the specified condition is true, i.e., non-nil.

always condition

This clause stops the loop when the specified condition is nil. Unlike while, it stops the loop using return nil so that the finally clauses are not executed. If all the conditions were non-nil, the loop returns t:

(if (cl-loop for size in size-list always (> size 10))
    (only-big-sizes)
  (some-small-sizes))
never condition

This clause is like always, except that the loop returns t if all conditions were false, or nil otherwise.

thereis condition

This clause stops the loop when the specified form is non-nil; in this case, it returns that non-nil value. If all the values were nil, the loop returns nil.

iter-by iterator

This clause iterates over the values from the specified form, an iterator object. See (see Generators in GNU Emacs Lisp Reference Manual).

Next: Accumulation Clauses, Previous: For Clauses, Up: Loop Facility   [Contents][Index]