2017-12-14 8 views

答えて

1

がちょうど中二階で、あなたは

for: func [ 
    "Repeats a block over a range of values." 
    [catch throw] 
    'word [word!] "Variable to hold current value" 
    start [number! series! time! date! char!] "Starting value" 
    end [number! series! time! date! char!] "Ending value" 
    bump [number! time! char!] "Amount to skip each time" 
    body [block!] "Block to evaluate" 
    /local result do-body op 
][ 
    if (type? start) <> (type? end) [ 
     throw make error! reduce ['script 'expect-arg 'for 'end type? start] 
    ] 
    do-body: func reduce [[throw] word] body 
    op: :greater-or-equal? 
    either series? start [ 
     if not same? head start head end [ 
      throw make error! reduce ['script 'invalid-arg end] 
     ] 
     if (negative? bump) [op: :lesser?] 
     while [op index? end index? start] [ 
      set/any 'result do-body start 
      start: skip start bump 
     ] 
     if (negative? bump) [set/any 'result do-body start] 
    ] [ 
     if (negative? bump) [op: :lesser-or-equal?] 
     while [op end start] [ 
      set/any 'result do-body start 
      start: start + bump 
     ] 
    ] 
    get/any 'result 
] 

のための独自のを書くことができますが、ネットなどにも数強し以上のc-構文のようなバージョンを見つけることができますa proposal for Rebol3

cfor: func [ ; Not this name 
    "General loop based on an initial state, test, and per-loop change." 
    init [block! object!] "Words & initial values as object spec (local)" 
    test [block!] "Continue if condition is true" 
    bump [block!] "Move to the next step in the loop" 
    body [block!] "Block to evaluate each time" 
    /local ret 
] [ 
    if block? init [init: make object! init] 
    test: bind/copy test init 
    body: bind/copy body init 
    bump: bind/copy bump init 
    while test [set/any 'ret do body do bump get/any 'ret] 
] 

赤は、独自のマクロを定義してコンパイルする可能性も提供します。

関連する問題