Thu Mar 17 09:20:12 PDT 2011

on being surprised by a programming language

Good news, everyone! J is now GPL licensed!

J is a legendarily brain-melting functional array programming language, in the vein of APL, but less stupid. It first came to my attention after James Hague wrote a glowing post regarding the "labs"; in-IDE programming tutorials.

I downloaded it and poked around a bit, but wary of wasting time on a proprietary, closed source programming language, I eventually dropped it. And that was the only reason, those of you sniggering in the back.

But an open source J blew away my only excuse, so I jumped back in. And dude. Bro. This shit is crazy! Let me show you a log.

── (13 of 15) Permutations ──────────────────────────────────
The dyadic function x{y indexes y by x  If p is a
permutation, p{y permutes y by p.
)
   
   p=: 22 ?. 22    NB. a random permutation
   p
0 18 10 16 15 21 8 6 14 9 19 3 4 5 2 13 7 17 11 12 1 20
   p { p
0 11 19 7 13 20 14 8 2 9 12 16 15 21 10 5 6 17 3 4 18 1
   p { p { p
0 3 12 6 5 1 2 14 10 9 4 7 13 20 19 21 8 17 16 15 11 18
   
   3 22$p
0 18 10 16 15 21 8 6 14 9 19 3 4 5 2 13 7 17 11 12 1 20
0 18 10 16 15 21 8 6 14 9 19 3 4 5 2 13 7 17 11 12 1 20
0 18 10 16 15 21 8 6 14 9 19 3 4 5 2 13 7 17 11 12 1 20
   {/3 22$p
0 3 12 6 5 1 2 14 10 9 4 7 13 20 19 21 8 17 16 15 11 18
   {/\3 22$p
0 18 10 16 15 21  8  6 14 9 19  3  4  5  2 13 7 17 11 12  1 20
0 11 19  7 13 20 14  8  2 9 12 16 15 21 10  5 6 17  3  4 18  1
0  3 12  6  5  1  2 14 10 9  4  7 13 20 19 21 8 17 16 15 11 18

(A note on notation, here: J arrays are space delimited, and in interactive mode, user input is indented, and the results are left justified. This quote is all from the tutorial, and is the IDE asking itself questions, then answering them. "22 ?. 22" generates an array of 22 numbers between 0 and 21 without repeating any, and "p=:" assigns that array to the variable p.)

At this point I was more or less completely lost. I'm about an hour into the tutorial by this point, and it's obvious that { is some kind of array operation, but I don't know what. So, because I'm stupid, I repeat what the machine did.

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

Hmm. I guess it wasn't kidding. I still don't know what it actually does, so I generate a 22 number array of ones, then hit it with the ole' {.

   ones =: 22 # 1
   ones
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
   ones { p
18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18

What the fuck? What if I do it the other way around?

   p { ones
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

What. I better check the arrays, to make sure the computer isn't randomly editing them just to spite me.

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

Wait.

Wait. It says it indexes them, right? What if it uses the contents of array X to fetch the number at that position in array Y? If that's what it's doing, then just giving it a single number should fetch the number at that location in the array!

   1 { p
18
   2 { p
10

Holy shit!

But it said it permutes them. Shouldn't fetching array elements at random skew the set?

   dstat p
sample size:             22
minimum:                  0
maximum:                 21
median:                10.5
mean:                  10.5
std devn:     6.49358657959
skewness:                 0
kurtosis:      1.7950310559
   dstat (p { p)
sample size:             22
minimum:                  0
maximum:                 21
median:                10.5
mean:                  10.5
std devn:     6.49358657959
skewness:                 0
kurtosis:      1.7950310559
   dstat (p { p)
sample size:             22
minimum:                  0
maximum:                 21
median:                10.5
mean:                  10.5
std devn:     6.49358657959
skewness:                 0
kurtosis:      1.7950310559

Holy shit! Holy shit! (At this point I literally slapped my hands to my face in surprise, and ran the statistics twice in shock) That's why it generated 22 numbers from 0 to 21 without repeating! It's just 0 through 21 in random order! Soylent green is people! Snape killed Dumbledore!

I've seen people nerdily lose their shit while programming before, but this was the first time it happened to me. It was a profoundly odd experience to be yelling excitedly about array math at five in the morning. You might want to try it out too.


Posted by | Permanent link | File under: important, nerdery