【发布时间】:2021-03-22 09:45:36
【问题描述】:
repeat 的循环变量可以像这样在它的块中使用:
>> b: func [x] [x + i]
== func [x][x + i]
>> repeat i 2 [print reduce append copy [b] [3]]
4
5
可以看到变量“i”被函数“b”使用了。
但是,在以下示例中,函数看不到循环变量“idx”。给出错误信息:
*** Script Error: idx has no value
Red []
map: function [a-func a-block][
result: [] args: [] clear result
either parse a-block [some block!][
repeat idx length? a-block/1 [
clear args
foreach x a-block [append args to-block x/:idx]
append result reduce append copy [a-func] args
]
return result
]
[
repeat idx length? a-block [
append result to-block a-func a-block/:idx
]
]
]
map func [x y] [x + y - idx] [[1 2] [3 4]]
为什么第二个代码错了?以及如何让它发挥作用?
【问题讨论】:
标签: red