This was a WIP post dated August 2nd, 2017 that lived on my Project Euler solutions page. I took it down to comply with their request not to post solutions on the public web. Since this post offers no solutions, I’m putting it back up. I had intentions to make a longer guide to J (as well as to finish this guide), but life got in the way (read: I got distracted). I think this is interesting/helpful enough on its own, though.
I also updated its tags to add
eso-lang (as well as the tags on my post about
Project Euler). I don’t really think of J as being an esoteric language the same
way I think of ><> and others, but I think most programmers
would group the two together. Tags should be useful for the average person, not
for me, so I added it.
Things You’ll Often See in J
This guide is meant to be a brief tour of what you might expect to see in an answer in J. This is by no means a comprehensive overview of the language, but it’s meant to cover some simple things that I might not explain too in-depth in my comments (mostly because repeating explanations isn’t fun). Important to note is that I will only be covering parts of the language that I commonly use.
If you want to know more about particular parts of the language, refer to NuVoc which usually does a great job of explaining what each primitive does (it also has some idioms). Learning J and other guides on jsoftware are also resources for learning the language. The IRC channel #jsoftware, though often inactive, has been very helpful to me, as has Stack Overflow (remember to use the [j] tag for questions about the language).
Functions in J are referred to as verbs. Functions which take as input verbs and return a new verb are known as adverbs. Values (integers, arrays, etc.) are known as nouns. Control structures, for lack of a better term, are called conjunctions. They do things like compose verbs with each other (
&:) or execute a verb a certain number of times (
Verbs can be monadic or dyadic.
Monadic means “taking one argument,” which is the right argument. Monadic verbs are also known as monads. Dyadic means “taking two arguments,” which are the left and right arguments. Dyadic verbs are also known as dyads.
In J, most verbs have monadic and dyadic forms, so I’ll specify which form I’m referring to.
Atoms are the individual elements of a list. In arrays, they’re either numbers or single characters. In boxed lists, they’re the lowest level of boxing.
Comments in J start with
NB. and go to the end of the line. NB. stands for Nota Bene, roughly “take note of,” and is commonly used in comments in academia.
In J, primitives are usually one to two characters long. The colon (
:) and dot (
.) characters are used to form two-character primitives, usually with similar functions to the original character.
For example, dyadic
> is Greater Than, dyadic
>. is Larger Of (Max), and dyadic
>: is Larger Or Equal.
Often times spaces may be omitted in a J program, so it might get easy to confuse these characters for conjunctions (since that is what they’re listed as in the vocabulary), but for the most part they serve as a solution to the limited number of ASCII characters.
Much like in the interactive interpreter, any code with three spaces of indentation is the input and code without indentation is the output.
Although I’ll try to be explicit, the typical arguments in J are
x for the left argument and
y for the right argument, e.g. for a dyad
x f y.
Numbers and the underscore
- is a verb in J, the underscore (
_) is used to represent negative numbers. Get used to seeing
_3 instead of
-3 and typing it too, since the two have very different meanings in some cases. Moreover,
_ on its own represents infinity, which means that
__ is negative infinity (yeah it’s not easy to tell that it’s two underscores). Finally
_. is indeterminate, though you ought not see that often.
Sum, product, and other insert idioms
You’ll often see me write that
+/ is Sum or
*/ is Product. Knowing that there are primitives with two-character names, you might think that
/ is just used to allow for more names. In fact,
/ is an adverb: Insert.
Given an array, it inserts the function before it between each atom and then evaluates the result.
+/ 1 2 3 4 5 15 1+2+3+4+5 15 */ 1 2 3 4 5 120 1*2*3*4*5 120
With boolean arrays, inserting Or (
+./) and And (
*./) give Any and All respectively.
+./ 1 1 0 1 1 *./ 1 1 0 1 0
Inserting Max (
>.) or Min (
<.) gives the overall max or min of an array respectively. You can see that because J evaluates in order from right to left, it carries with it the maximum or minimum in each comparison until it’s compared to all the elements in the array.
>./ 2 5 7 1 3 7 2 >. 5 >. 7 >. 1 >. 3 7 1 >. 3 3 7 >. 3 7 5 >. 7 7
Note that this section only covers the monadic form. When the verb modified by insert is given both a right and left argument, it pairs every atom of each argument together and applies the verb to these pairs.
0 1 2 +/ 1 2 3 1 2 3 2 3 4 3 4 5 0 + 1 2 3 1 2 3 1 + 1 2 3 2 3 4 2 + 1 2 3 3 4 5
- Reflex/Passive (
- Power Of (Do while, Collect in array, Converge)
- Convert to string
- Split to digits