Ric,

Regarding the nature of the error that occurs with your original code attempt

   ops=: +`*`-
   ((0 1 2,:0 2 1){ops) /"1 _] 1 2 3 4  NB. Ric's attempt

|rank error

   ((0 1 2,:0 2 1){ops) 4 :'x/y'"1]1 2 3 4 NB. Raul's fix
_1 _9

The rank error occurs because there are two things to accomplish but your code 
only provides for one of them. Insertion allows verbs to take effect between 
each of the four integers -- and as you've found it works fine so long as what 
is to the left of the slash is not fancier than a list of verbs.

But you want to do this again and again, once for each list of verb-lists you 
specify. Here's the example you used:

   ((0 1 2,:0 2 1){ops)
+-+-+-+
|+|*|-|
+-+-+-+
|+|-|*|
+-+-+-+

When you try to insert this into (1 2 3 4) you're specifying something like 
this:
   
  +   *   -
1   2   3   4
  +   -   *

Conceivably this could be interpreted as you wish, but J does not do that. 
Instead, we get "rank error", which here means "Whoa, you've handed me a stack 
of verbs between each of these items; I need one verb for each pair of nouns."

If we think in terms of loops, we need one loop for each of the two axes in the 
verb array. Insert supplies only one them. Raul's solution is to build a verb 
that supplies the other.

The fact that Insert is overwhelmed as soon as it gets a multi-axis left 
parameter can be seen below, which shows that everything that was to the right 
of the slash is irrelevant to this problem:

   ((0 1 2,:0 2 1){ops)/
|rank error
|       ((0 1 2,:0 2 1){ops)/

When Insert is used the rank of the resulting verb defaults to maximum.  E.g.

   + b. 0
0 0 0
   (+/) b. 0
_ _ _
   (4 :'x/y') b. 0
_ _ _
   (4 :'x/y'"1) b. 0
1 1 1

The last line of these four lines of code shows the verb Raul wrote that did 
the trick. By specifying rank-one for the resulting verb, it means "take rows 
of x and apply them to rows of y" rather than "take all of x and apply it to 
all of y".  This provides the row-wise interpretation (cycling) of your array 
of gerunds, while insert (/) continues to provide the "inner loop."

I really enjoyed seeing -- and thinking through -- this multi-axis application 
of verbs, Ric.
 --Tracy


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

Reply via email to