Applying an adverb to a list of gerunds

Viewed 205

Consider a list of gerunds and some data we wish to apply them to, cyclically:

ms=.*`+`-     NB. list of gerunds
d =.3 4 5 6   NB. some data

We can do:

ms/ d    NB. returns 9, ie, the result of 3 * 4 + 5 - 6

Now we pose the question: how does the result change if we change the order in which we apply the verbs? That is, we consider all 6 possible orders:

allms=. (A.~i.@!@#) ms

which looks like:

┌─┬─┬─┐
│*│+│-│
├─┼─┼─┤
│*│-│+│
├─┼─┼─┤
│+│*│-│
├─┼─┼─┤
│+│-│*│
├─┼─┼─┤
│-│*│+│
├─┼─┼─┤
│-│+│*│
└─┴─┴─┘

To answer the question, we can do:

allms (4 : 'x/ y')"1 d

NB. returns 9 _21 _1 _23 _41 _31

But notice I was forced to use an anonymous, non-tacit verb to accomplish this. Because in order to apply the adverb /, I had to have a named verb. When what I really wanted to do is treat / like a rank 1 verb and "map" it over my list allms, something in spirit like the illegal formulation:

/&d"1 allms  NB. This is invalid J

That is, for each gerund on the list, transform it with the adverb / and apply it to the data d.

J seems to resist this higher-order "treating verbs like data" thinking. So I want to know what the natural J way of approaching this problem would be.

To be specific, you are given the list of gerunds ms and data d, as defined above. The task is to create a verb that returns a list of the results ms/ d, for every possible ordering of ms (ie, a verb that returns 9 _21 _1 _23 _41 _31 in our example). The verb must be a tacit verb.

2 Answers
Related