the following attempts to code the one line pattern with one less if. :
if. test1 y do. if. -. test2 x do. 'test2 failed' return. end. end.

The problem is that the verb sideff is called even if it shouldn't be.

test=: 3 : 0
if. -. (2 = sideff y)"_ ^: (*./ y) 1 do. 'bad' return. end.
)
sideff =: 3 : 'a=: +/ y'


   test 1 2
bad
   a
3
   test 1 1
   a
2
   test 1 0
   a
1

is this just due to nature of parentheses, and the interpreter needing to parse 
what is on other side of "_

 
This works as a one liner including shortcircuiting out the side effect when 
appropriate :

   3 (2 = sideff)@:[ ^: (* 0) 1
1
   3 (2 = sideff)@:[ ^: (* 1) 1
0
   2 (2 = sideff)@:[ ^: (* 1) 1
1

but I don't understand why this fails:

guard =: 2 : 0 
:
(u@:[)^: (v y) 1
) 

   3 (2 = sideff) guard *  1
0
   2 (2 = sideff) guard *  1  NB. should be 1
0

is there a way to define guard to properly short circuit and give the right 
answer?  Is it impossible if the u argument evaluates to a noun as it was used 
in first example?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to