:For i :In ⍳0

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

:For i :In ⍳0

Postby ray on Fri Oct 02, 2020 11:07 am

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
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 221
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: :For i :In ⍳0

Postby petermsiegel on Fri Oct 02, 2020 9:18 pm

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.
petermsiegel
 
Posts: 141
Joined: Thu Nov 11, 2010 11:04 pm


Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest