This may come as a surprise, but I will today write about a programming language for which no Free Software implementation exists, the J programming language. J does not stand for the J dirty word (Java), but is the full name of the language.
I first heard about J while solving some problems on the Project Euler challenge. One of the problems to be solved was “given that C(n,p) is the number of ways to take p items amongst m items, find the number of C(n,p) greater than one million for n between 1 and 100 inclusive.”
This was a very easy problem. I coded my solution using Python and, by curiosity, looked at how other people did it. I saw someone mentionning its J solution:
+/1e6< ,!/~>:i.100
Wow, 17 characters! As I was in disbelief, I gave it a try and installed a copy of J on my GNU/Linux laptop. I cut and pasted the expression, and it gave me 4075, the expected solution. Let me describe how it works.
First of all, expressions are evaluated from right to left.
i.100
will generate a list of all integers from 0 to 99 (the 100 first non-negative integers). Then, the increment operator is used, so the part
>:i.100
leads to the list of numbers from 1 to 100. The rank of the increment operator is zero, which means that it operates on individual items, not on lists. Since we give it a list as argument, each member of the list will be incremented, and give the new 1 to 100 list.
The C(n,p) operator in J is written as
p ! n
However, we want to apply it for every p and n in 1 to 100. The tilde (~) modifier means that we want the left argument of the ! function to be the same as the right argument. The slash (/) modifier means that we want to build a table. Without it, the ! operator would be applied to the first item of the left argument and the first item of the right argument, then to the second item of the left argument and the second item of the right argument and so on. Here, it is applied to every combination of its left and right arguments, meaning that the result is a 100×100 table:
!/~>:i.100
Now, the coma (,) transforms the 100×100 table into a 10000 elements flat list:
,!/~>:i.100
Now, we want to select all the elements greater than one million. For that, we will compare each element to 1e6 (one million in scientific notation). Since the left argument is a plain number and the right argument is a list (the 10000 long list we obtained), the result will be a 10000 items list containing either 0 (false) or 1 (true):
1e6< ,!/~>:i.100
Ok, so we now have a 10000 items list containing 0 (smaller than one million) or one (greater than one million). To find out the number of ones, we only have to sum up the items. Using the plus (+) operator and the slash (/) modifier, an addition will be inserted between every item of the list:
+/1e6< ,!/~>:i.100
That’s it.
To the computer literate people, it looks a lot like APL without the special characters and the need for a special keyboard. No surprise, J and APL have both been designed by Kenneth E. Iverson.
J is very efficient while working on large sets of data. Its table operator and its rank analysis almost entirely obviates the need for explicit loops. For example, if you want to multiply the 100 first prime numbers, you can use the p: operator which returns the n\super{th} prime number (starting with 0, which returns 2, 1 returns 3, 2 returns 5, 3 returns 7 and so on):
*/p:i.100x
The x modified asks for extended (read unlimited) integer precision. You want to know what the solution is? Install your copy of J and try it yourself.
Related posts:


