Essays/Realizations

From J Wiki
Jump to navigation Jump to search

These are some things that took me a long time to find out about J, because they were not apparent from anything I read. I write them here to possibly spare someone from having to find them out the hard way again.

Multiple dyadic ranks are not always redundant

Monadic verbs don't need more than one rank. What do I mean by this? You never have to write u"3"5 y or u"5"3 y, because you can just write u"3 y instead. In general, if u is a monadic verb and you apply two ranks to it like u"r1"r2 then you can always replace this with just a single rank like u"r. The new rank is the minimum of ranks after converting all ranks to nonnegative (for this conversion you have to know the rank of the noun you'll call the verb with).

On the other hand, if you have a dyadic verb, two ranks are not always redundant. Here's an example:

   'abc' <@:,"0 0"1 0 'de'
+--+--+--+
|ad|bd|cd|
+--+--+--+
|ae|be|ce|
+--+--+--+

There is no way to write just "r (for some r) instead of "0 0"1 0 there to get the same effect.

It's definitely helpful to write down those "Aha!" moments when learning J (and sharing them is a great idea). Out of interest, you could re-express the specific phrase above so:

'abc' <@:,&>"p. 'de' Note use of verb argument to ". Of course, we could write "1 0 instead of "p., but the latter is shorter.
'abc' ,&.>"p. 'de' Consolidate boxing logic (again, we could write this ,&.>"1 0). Note that &.> implicitly applies "0, hence the equivalence to "0"1 0 (frequently one can reduce a nested rank with judicious function composition)
'abc' <nowiki>|:@{@; 'de'</nowiki> More direct expression (semantically).

-- Dan Bron <<DateTime(2010-08-27T12:52:52-0300)>>

Don't forget 'abc' <@:,"0~/~ 'de' . The table adverb can often replace the outer rank in such expressions. -- B Jonas <<DateTime(2010-08-28T23:45:40+0200)>>

Good point. Combining our ideas, we can re-express this without explicitly using rank at all: 'abc' ,&.>~/~ 'de' . This is the shortest yet, and actually is the most natural expression of the idea. I would probably write it this way "in real life". -- Dan Bron <<DateTime(2010-08-30T12:37:49-0300)>>

 Maybe.  I'm not actually interested about how to write this expression here, but how to write expressions that would require two ranks in general, <@:, was merely a convenient verb because the output clearly shows what happens.  I would use something like 1 2 3 +"0"1 0] 10 20 but the problem is that + has a builtin 0 rank so the first one can be elided and I don't want to complicated the explanation.

(Two ranks might not be enough either, you need all the ranks in $ (i.1 3 5) 9"0 0"0 1"1 1"1 2"2 2"2 3 i.2 4 6. Needing two ranks, however, actually come up very often when I write code.)

Proverbs are verbs

(I will explain this one later.)

Only the local variables since the innermost explicit call are in scope

(I will explain this one later.)