← Back to context

Comment by gcanyon

4 months ago

There's an initial learning curve as there is with any new programming paradigm (which everyone who works exclusively in C-like languages forgets that they paid a long time ago) but there are only something like 100 primitives, and then once you understand how they interact, the world opens up and the volume of a sphere being

Vs=: 4r3p1"_ * ] ^ 3:

just reads normally. It's been over ten years since I did anything in J and I can almost explain that now...

Well... in a sibling comment another person writing a "how it works?" question, and you writing an answer -- only proves my point :)

> Vs=: 4r3p1"_ * ] ^ 3:

What's the "_ for?

  • Vague recollection, could be wrong -- in J, you can apply a "rank" to verbs, which determines how the verb applies to its arguments. So for example, if we define that average verb:

    avg =: +/%#

    then we can say

    avg 2 4

    and get 3

    We can also say:

    x =: i.2 3 4

    and that makes an array like so:

       0  1  2  3
       4  5  6  7
       8  9 10 11
    
       12 13 14 15
       16 17 18 19
       20 21 22 23
    

    And then we can do things like:

          avg"1 x
       1.5  5.5  9.5
       13.5 17.5 21.5
          avg"2 y
       4  5  6  7
       16 17 18 19
          avg"3 y
       6  7  8  9
       10 11 12 13
       14 15 16 17
    

    In this instance I believe the "_ is just giving back 4r3p1 = 4.18879 for each of the arguments handed to the rest of the verb. This is one of those things that is second nature once you internalize J, but so so foreign until you do.