What constitutes a “compatible shape” for array arguments to J verbs?
Question
I'm in the process of learning (and having my mind blown by) J, and reading Learning J. I have noticed that many (all?) verbs I've been learning are pretty flexible with their arguments. I can do the traditional 4 - 2
, or I can use it with lists e.g. 4 5 - 3 4
, and can even do 3 4 5 - 1
which seems to "promote" the right-hand side of the operator to an array even though I just gave it a scalar.
I assume the latter behavior is covered by this line, from ch. 2 of the aforementioned text:
Consequently, the two arguments of = must have the same shapes, (or at least, as in the example of Y=2, compatible shapes). Otherwise an error results.
Indeed, 3 4 5 - 2 3
is an error. What is the technical definition of a "compatible shape"? Is it as simple as "same shape or a scalar", or is there more to it?
Solution
From the Dictionary page B. Verbs :
Agreement. In the phrase
p v q
, the arguments ofv
must agree in the sense that one frame must be a prefix of the other
So the following will all work
(i. 2 3) * (i. 2 3 4) NB. frame of left arg is prefix of the right arg
(i. 2) * (i. 2 3 4) NB. frame of left arg is prefix of the right arg
(i. 2 3 4) * (i. 2 3) NB. frame of right arg is prefix of the left arg
For a formal definition of frame see the Dictionary page A. Nouns
I'd recommend this section of J for C programmers as a thorough coverage of these concepts.