I have been reading about the J programming language lately. It is quite different from many other languages, but I will try to cover the absolute basics of it, while, inevitably, much is left out. In the end, this article tries to give you a feel of the language and get you started, other resources are available for achieving higher proficiency in the language.
The language has very good resources available, so kudos for that. Freely available books include Learning J and J for C Programmers.
J is an offspring to the APL programming language. One of the most important changes from APL to J is that you can now use a standard keyboard (without any extra character map or similar trick) to type the code, as J, unlike APL, uses only pure ASCII characters.
J is a member of the array programming paradigm. Other notable paradigms J is a member of are the functional, function-level and tacit programming paradigms.
Just recently, in March 2011, J was released as open source software under the GNU GPL v3.
J is suitable for mathematical and statistical computation. It also happens to be good for code golf, as a bonus, because the language is very terse. J provides standard libraries, an integrated development environment, bindings to other programming languages and built-in support for 2D as well as 3D graphics. Plotting features exist, and I believe J could be used as a Maxima or Mathematica replacement in some cases.
J can be run interactively, which greatly helps while learning and/or trying out new techniques. It is recommended that you download and install J on your system and try out the material in this article in order to achieve a better understanding of the language.
Basic symbols
The +
symbol is used to add two numbers together, one on the right and one on the left, e.g. 8 + 4
. Spaces are optional (in this case), and can be used to increase clarity. Subtraction uses the -
symbol and multiplication *
. Division uses %
, not /
. C and most other common programming languages use /
for division and %
for modulus, but J doesn’t (|
is used as modulus in J).
You can chain symbols for more complex expressions, e.g. 2 * 3 + 4
. You might think that this would produce the result 10, as multiplication usually has higher precedence than addition, but J doesn’t have any precedence rules, and evaluates everything from the right to the left (this is also unlike mathematics, where evaluation happens from the left to the right when operators have the same precedence), thus, 2 * 3 + 4
evaluates to 14. Symbols lack precedence rules in J because J has hundreds of them, and it wouldn’t be fair to have programmers remember the precedence rules for all of them. You can, however, use parenthesis to change precedence just like in mathematics. To make 2 * 3 + 4
evaluate to 10, type (2 * 3) + 4
.
Negative numbers are prefixed with an underscore (_
), not a hyphen as in most other languages. 9 -4
will evaluate 9 subtracted by 4, while 9 _4
is a list containing the elements 9 and negative 4 (lists will be explained later).
Comments
Comments in J are started with NB.
and continue towards the end of the line. An example is NB. This is a comment
(don’t miss the dot in NB.
).
Monads and dyads
Monads are functions with values on just the right side, while dyads are functions with one or more values on both sides of the function. Symbols often have separated monadic and dyadic cases, where the symbol will act in different ways if the symbol is used as a monad or as a dyad. As an example, -
used as a dyad (x - y
) is the familiar subtraction symbol, but used as a monad it is the negation symbol (e.g. - 9 _4
) negates the two values, returning _9 4
.
Lists
Lists are one-dimensional arrays. A list can be constructed by placing values separated by spaces after each other, as in 2 9 4
. 2 9 4
is a list containing 2, 9 and 4. We can add something to each value of the list by typing 2 9 4 + 42
; here we add 42 to each item, producing a new list 44 51 46
. You can also type something like 1 2 3 + 4 5 6
, which will produce 5 7 9
(1+4, 2+5, 3+6).
To get the length of a list, use the #
symbol (as a monad), followed by a list.
Verbs and adverbs
J categorises many parts of the language into classes that are much like the classes of different natural languages. Among these are verbs and adverbs, which relate to functions. A verb is pretty much a normal function, such as +
. An adverb is a special function that takes another function from the left and changes the way that function gets applied. An example of such a function is /
.
/
takes a verb and then applies that to all the elements of a list. For instance, + /
can be used to sum the values of list elements as + / 8 4 2 4
. Likewise, you can multiply all elements together with * /
and so on.
Another example is ~
, which can be used to swap left-side and right-side arguments. For instance, 7 %~ 9
divides 9 with 7, not 7 with 9, as the sides the arguments resided on were swapped.
Assignment
J uses =:
for assignment (and =
, not ==
, for equality comparison). We can, for instance, assign the value 60 to the identifier x
, by typing x=:60
. Note that the equals sign comes first, which differs from the standard practice of putting the colon first; something that had me confused for a while.
Tables and multidimensional arrays
Using the dyadic $
symbol, you can construct multidimensional arrays. Arrays that have exactly two dimensions are called tables.
We can construct a table using 3 3 $ 1 2 3 4
. The numbers on the left side specify the length of the different dimensions. Each number in that array represents the length of a specific dimension, and the amount of numbers sets how many dimensions will be used in the array. With 3 3
, we create a 3 by 3 table. The numbers on the right specify the values which will be used as elements in the array. A 3 by 3 array would contain 9 elements, but we only specified 4 in the right-hand array. In that case, the array values we have will be repeated, forming the following table:
1 2 3 4 1 2 3 4 1
If we want to loop some values in a list, we can also use $
for that, e.g. 10 $ 1 2 3
, producing 1 2 3 1 2 3 1 2 3 1
.
Conclusion
J is definitely the coolest programming language I have touched recently (and among the languages I have recently played with include hyped languages such as Go, Haskell, Scala, D and Clojure, many which impressed me, but not as much as J). I look forward to learning more about J, and I hope you will do so as well.
I have not yet used it for anything non-trivial, and I will see how well it will do in such tasks, but so far, I’m pretty damn impressed.
Comments (307)