26.4 Как работает Loop

The driving element of the Loop Facility is the loop macro. When Lisp encounters a loop macro call form, it invokes the Loop Facility and passes to it the loop clauses as a list of unevaluated forms, as with any macro. The loop clauses contain Common Lisp forms and loop keywords. The loop keywords are recognized by their symbol name, regardless of the packages that contain them. The loop macro translates the given form into Common Lisp code and returns the expanded form.

Главным элементом Loop является макрос loop. Как только Common Lisp видит вызов макроса loop, он разворачивает его без вычисления аргументов, что собственно происходит и с любым другим макросом. Loop содержит формы и ключевые символы. Ключевые символы обозначаются символами не обращая внимание на их принадлежность к пакету. Макрос loop транслирует полученную форму в Common Lisp’овый код и возвращает развёрнутую форму.

The expanded loop form is one or more lambda-expressions for the local binding of loop variables and a block and a tagbody that express a looping control structure. The variables established in the loop construct are bound as if by using let or lambda. Implementations can interleave the setting of initial values with the bindings. However, the assignment of the initial values is always calculated in the order specified by the user. A variable is thus sometimes bound to a harmless value of the correct data type, and then later in the prologue it is set to the true initial value by using setq.

Развернутая форма представляется собой одно или более лямбда-выражений для связываний переменных цикла и формы block или tagbody, которые содержат тело цикла. Переменные для цикла создаются как если бы с помощью let или lambda. Связывание локальных переменных происходит в том порядке, в каком указал пользователь.

The expanded form consists of three basic parts in the tagbody:

Развёрнутая форма состоит из трёх основных частей в tagbody:

Expansion of the loop macro produces an implicit block (named nil). Thus, the Common Lisp macro return and the special operator return-from can be used to return values from a loop or to exit a loop.

Раскрытие макроса loop порождает неявный block (с именем nil). Таким образом, внутри цикла для возврата значений или выхода можно пользоваться макросом return или специальным оператором return-from.

Within the executable parts of loop clauses and around the entire loop form, you can still bind variables by using the Common Lisp special operator let.

В любой части выражения цикла можно использовать обычную конструкцию связывания переменных let.