During my first encounter with J I succeeded in shortening the
+/1e6< ,!/~>:i.100
program by more than 10%.
+/1e6<,!/~i.101
Please notice, that !/~>:i.100 doesn’t use the
complete table of binomial coefficients (i. e. Pascals’s triangle)
Example:
!/~>:i.10
1 2 3 4 5 6 7 8 9 10
0 1 3 6 10 15 21 28 36 45
0 0 1 4 10 20 35 56 84 120
0 0 0 1 5 15 35 70 126 210
0 0 0 0 1 6 21 56 126 252
0 0 0 0 0 1 7 28 84 210
0 0 0 0 0 0 1 8 36 120
0 0 0 0 0 0 0 1 9 45
0 0 0 0 0 0 0 0 1 10
0 0 0 0 0 0 0 0 0 1
in contrast to:
!/~i.11
1 1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 10
0 0 1 3 6 10 15 21 28 36 45
0 0 0 1 4 10 20 35 56 84 120
0 0 0 0 1 5 15 35 70 126 210
0 0 0 0 0 1 6 21 56 126 252
0 0 0 0 0 0 1 7 28 84 210
0 0 0 0 0 0 0 1 8 36 120
0 0 0 0 0 0 0 0 1 9 45
0 0 0 0 0 0 0 0 0 1 10
0 0 0 0 0 0 0 0 0 0 1
regards
Gregor Lingl
Hi i am having a bit of trouble with a program i am working on. Basically i have 2 function getInxSmallest and getInxLargest they take in an array on numbers and reutrn the position of the largest or lowest depending on which function you use. Now i must must create a function that returns the position of the smallest number in the array when a 0 is entered and the largest when a 1 is entered.
for example lets say you create an array v1 =: 5 2 1 3 when 0 FUNCTIONNAME v1 is entered the answer should return 2 as the smallest number 1 is in position 2. Can you please help i would greatly appreciate it.
You can define it as an adverb easily: (which does what you ask, including embed your getInxSmallest and getInxLargest functions)
I’ll let you analyze the adverb.
Thank you very much you’re a life saver.
Hi I am back again. Just one small question, in order for me to muliply a column i change it to a row using |: as i want to use them all at once. So my column 1
0
2
4
is changed to 1 0 2 4. It works but I am just wondering is there not another way to do this? Given that I am only a novice with J I am sure there is. Can you please help I would really appreciate it.
Hi Samuel Thanks for all your advice so far. My project is due shortly on J but i an having one last error. The folllowing line returns a domain error and i cannot understand why.
getIHW =: ( (0 {:: NetParams) ,&> (1 {:: NetParams)) $ ((0 {:: NetParams) * (1 {:: NetParams)) {.!.0 [
Can you please offer some advice for one last time i would grateful.
Hi, I discovered J a few years ago and am quite impressed. Sadly, as is mentioned, “…no Free Software implementation exists.” I am contemplating writing an open source version of J. However, before I start this Herculean task, does anybody know if an effort to produce an open source J has already been started?
According to:
http://www.byte.com/art/9603/sec11/art10.htm
The public-domain version 7 of the J programming language. BYTEmark is a collection of 10 tests that exercises the CPU, FPU, cache, and system memory; J is a complete interpreter that is the last public-domain release of the J language’s source code prior to J’s becoming a commercial package.
That is a pretty good solution. I have never used J before and it looks totally different from all the languages I’m use to.
But it also looks like it would be useful for some problems in specific domains.
Why does the code read right to left? Is there a good reason?
“J is very efficient while working on large sets of data.”
Uh, no, it isn’t. J and APL share the same damning problem: they see the world as arrays, so when it comes time to do anything that isn’t array bound, you get ground to a halt. That’s why they’re both dead languages.
Please don’t say things like this when you’ve never benchmarked them. J isn’t even particularly efficient in the few data structures it knows how to do. Things like C, Forth, Occam, Coq, Haskell and Ocaml burn all over J in efficiency.
Also, that “table operator” is called an array comprehension, the word you wanted was “obviates” (obliviates means “to make ignorant”), and most of these things can be done equally densely in normal modern languages.
Examples stop looking so dense when you move to non-trivial things, where saving half a dozen characters on a way to express something begins to take a back seat to logic.
Maybe save the swooning until you’ve done something in J, so that you’ll understand why there are no experienced J programmers who recommend it.
Harry Reed: don’t waste your time. There are public domain Js, and the reason they’re not developed is that J and APL aren’t particularly expressive. Whereas yes, their syntax and notation are surprisingly dense, and well suited to trivial math toys, the fact of the matter is that expressing complex data structures is borderline impossible in J; you can’t so much as put together fundamental basics like doubly linked lists and hash tables, let alone the aggressive datastructures you need to appropriately represent real problems. And dear god help you if you need to express an algorithm more complex than quicksort (which is meant as a low water mark.)
J’s notation is poorly defined, there are no acceptance tests, et cetera. It isn’t for lack of a free implementation that J has failed; that didn’t stop other non-free languages like Delphi, LISP (in its heyday), Prolog (in its heyday and arguably today, since no free Prolog passes the prolog acceptance tests), et cetera.
The reason J failed is because it confuses the ability to tersely express math with the tools software developers actually need.
Find yourself a problem that J looks like a good candidate for, but which Occam, Haskell or Ocaml don’t look better for, and you’ll be a magician. APL was a good idea because it used actual math symbols, which allowed mathematicians to write simple math software without becoming programmers.
Taking those symbols away just means it’s useless.
Seriously, you could be looking into Clean, Erlang, Mozart-Oz, Fortress, Factor, Haskell, C++, Clojure, the list goes on. Why waste your time on J?
Or maybe better said, “if you like J and APL, the programming language you’re looking for is called Mathematica.”
j parses right to left for 2 main reasons. 1 rpn is terser, 2. the parser can know what to do at any point by looking at most up to the 4 rightmost words.
the speed is good. It is a fully dynamic language so good is relative to other dynamic languages.
IMO, data structures is a huge strength of J. Weaknesses, IMO, are the maintainers refusal to add enhancements useful for DSLs, and a lack of batteries.
John Haugeland said:
“J isn’t even particularly efficient in the few data structures it knows how to do.”
“… J and APL aren’t particularly expressive. Whereas yes, their syntax and notation are surprisingly dense, and well suited to trivial math toys, …”
Regarding efficiency, IBM’s APL compiler from the mid 1980’s actually beat their optimizing “H” FORTRAN compiler on boolean array manipulation (presumably because it could “see” the forest of an algorithm expressed in APL better than the “trees” expressed in FORTRAN-style nested loops). So I would be cautious before assuming that J can’t be implemented efficiently.
Regarding paucity of data structures, APL 2 at least has nested arrays, which can make up for some of the blinders (I don’t honestly know if J has them or not). One could just as easily toss tomatoes at LISP and Scheme for “only” having S-expressions, or at Haskell for “only” having algebraic data types (although neither of these is quite true). I have actually heard exactly this done over the years in various venues. Of course for real-world problem solving, one would like to have the widest variety of concepts that can fit comfortably in one’s paradigm, but there is great value in exploring ways of expressing things with a “restricted” set of concepts and language. (E.g., pure functional style, or logic programming, constraint-based, etc.)
But I find your “trivial math toys” remark especially ironic: many, many people would level the same criticism against Haskell (and Clean and Erlang and others you mention as well). Many, many people did, especially a couple of years ago, before STM, RWH, etc.
Can’t we just leave it at “J is interesting, fun, and will change your perspective on what’s possible”? As will Haskell, Clojure, Scheme, Erlang, etc., albeit in different ways.
Why rain on someone’s parade as they experience the joy of a new language? Why not be more charitable? (Hey, Sam, Joy and Charity are another fun couple of languages: check ‘em out!)
I guess I just find the “X isn’t a REAL programming language” kind of talk counter-productive and a little tiresome, independent of the X involved. I’ve heard it said about APL, LISP, C, SML, Scheme, Haskell, Java, Miranda and lots of others over the years, and I’ve enjoyed using all those languages in their own way.
Can’t we leave this kind of “trash talk” to the high school football crowd, or their “grown-up” counterparts, the war and religion crowds? Do we really have to constantly hear this kind of stuff from the programming language geek crowd?
(Yeah, I know: me and Rodney King, both. I’ll get off my own soapbox now
. And nothing personal, John, everyone seems to do it … .)
Unless J is open sourced, it’ll remain obscure.
“J is very efficient while working on large sets of data.”
John Haugeland said:
“Uh, no, it isn’t. J and APL share the same damning problem: they see the world as arrays, so when it comes time to do anything that isn’t array bound, you get ground to a halt. That’s why they’re both dead languages.”
John,
I routinely perform queries and calculations on huge amounts of data using J. The performance of J has changed the game for me.
No longer do I build large databases with queries that take minutes or hours to run, now I can load the same data in J’s data engine (JDB) and run the same queries in sub-second times. In addition, I have the freedom to do manipulate the query results in ways that would be very difficult in other languages. I’m not saying that J is easy to learn, but the time you spend with J is time well spent.
John Hougland is exactly right. i’ve been saying this for many, many years (including at APL conferences). It makes little sense to use J or APL while they are dying when Mathematica is around and thriving.
APL and J are elegant intellectual structures. So is FORTH, and perhaps a hundred other special or general (e. g. MATLAB) tools. People should use them, for the same reason that some musicians should perform the works of obscure Baroque composers.
There are elegant natural languages, perhaps aboriginal ones in remote countries, that have unique powers for expressing human feelings and perception. The people who learn these will be rewarded, but within the small world of other people who speak these languages.
Mathematica is also beautiful. Moving from APL to Mathematica for me was something like leaving a lovely small town and coming to the city. It took awhile, but I can only go back home for short visits.
i wish wolfram had released the programming portion of Mathematica as a self-contained free language but he hasn’t and it has spelt the effective death of the language for practical use (i don’t believe that so-called prototyping languages will ever succeed, especially if scaling them up for large systems and for fast computations (preferably both simultaneously), means converting the prototype program to another language. this is especially true for languages like J and Mathematica which are not translatable in a straightforward manner ).
John Haugeland is an idiot. I normally don’t get involved in “language advocacy” disputes, but in this case he isn’t just slagging a fantastic programming language out of ignorance and bias and having an axe to grind, he’s trying to influence some other guy who is curious about trying the language out.
You see that as a problem?! HA!
1) you don’t get ground to a halt
2) many, if not most things, are “array bound” anyway
3) you could say that “non-array languages” suffer from the opposite problem – the minute you get into anything “array bound” you ground to a halt… yet you didn’t say that, did you?
4) efficiency arguments in the days of GHz processors, quad cores, and terrabytes of memory, are just silly. that’s assuming your efficiency argument was valid anyway, which it isn’t.
What a joke. I’d love to know what the rationale is for this statement.
A lie. I put together complex data structures all the time, for major projects no less (just finished a private electronic exchange – stocks, commodities, etc.) all in J, and done in 10% of the time and code it would have taken in a more “mainstream” programming language.
More lies.
I’d love to know the definition of “failed.” If “failed” means “didn’t become one of the 10 most popular programming languages,” yeah, it failed. I also don’t see why on earth anyone would want to use such a moronic metric. I could care less how popular a language is. It is irrelevant. As long as it is just popular enough to ensure that it continues to be developed (which is the case with J), it suits me just fine.
Really, at this point you are just making a fool out of yourself.