What is J? An introduction by Leroy J. Dickey University of Waterloo This article is intended to be an introduction to the language J and some of its features. J is a high powered general purpose programming language. This dialect of APL uses the ASCII character set, has boxed arrays, complex numbers, the rank operator, and some novel compositions of functions. Like APL, J is an array language. Any time one wants to do calculations with more than one number at a time, or with more than one name in a list, a collective (an array) is the right structure to use, and J is designed to do such calculations easily. Those who write programs for themselves and who want their answers quickly will be happy with the swift and concise way programs can be written in J. This article consists of several examples that illustrate some of the power of the language J. Each example presents input and output from an interactive J session, and a few lines of explanation have been added. Lines that are indented with four spaces are those that were typed in to the J interpreter, and the lines that are indented with only one space are the responses from J. Other text lines are comments that have been added later. The topics that have been chosen for inclusion do not come close to telling everything about J, but some of them represent subjects that at one time or another the author found new and exciting. J is a general purpose computing language, and is well suited to a broad spectrum of programming needs. Because the interests of the author are in mathematics, examples are primarily mathematical in nature. In this discussion about J, the words noun and verb are used to stand for data and function, respectively. Thus 'neon' is a noun, 1 2 3 4 is a noun, and + is a verb. One may assign data to a name, as in the statement alpha5=.'neon' , and a name (such as alpha5) used to refer to the data is said to be a pronoun. Similarly, one may associate a name with a verb, and such a name is said to be a proverb, (pronounced "pro-verb", not "prah-verb"). In the assignment plus =. + the name plus is a proverb used to refer to the verb +. There are also language elements called conjunctions, which join two verbs or proverbs, and there are elements called adverbs, which modify the meaning of one verb or proverb. Example 1: Calculating e This example shows how calculations are done with a list of numbers and how J uses a functional notation. That is, each verb (function) acts on the noun or pronoun (data) to its right. It is easy to create a collective (array). Here, for instance, are 9 integers: i. 9 0 1 2 3 4 5 6 7 8 a =. i. 9 a 0 1 2 3 4 5 6 7 8 The factorials of these numbers are: ! a 1 1 2 6 24 120 720 5040 40320 The reciprocals of the above: % ! a 1 1 0.5 0.166667 0.0416667 0.00833333 0.00138889 0.000198413 2.48016e_5 And the sum of reciprocals of the factorials of a is: +/ % ! a 2.71828 Those who know some mathematics may recognize an approximation to the number e, the base for the natural logarithms. Of course, J has other ways to calculate this number; the point here is the use of the natural left-to-right execution of the meaning of the sequence symbols +/ % ! a as "the sum of the reciprocals of the factorials of a". Those who have done some programming in almost any other language, will see that the expression +/%!i.9 is a remarkably short one for a function that produces the sum of the first few terms of the series that approximates e. Moreover, it is possible to pronounce this program in English in a way that precisely conveys its meaning, and those who know the meaning of the words in the sentence will be able to understand it. And anybody who knows the mathematics and who has learned this much J will recognize that the program does exactly what the English words say and that it will produce an approximation to e. The author finds this expression in J much easier to think about and understand than the corresponding program intended to compute the same number in certain older languages. Here, for example, is what one might write in Fortran, to accomplish the same result: REAL SUM, FACT SUM = 1.0 FACT = 1.0 DO 10 N = 1, 8 FACT = FACT * N SUM = SUM + 1.0 / FACT 10 CONTINUE PRINT, SUM STOP END Compare this Fortran program with the J program that uses only a few key strokes: +/ % ! i. 9 . Not only is the J program shorter, but if you agree with the author that the program more closely approximates the English description, then you will probably agree that the J expression is easier to understand, even for someone who may have been using an older language, and who has only just learned the meaning of the symbols used in this example. The use of this compact notation brings about much shorter programs. It is the author's experience that for some applications, the ratio of the number of lines of Fortran or C code to the number of lines of J code has been about 20 to 1. Example 2: Average In this example, two verbs (functions) are defined. As before, lines indented with four spaces were given as input during a J session, and the ones immediately after, with one leading space, were produced by the J interpreter. Sum =. +/ Average =. Sum % # c =. 1 2 3 4 Average c 2.5 The first two lines create proverbs, the third creates a pronoun, and the fourth invokes the proverb (function) Average with the pronoun (data) c. The meaning of Average c is this: (+/ % #) 1 2 3 4 and this may be thought of as: find Sum c, ( +/ c means add up the entries in c), find # c (tally c), and then find the quotient of those two results. The sequence of three functions used in the definition of Average provides an example of a fork. A fork is a sequence of three functions (f h g), where x (f g h) y means (x f y) g (x h y) . This diagram may help you to understand the meaning: g / \ f h / \ / \ | | | | x y x y and the figure might suggest to you why the name fork is used. In a similar way, used with one argument, (f g h) y means ( f y) g ( h y). Thus, looking again at Average c, we can see that this has the same meaning as (Sum c) % (tally c). If the above definition of fork strikes the reader as awkward, the reader is invited to consider a thought experiment about the meaning of the English phrase "a less than or equal to b". Imagine that there is a function LT which returns the value 1 when a. ... i also believe that the rank operator is a wonderful invention, up there in the top ten computer inventions of all time. Conclusion: In this article, only a few of the many verbs in J have been used, but the reader has had a glimpse some of the power of J, as seen in hooks and forks, in the implicit definition of verbs, and in the uniform way that J acts on sub-arrays by means of the rank operator. J is powerful because one can get results quickly. It is a carefully thought out and beautifully consistent programming language for people who need to write programs for themselves, but who don't want to spend inordinately large amounts of time programming to get those results. If you have to write programs for others, this could still be a language for you, especially if you get paid by the job, and not by time. Availability: J is available by anonymous ftp from watserv1.waterloo.edu in the directory "~/languages/apl/j" and is available from certain other servers. At the Waterloo site, versions of J are available that run on several kinds of computers. J is a product of Iverson Software Inc., 33 Major Street, Toronto, Ontario, Canada M5S 2K9. (416) 925 6096, and when purchased from them, comes with a dictionary of J and other aids for learning J. Learning J: Because J is a relatively new language, there are not yet many books or articles about it. At the Waterloo site, there is a tutorial that has much material concentrated into a small space. With each release of J to date, there has been a status file that tells which features have been implemented. Several status files are available. The reader who chooses to learn J from these materials might like to print the 47 tutorial files and study them one at a time, and to have at hand a copy of a status file listing the names of the verbs. For each example given in the tutorial, the reader might wish to experiment with J by inventing nouns, verbs, adverbs, and conjunctions to test ideas related to those presented in the tutorial and thereby to let J itself be the guide and the interpreter (pun intended). The author of J: The inventor-developer of J is Kenneth E. Iverson, the same man who invented APL, a pioneer of computing, and recipient of the prestigious ACM Turing Award. Some years ago, he retired from IBM and went to work for I.P.Sharp Associates, a Toronto company that developed the world's first private packet switching network, the finest APL available, and was at one time the world's largest time sharing service which just happened to be devoted entirely to APL. The descendent of this company, Reuter:file, still has offices in many of the world's major cities. Another descendant, Soliton , still has its head office in Toronto. Now Iverson is retired (again) and lives in Toronto, where he spends his time developing and promoting J. The implementor for J is Roger Hui. Roger hails from Edmonton, and has lived in Toronto since he joined I.P.Sharp Associates in 1975. The source code for J, written in C, is worthy of some comment, because it makes extensive use of macro expansion. Its style, influenced by the work of Arthur Whitney of Morgan Stanley in New York, reflects Roger's strong background in APL. Today, Roger spends much of his time developing J and does occasional contract work in APL. Copyright (c) 1991,1992, 2000 by Leroy J. Dickey Permission is granted to use this for nonprofit and educational purposes. It may be given away, but it may not be sold. Revised 1992-03-21. Revised 2000-04-04.