红色的for循环的等价物?

时间:2017-12-14 12:26:18

标签: rebol red

我想用于http://www.rebol.com/docs/words/wfor.html 对于Red,它没有用。

等同于什么?

1 个答案:

答案 0 :(得分:1)

由于Rebol2中的 只是一个夹层,您可以自己编写

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语法的版本,例如一个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]
]

Red还提供了定义自己的宏并编译它的可能性。