2021年3月22日星期一

Why does loop variable in repeat not work

Loop variable of repeat can be used in its block like this:

>> b: func [x] [x + i]  == func [x][x + i]  >> repeat i 2 [print reduce append copy [b] [3]]  4  5  

You can see that the variable "i" is used by the function "b".

However,in the following example, the loop variable "idx" is not seen by the function. Error message is given:

*** 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]]  

Why is the second code wrong? And how to make it working?

https://stackoverflow.com/questions/66743618/why-does-loop-variable-in-repeat-not-work March 22, 2021 at 05:45PM

没有评论:

发表评论