Page 1 of 1

:For i :In ⍳0

Posted: Fri Oct 02, 2020 11:07 am
by ray
Hi,
I found the following error "amusing".

Given a function Foo

Code: Select all

     ∇ Foo;i
[1]    :For i :In ⍳0
[2]        i
[3]    :End
[4]    :If i>1
[5]        'over 1'
[6]    :End
     ∇       

I did not expect it to error with a value error thus.

Code: Select all

      Foo   
VALUE ERROR: Undefined name: i
Foo[5] :If i>1
           ∧

Somehow (but I don't understand how it really could!) I would have expected "i" to have been assigned a value!
(An "empty" scalar perhaps? LOL)

Ray

Re: :For i :In ⍳0

Posted: Fri Oct 02, 2020 9:18 pm
by petermsiegel
The reference guide (p. 82 for release 18.0) gives a good description of the logic. Read literally, it does accurately describe the expected behaviour, but OTOH it could say just a tad more, since the null case is always important. One might helpfully add the words in red.
The way a :For loop operates is as follows. On encountering the :For, the expression to the right of :In is evaluated and the result stored. This is the control array. The control variable, named to the right of the :For, is then assigned the first value in the control array, and the code between :For and :EndFor is executed....
Note that if the control array is empty, the control variable is not set (or updated) and the code in the body of the :For structure is not executed.
You could of course set the variable in advance:
      i←⍬
and life would be good.