Well diagnosed Anton, your result surprised me, thanks..

In general I agree with Raul, in this case I think a for. Loop with a 
conditional early exit is the best solution (and likely clearer in the code 
too).

Good job, and I learnt something out of this too, so thanks !…/Rob

> On 26 May 2020, at 8:51 pm, Anton Wallgren <[email protected]> wrote:
> 
> I meant that when v returns 0, f is not executed (and so will not print 
> anything), but v is still queried until the iteration count is reached, like 
> this:
> 
>    f=: verb define
> smoutput 'res = ' , ":r=:2*y
> r
> )
>    v=: verb define
> smoutput 'v = ' , ": r=: (10 > ]) y
> r
> )
>    f^:v^:10 ] 1
> v = 1
> res = 2
> v = 1
> res = 4
> v = 1
> res = 8
> v = 1
> res = 16
> v = 0
> v = 0
> v = 0
> v = 0
> v = 0
> v = 0
> 16
> 
> Regards, Anton Wallgren
> On 26 May 2020, 12:04 +0200, 'Rob Hodgkinson' via Programming 
> <[email protected]>, wrote:
>> I don’t agree, it does not proceed further.
>> 
>> ([x] u^:v^:_ y) will actually stop when v returns 0 (does not skip or 
>> continue executing)
>> 
>> Here is a simple way to confirm this using a function f to do 2&* but which 
>> also uses ’smoutput’ to output to the session log (print intermediate 
>> results).
>> 
>> f=: verb define
>> smoutput r=:2*y
>> r
>> )
>> 
>> f 5
>> 10 NB. Intermediate print using smoutput
>> 10. NB. Result of f
>> 
>> Test it…
>> a=:f 5
>> 5. NB. It still prints within f via smoutput
>> 
>> So now …
>> f^:(10>])^:3 ] 1
>> 2 }
>> 4 } Intermediates from smoutpout (f called 3 times)
>> 8 }
>> 8 NB. Result returned (ran to completion)
>> f^:(10>])^:4 ] 1
>> 2 }
>> 4 } Intermediates from smoutpoutn (f called 4 times)
>> 8 }
>> 16 }
>> 16 NB. Result returned (ran to completion)
>> f^:(10>])^:8 ] 1
>> 2 }
>> 4 } Intermediates from smoutput (f called 4 times)
>> 8 }
>> 16 }
>> 16 NB. Result returned (aborted, so it stopped short and did not proceed 
>> further)
>> 
>> HTH, Rob
>> 
>> 
>>> On 26 May 2020, at 6:40 pm, Anton Wallgren <[email protected]> wrote:
>>> 
>>> On closer inspection, f^:v^:n doesn’t seem to work (as I want it to). What 
>>> it does is to skip evaluation each time v y returns 0. While this would 
>>> work in my situation, as the stopping condition would remain true, it would 
>>> of course be unnecessary (and inefficient) to keep checking this for the 
>>> remainder of the iterations.
>>> 
>>> Thanks, Anton Wallgren
>> 
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to