The loop keywords always, never, thereis, until, and while designate constructs that use a single test condition to determine when loop iteration should terminate.
Ключевые символы always, never, thereis, until и while обозначают конструкции, которые с помощью условия проверяют должен ли быть завершён цикл.
The constructs always, never, and thereis provide specific values to be returned when a loop terminates. Using always, never, or thereis with value-returning accumulation clauses can produce unpredictable results. In all other respects these constructs behave like the while and until constructs.
The macro loop-finish can be used at any time to cause normal termination. In normal termination, finally clauses are executed and default return values are returned.
End-test control constructs can be used anywhere within the loop body. The termination conditions are tested in the order in which they appear.
The while construct allows iteration to continue until the specified expression expr evaluates to nil. The expression is re-evaluated at the location of the while clause.
The until construct is equivalent to while (not expr). If the value of the specified expression is non-nil, iteration terminates.
You can use while and until at any point in a loop. If a while or until clause causes termination, any clauses that precede it in the source are still evaluated.
Examples:
The always construct takes one form and terminates the loop if the form ever evaluates to nil; in this case, it returns nil. Otherwise, it provides a default return value of t.
The never construct takes one form and terminates the loop if the form ever evaluates to non-nil; in this case, it returns nil. Otherwise, it provides a default return value of t.
The thereis construct takes one form and terminates the loop if the form ever evaluates to non-nil; in this case, it returns that value.
If the while or until construct causes termination, control is passed to the loop epilogue, where any finally clauses will be executed. Since always, never, and thereis use the Common Lisp macro return to terminate iteration, any finally clause that is specified is not evaluated.
Examples:
The macro loop-finish terminates iteration normally and returns any accumulated result. If specified, a finally clause is evaluated.
In most cases it is not necessary to use loop-finish because other loop control clauses terminate the loop. Use loop-finish to provide a normal exit from a nested condition inside a loop.
You can use loop-finish inside nested Lisp code to provide a normal exit from a loop. Since loop-finish transfers control to the loop epilogue, using loop-finish within a finally expression can cause infinite looping.
Implementations are allowed to provide this construct as a local macro by using macrolet.
Examples: