跳轉到內容

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
]
華夏公益教科書