My recursive for-looping solution:

      TEST=:3 :0
if. 0= #@$ y do. r=:r+y return. end.
out=.0{.y
for_i. y do. out=.out,TEST i end.
)

   TEST 5#,:i.10
  0   1   3   6  10  15  21  28  36  45
 45  46  48  51  55  60  66  73  81  90
 90  91  93  96 100 105 111 118 126 135
135 136 138 141 145 150 156 163 171 180
180 181 183 186 190 195 201 208 216 225

Marshall

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Zsbán Ambrus
Sent: Monday, March 14, 2011 7:15 AM
To: Programming forum
Subject: Re: [Jprogramming] Recursive algorithm

On Mon, Mar 14, 2011 at 3:34 AM, Robert O'Boyle <[email protected]>
wrote:
> (TEST"0  i.10) "0 i.5

This doesn't work because (TEST"0 i.10) is a noun, so the outer call to rank
will just replicate that array.  If you want to repeat an operation multiple
times, you need to apply rank to a verb.

   r=:0
   ([: TEST"0 (i.10)"_) "0 i.5
  0   1   3   6  10  15  21  28  36  45
 45  46  48  51  55  60  66  73  81  90
 90  91  93  96 100 105 111 118 126 135
135 136 138 141 145 150 156 163 171 180
180 181 183 186 190 195 201 208 216 225

Of course, in this case you could just do something like this,

   TEST"0] 5$,:i.10

Alternately you could use explicit for. loops.

   3 :0] 0
r =: 0
os =. i. 0 0
for_is. i. 5
do. ot =. $ 0
for_it. i. 10
do. ot =. ot , TEST it
end.
os =. os , ot
end.
os
)
  0   1   3   6  10  15  21  28  36  45
 45  46  48  51  55  60  66  73  81  90
 90  91  93  96 100 105 111 118 126 135
135 136 138 141 145 150 156 163 171 180
180 181 183 186 190 195 201 208 216 225

(For loops could have the advantage that I think jsoftware has made some
vague statements on the mailing lists that if you use the rank operator on
verbs with side effects, it might not always call the verb as many times or
in the right order in some case of optimizations in future versions.  This
is belivable because (u/\. y) already does such an optimization, and in fact
I think that's a right idea.)

--
Ambrus
----------------------------------------------------------------------
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