跳转到内容

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
]
华夏公益教科书