Rebol 程式設計/for
外觀
FOR 'word start end bump body
在一定範圍的值上重複一個程式碼塊。
FOR 是一個函式值。
- word -- 用於儲存當前值的變數 (型別:詞語)
- start -- 起始值 (型別:數字 序列 貨幣 時間 日期 字元)
- end -- 結束值 (型別:數字 序列 貨幣 時間 日期 字元)
- bump -- 每次跳過的數量 (型別:數字 貨幣 時間 字元)
- body -- 要執行的程式碼塊 (型別:程式碼塊)
- 捕獲
- 丟擲
for: func [
"Repeats a block over a range of values."
[catch throw]
'word [word!] "Variable to hold current value"
start [number! series! money! time! date! char!] "Starting value"
end [number! series! money! time! date! char!] "Ending value"
bump [number! money! 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
]