No history yet

J Language Fundamentals

J's Lexical Universe

J treats programming like a language with its own grammar and parts of speech. Instead of thinking in terms of variables, functions, and operators, you'll work with nouns, verbs, adverbs, and conjunctions. This vocabulary is the foundation for J's famously concise and powerful expressions.

  • Nouns are the data. They are arrays of numbers, characters, or boxed data. Think of them as the subjects and objects in a sentence.
  • Verbs are the functions. They perform actions on nouns. Examples include + (plus), - (minus), and * (times).
  • Adverbs modify verbs. Just as an adverb in English modifies a verb (like "run quickly"), a J adverb changes a verb's behavior. For instance, the / adverb turns a verb into an accumulator, applying it between the items of a noun. So, +/ becomes the verb for 'sum'.
  • Conjunctions combine two parts of speech (usually two verbs) to create a new verb. For example, the @ conjunction (At) applies a verb to a specific part of a noun.
   avg =: +/ % #  NB. define a verb 'avg' to calculate the mean
   avg 1 2 3 4 5  NB. apply the verb to a list of numbers
3

Evaluation and Valence

Unlike most programming languages that evaluate from left to right, J evaluates everything from right to left. There is no operator precedence; the function on the right is always applied first, and its result becomes the right-hand argument for the next function to its left. Parentheses work as you'd expect, forcing an expression to be evaluated first.

For example, 3 * 4 + 2 in a typical language is 14. In J, it's evaluated as 3 * (4 + 2), resulting in 18.

   3 * 4 + 2
18
   (3 * 4) + 2
14

J verbs also have a concept called valence, which refers to the number of arguments they take. A verb can be monadic (taking one argument, to its right) or dyadic (taking two arguments, one on each side). The same symbol can represent a completely different function depending on its valence. This duality is a core feature of J's design, packing a huge amount of functionality into a small set of symbols.

A verb's is determined by its context. If it has a noun only to its right, it's monadic. If it has nouns on both its left and right, it's dyadic.

SymbolMonadic Meaning (One Argument)Dyadic Meaning (Two Arguments)
*Signum (*1 0 _1 -> 1 0 _1)Times (3 * 4 -> 12)
%Reciprocal (%2 -> 0.5)Divide (8 % 2 -> 4)
#Tally (#1 2 3 -> 3)Copy (2 # 'ab' -> 'aabb')
$Shape Of ($2 3$'a') -> 2 3Shape (2 3 $ 'abcdef' -> ab..)

Naming and Vocabulary

Assigning names to nouns or verbs is done with a copula. The two most common are =: (global assignment) and =. (local assignment). Global assignments are visible everywhere, while local assignments are scoped to the current function definition.

Think of =: as defining a new word in J's dictionary. This is how you build up your own reusable tools.

   NB. Global assignment
   numbers =: 1 2 3 4 5

   NB. The name 'numbers' now refers to that list
   numbers
1 2 3 4 5

   sum =: +/
   sum numbers
15

J's built-in functions are called primitives. You can explore J's extensive vocabulary using suffixes. A suffix after a name indicates whether it's a global or local definition. These suffixes are also used with verbs to form special utilities. For example, f. gives you information about a verb, f: shows its definition, and u. or v. will show the definitions of an adverb or conjunction.

Let's test your understanding of these core concepts.

Quiz Questions 1/6

In the J programming language, what is the role of a "conjunction"?

Quiz Questions 2/6

How is the expression 24 % 6 + 2 evaluated in J, and what is its result?