Lucentbeing.com

The J Programming Language

…and you thought Perl was tough?

Back in the days when computers took up an entire room, programs had to be punched into the computer by hand, in binary code, by operators who were trained in this activity. I daresay that those operators enjoyed a sense of exclusivity in that they knew they could look at something which someone else thought incomprehensible, and recognize the underlying beauty.

Now, everyone and their aunt can program, and true geeks are left with a feeling of normalcy, with nothing to distinguish them from the rest of mankind.

Well, maybe that isn’t the best way to introduce a programming language, but it’s funny how our motives for these things work out.

Sometime back, I was posting and reading the forums at Project Euler (I forget which problem it was), when someone came along and posted “Here’s my solution!”, followed by something that looked like a cartoon character swearing (yes, worse than Perl). What was amazing was that this ‘solution’ was 10 characters long, when others were posting their prized C monster programs. The reply to this post was (not me) “Oh no, not another J solution!”.

After that, I noticed several others posting their solutions in this ‘J’ programming language, which almost never extended more than a line of 80 characters, when everyone else was writing pages. Intrigued by how a programming language could be so brief, I took a look.

The J Programming language is a programming language like none I’ve ever seen before. I downloaded the installer for Linux (all platforms are supported) and installed it. Installation was almost non-existent, only asking me for an installation directory.

J is an interpreted language, and the main way of interacting with it is to use the provided Java based IDE. However, I suggest you bookmark the locations of the html documentation, and use the console directly, as I found the IDE pretty damn slow.

Now, on to actually using it.

Baby Steps

J is a calculator:

NB. Anything starting with 'NB.' is a comment.
   2 + 3
5
   5 * 6
30
   20 % 2 NB. '%' is division, not '/'.
10

However, we start to run into problems as soon as we try something a bit more complicated:

   5*2 + 7
45

Wait…what? After some digging, I found that J doesn’t operate on the normal rules of expression evaluation. There are, in particular, 2 caveats:

  • There is no operator precedence. You have to use brackets to control evaluation.
  • All expressions are evaluated from right to left. Again, a departure from what we’re used to.

A handy way to remember this is: The right operand of an operator is the result of evaluating everything to its right. This helped me get through some of the more insane examples.

Now that we know what it’s doing, we can go back and try again:

   (5 * 2) + 7
17
   1 - 2 - 4 - 8 NB. '-' is binary subtraction, '_' is unary negation.
_5

Starting out with Verbs

J borrows terms from English grammar for its terminology. As such, a complete line is called a sentence, numbers, strings and the like are nouns, and anything that does an action is a verb. There are other parts of speech (as you are no doubt recalling from high school grammar), such as adverbs, adjectives, conjunctions and the like. J also has these, but I’m not getting into them right now, maybe in a later post.

Anyway, verbs do stuff. They are of 2 types, monadic or dyadic. That is, they take either one or two operands. You can define your own verbs, but the builtins ought to do you just fine for the time being. Let’s look at a few of my favorites.

The i. verb generates a list of numbers from 0 to the argument - 1.

   i. 10
0 1 2 3 4 5 6 7 8 9
   5 + i.10
5 6 7 8 9 10 11 12 13 14
   2 * i.10
0 2 4 6 8 10 12 14 16 18

Obviously, J knows how to handle lists, and manipulate them. Another one of my favorites is the q: verb, which gives the prime factorization of a number.

   q: 360
2 2 2 3 3 5
   _4 q: 2520
2 3 5 7
3 2 1 1

The second example shows the verb being used in dyadic form. Here, its left operand is a special modifier, which tells q: to give the factorization as a table, with primes in the first row, and corresponding exponents in the top row. From here, you can select the top row, the bottom, or whatever you want.

Thoughts

I think that J has become my 2nd favorite language, after Python. It’s interesting though, to see that they have opposite design goals: Python aims for readability, and J is anything but readable. Still, it gives a rush to be able to accomplish in one line what C programmers take fifty to do. J is fun to write, but hardly fun to read, and probably a horror to maintain.

J is meant for statistical and mathematical operations, and is exceptionally good at handling arrays. You still have string handling, and other general purpose stuff, but I severely doubt that I’ll be using J for that purpose. It also has the normal control structures such as if, else, for and while, but the ease with which the builtin verbs handle lists obviates the need to use such structures.

In fact, J lends itself to a specific style of programming called Tacit Programming, in which no variables are used to store intermediate values, and the result is obtained directly from the composition and application of functions to a given set of input arguments. Informally, this is the style of programming behind “one-liners”, programs that are cryptic, but do what they’re supposed to in one line. Sound familiar?

Obviously I’m not going to be able to do justice to all of J in one post, so I’m not even going to try. I might do some follow up posts, though. I’ll finish this post with a typically cryptic J program, and leave it for you to figure out what it does. :)

* / >: {: __ q: 360

blog comments powered by Disqus