相当于红色的for循环?
Equivalent of for loop in Red?
我想用于 http://www.rebol.com/docs/words/wfor.html
对于红色,它不起作用。
什么是等价物?
由于Rebol2中的for只是一个夹层,你可以自己写for
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 还提供了定义您自己的宏并对其进行编译的可能性。
我想用于 http://www.rebol.com/docs/words/wfor.html 对于红色,它不起作用。
什么是等价物?
由于Rebol2中的for只是一个夹层,你可以自己写for
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 还提供了定义您自己的宏并对其进行编译的可能性。