Fifty Shades of J/Preamble
Read this First!!
“Fifty Shades of J” is a collection of 50 essays which have appeared over a period of years as the J-ottings column in Vector. They have been updated and standardised so that all the code quoted runs on J806 and later. Although many of the essays are about basic features of J, it should not be regarded as a language primer, but rather as providing some thoughts, insights and worked examples for the user who has already cleared the first hurdles of learning J. A Principal Topics lists heads each essay, and most have concluding Code Summary and Script sections, which should prove helpful in giving a rapid view of the ground covered.
The essays are not printed in their order of appearance in Vector, but rather have been arranged and in some cases merged, so that the first sixteen each emphasise a single primitive or feature, the next five are about the underlying philosophy of J, and the remainder have some specific problem or problems as a starting point, and show how readily J can be used to address it. In some cases the original titles and emphasis have been changed, although I have resisted too much over-polishing in the belief that is might be better to retain something of the original spirit of discovery. Many of the essays have a mathematical slant which reflects that one of the originals objectives of J, like APL before it, was to iron out some of the ambiguities and anomalies of conventional mathematical notation by creating an immediately executable bridge between mathematical theory and practice.
APL and its descendants have always been regarded as array processing languages. However, I consider that it is more helpful to view J as primarily a list processing language for reasons which will become clearer as the reader proceeds through the essays. These are internally linked like this: Essay #5: Conjugacy and Rank for cross-reference, and also for the purpose of indexing. It is hoped that the index will help make the book a useful collection of phrases for the reader who is maturing into J. J has a formal vocabulary of English words which describe its symbol vocabulary, and which can be accessed from the Glossary linked at the top of each essay.
The J symbol vocabulary can be divided into sets, first those symbols with which most users are very familiar either because of their general usage in life, e.g. the symbols of basic arithmetic, or because they arise so frequently in using J. Those in the former set do not generally appear in the Principal Topics sections at the head of each essay, unless there are some special aspects of them which are illustrated in that particular essay. Typical members of this set are the arithmetic and logical verbs
+ - * % ^ ! (factorial) = < > >: and <: (dyadic) ~: (not equal)
The only unfamiliar forms are monadic * meaning signum, and monadic + meaning conjugate.
The symbols derived from the above for double, halve, square and square root, together with increment and decrement also fall into this category viz.
+: -: *: %: <: >:
as do the digrams +/ and */ for sum and product of lists (Σ and π in mathematics terms). The second set comprises the more specialised symbols which are often harder to pick out in the strings of characters which make up typical J expressions.
Series, that is lists, are an essential part of the discourse of mathematics, and it is assumed that the reader has grasped the important distinction between the joining verbs ravel (,) and link (;). tally (#) and reverse (|.) are also necessary elements of any programming language which admits strings. head/behead and tail/curtail are desirable additions to the basic set, although a degree of memorisation is required before it becomes ingrained that in J {. and {: mean “take just one”, }. and }: mean “take all but one”, dot means “from the left” and colon means “from the right”.
Intelligent reading of J requires an appreciation of verb composition which can be implicit, that is hook and fork, or explicit as in the various forms of @ and &, that is the conjunctions at, atop, bond, compose, under, appose. These are of such wide occurrence that they are dealt with in Essay #2: A Composition on Composition and thereafter do not appear in the Principal Topics lists. Linked with these is ~ (passive conjunction) which is often manifest in what I term the ‘bridge hook’ u~v. These symbols do not feature in Principal Topics, instead they are comprehensively described in Essay #2: A Composition on Composition.
The items in lists may be numbers, of which there are six convertible representations (see Essay #14: j is complex? You bet!), characters as given by a. (alphabet), or lists. Whenever lists consist of lists, it becomes essential to be aware of rank (see Essay #5: Conjugacy and Rank and Essay #6: Punctuation and Rank), that is for the user to be consciously aware of depth within a hierarchy of lists. Rank is a consideration for all but the simplest operations, from which it follows that fluency in using the rank conjunction (") is assumed. This is used in some way in almost half of the essays, for which reason it does not always appear in Principal Topics lists.
There are two defined adverbs which are used repeatedly and are part of the J standard library, namely
every=.&> each=.&.>
whose definitions are not repeated in individual essays.
Also the power conjunction pervades many essays – roughly speaking it provides in a single diagram a style of looping activity which is characteristic of most computing languages.
By contrast with At Play With J where Gene McDonnell typically took a substantial problem, and solved it, sometimes at some length, my aim has been, particularly in the first set of essays, to work outwards from what is already an algorithmically rich base, and by looking at primitives in detail, show ways of making them even more useful and interesting. Gene and I frequently exchanged transatlantic e-mails, and I like to think that his top-down approach is complemented by my more bottom-up one of broadening the possibilities available through J’s basic elements.
When J first appeared, its aficionados were carried away to some extent by enthusiasm for tacit programming, just as APL users were entranced by the ability to write one-liners. Tacit programming used uncritically can lead to long and incomprehensible lines which defeat Ken Iverson’s initial objective of a computing language which reflected the styles and usage of natural languages, thereby making their meaning that much clearer than the statements of more conventional computing languages. As a broad rule, once ‘pure’ lines exceed around seven or eight characters, it is usually better to consider defining a new named verb or adverb, if necessary building up a chain of mixed new names and J primitives to achieve a final overall objective. Also even working within the ‘seven or eight’ character rule, over-enthusiasm for tacit programming can sometimes be less clear than explicit definition using x and y as left and right arguments. In the ensuing pages, I have tried to use whichever seemed to me the clearer style in each individual circumstance. Thus, as well as code quoted being executable by copying, it should also be self-describing.
For this purpose the Code Summary at the end of each essay contains the main verbs, adverbs and nouns in a hierarchical order indicated by indentation. This is in contrast to the ‘bottom-up’ fashion in which they have generally been developed in the text of their essay. Broadly speaking Principal Topics describe what the essay sets out to do and the J features which required to help get there, the essay body describes the process of getting there, and the Code Summary consolidates the final point of arrival. For sufficiently fluent J readers the last is all the information they need! (see Essay #21: Are you thinking what I’m thinking?). There is also a Topics Index and a Vocabulary Index which reference essay numbers rather than page numbers. Broadly speaking the former lists topic areas which have seemed to me to be amenable to J treatment, while the latter covers applications suggested by the J primitives themselves.
Many of the essays were triggered by other articles in Vector. Where possible, such references to these are linked within the text. J expressions executed in the terminal are shown in pre-formatted sections. Where J expressions appear within text, they are in fixed-pitch font.