The Loop Facility provides the named construct to name a loop so that the Common Lisp special operator return-from can be used.
Для задания имени цикла используется конструкция named. Данное имя впоследствии можно использовать в return-from.
The loop keywords initially and finally designate loop constructs that cause expressions to be evaluated before and after the loop body, respectively.
Символы initially и finally обозначают выражения, которые будут выполнены перед и после тела цикла соответственно.
The code for any initially clauses is collected into one progn in the order in which the clauses appeared in the loop. The collected code is executed once in the loop prologue after any implicit variable initializations.
Выражения после всех initially собираются в один progn в исходном порядке. Сгруппированный код выполняется единожды перед началом итераций.
The code for any finally clauses is collected into one progn in the order in which the clauses appeared in the loop. The collected code is executed once in the loop epilogue before any implicit values are returned from the accumulation clauses. Explicit returns in the loop body, however, will exit the loop without executing the epilogue code.
Выражения после всех finally собираются в один progn в исходном порядке. Сгруппированный код выполняется единожды после выполнения всех итераций в эпилоге перед неявным возвратом значений. В случае явного выхода из цикла, эпилог не выполняется.
Many loop constructs take a type-spec argument that allows you to specify certain data types for loop variables. While it is not necessary to specify a data type for any variable, by doing so you ensure that the variable has a correctly typed initial value. The type declaration is made available to the compiler for more efficient loop expansion. In some implementations, fixnum and float declarations are especially useful; the compiler notices them and emits more efficient code.
Многие конструкции принимают аргумент type-spec, который позволяет задать тип для переменной. Конечно в этом нет прямой необходимости, но декларации типов упрощают дальнейшую работу с программой. Декларация типов также помогает компилятору оптимизировать программу. Особенно это касается типов fixnum и float.
The type-spec argument has the following syntax:
Аргумент type-spec выглядит так:
Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable. During loop expansion, each variable in the variable list is matched with the values in the values list. If there are more variables in the variable list than there are values in the values list, the remaining variables are given a value of nil. If there are more values than variables listed, the extra values are discarded.
Suppose you want to assign values from a list to the variables a, b, and c. You could use one for clause to bind the variable numlist to the car of the specified expression, and then you could use another for clause to bind the variables a, b, and c sequentially.
Destructuring makes this process easier by allowing the variables to be bound in parallel in each loop iteration. You can declare data types by using a list of type-spec arguments. If all the types are the same, you can use a shorthand destructuring syntax, as the second example following illustrates.
If you use destructuring to declare or initialize a number of groups of variables into types, you can use the loop keyword and to simplify the process further.
A data type specifier for a destructuring pattern is a tree of type specifiers with the same shape as the tree of variables, with the following exceptions:
If nil is used in a destructuring list, no variable is provided for its place.
Note that nonstandard lists can specify destructuring.
[It is worth noting that the destructuring facility of loop predates, and differs in some details from, that of destructuring-bind, an extension that has been provided by many implementors of Common Lisp.—GLS]
The initially construct causes the specified expression to be evaluated in the loop prologue, which precedes all loop code except for initial settings specified by constructs with, for, or as. The finally construct causes the specified expression to be evaluated in the loop epilogue after normal iteration terminates.
The expr argument can be any non-atomic Common Lisp form.
Clauses such as return, always, never, and thereis can bypass the finally clause.
The Common Lisp macro return (or the return loop construct) can be used after finally to return values from a loop. The evaluation of the return form inside the finally clause takes precedence over returning the accumulation from clauses specified by such keywords as collect, nconc, append, sum, count, maximize, and minimize; the accumulation values for these pre-empted clauses are not returned by the loop if return is used.
The constructs do, initially, and finally are the only loop keywords that take an arbitrary number of (non-atomic) forms and group them as if by using an implicit progn.
Examples:
The named construct allows you to assign a name to a loop construct so that you can use the Common Lisp special operator return-from to exit the named loop.
Only one name may be assigned per loop; the specified name becomes the name of the implicit block for the loop.
If used, the named construct must be the first clause in the loop expression, coming right after the word loop.
Example: