Macro LOOP

UP


Macro LOOP

Macro LOOP

構文

単純なloopフォーム:
loop compound-form* => result*

拡張されたloopフォーム:
loop [name-clause] {variable-clause}* {main-clause}*
=> result*

name-clause::= named name 
variable-clause::= with-clause | initial-final | for-as-clause 
with-clause::= with var1 [type-spec] [= form1] {and var2 [type-spec] [= form2]}* 
main-clause::= unconditional | accumulation | conditional | termination-test | initial-final 
initial-final::= initially compound-form+ | finally compound-form+ 
unconditional::= {do | doing} compound-form+ | return {form | it} 
accumulation::= list-accumulation | numeric-accumulation 
list-accumulation::= {collect | collecting | append | appending | nconc | nconcing} {form | it}  
                     [into simple-var] 
numeric-accumulation::= {count | counting | sum | summing | } 
                         maximize | maximizing | minimize | minimizing {form | it} 
                        [into simple-var] [type-spec] 
conditional::= {if | when | unless} form selectable-clause {and selectable-clause}*  
               [else selectable-clause {and selectable-clause}*]  
               [end] 
selectable-clause::= unconditional | accumulation | conditional 
termination-test::= while form | until form | repeat form | always form | never form | thereis form 
for-as-clause::= {for | as} for-as-subclause {and for-as-subclause}* 
for-as-subclause::= for-as-arithmetic | for-as-in-list | for-as-on-list | for-as-equals-then | 
                    for-as-across | for-as-hash | for-as-package 
for-as-arithmetic::= var [type-spec] for-as-arithmetic-subclause 
for-as-arithmetic-subclause::= arithmetic-up | arithmetic-downto | arithmetic-downfrom 
arithmetic-up::= [[{from | upfrom} form1 |   {to | upto | below} form2 |   by form3]]+ 
arithmetic-downto::= [[{ {from form1} }1  |   { { {downto | above} form2} }1  |   by form3]] 
arithmetic-downfrom::= [[{ {downfrom form1} }1  |   {to | downto | above} form2 |   by form3]] 
for-as-in-list::= var [type-spec] in form1 [by step-fun] 
for-as-on-list::= var [type-spec] on form1 [by step-fun] 
for-as-equals-then::= var [type-spec] = form1 [then form2] 
for-as-across::= var [type-spec] across vector 
for-as-hash::= var [type-spec] being {each | the}  
               { {hash-key | hash-keys} {in | of} hash-table  
                [using (hash-value other-var)] |  
                {hash-value | hash-values} {in | of} hash-table  
                [using (hash-key other-var)]} 
for-as-package::= var [type-spec] being {each | the}  
                  {symbol | symbols | 
                   present-symbol | present-symbols | 
                   external-symbol | external-symbols} 
                  [{in | of} package] 
type-spec::= simple-type-spec | destructured-type-spec 
simple-type-spec::= fixnum | float | t | nil 
destructured-type-spec::= of-type d-type-spec 
d-type-spec::= type-specifier | (d-type-spec . d-type-spec) 
var::= d-var-spec 
var1::= d-var-spec 
var2::= d-var-spec 
other-var::= d-var-spec 
d-var-spec::= simple-var | nil | (d-var-spec . d-var-spec) 

引数と戻り値

compound-form - compound-form
name - シンボル
simple-var - シンボル(変数名)
form, form1, form2, form3 - フォーム
step-fun - ひとつの引数を取る関数に評価されるフォーム
vector - vectorに評価されるフォーム
hash-table - ハッシュテーブルに評価されるフォーム
package - パッケージ指定子に評価されるフォーム
type-specifier - 型指定子。 これは原始的な型指定子でも合成された型指定子のどちらかであり、 それは分配される様式を正式に構文解析するような いくつか追加された複雑な機能が導入されているかもしれません。 詳しくは6.1.1.7. 分配をご確認ください。
result - オブジェクト

定義

詳細は6.1. 繰り返しの機能をご確認ください。

例文

;; LOOPの単純なフォームの例
(defun sqrt-advisor ()
  (loop (format t "~&Number: ")
        (let ((n (parse-integer (read-line) :junk-allowed t)))
          (when (not n) (return))
          (format t "~&The square root of ~D is ~D.~%" n (sqrt n)))))
=>  SQRT-ADVISOR
 (sqrt-advisor)
>>  Number: 5<NEWLINE>
>>  The square root of 5 is 2.236068.
>>  Number: 4<NEWLINE>
>>  The square root of 4 is 2.
>>  Number: done<NEWLINE>
=>  NIL

;; LOOPの拡張されたフォームの例
(defun square-advisor ()
  (loop as n = (progn (format t "~&Number: ")
                      (parse-integer (read-line) :junk-allowed t))
        while n
        do (format t "~&The square of ~D is ~D.~%" n (* n n))))
=>  SQUARE-ADVISOR
 (square-advisor)
>>  Number: 4<NEWLINE>
>>  The square of 4 is 16.
>>  Number: 23<NEWLINE>
>>  The square of 23 is 529.
>>  Number: done<NEWLINE>
=>  NIL

;; LOOPの拡張されたフォームの別の例
(loop for n from 1 to 10
      when (oddp n)
        collect n)
=>  (1 3 5 7 9)

影響

なし。

例外

なし。

参考

do, dolist, dotimes, return, go, throw, 6.1.1.7. 分配

備考

単純なloopフォームの中では loop-finishを使用することはできませんが、 それ以外で単純なloopフォームは、 拡張されたloopフォームと下記のような関係があります。

(loop compound-form*) ==  (loop do compound-form*)

TOP, Github