From J Wiki
Jump to navigation Jump to search

⬅ Return to 'Welcome to J'

⬅ Return to 'NuVoc Reference'

⬅ Return to NuVoc

The Essential Ideas of J

What is a program in J?

J programs are made up of J sentences, which are single lines of text made up of words.

What are the types of words?

A word is either a name, a primitive, a constant, or punctuation.

Can you give examples?

Names: i, quicksort, data1, name_a_. Primitives: +, %:, i., {::. Constants: 'a', 'abc', 1, 1e6 3, _1.25. Punctuation is: =:, =., ', (, ), {{, }}, and also NB. which introduces a comment that extends to the end of the line.

Why are {{ and }} punctuation but not [ or }?

[, ], {. }, and " are not paired. They are individual words. The only paired words are the punctuation {{ which pairs with }}, and ' which pairs with another ' to define a literal constant.

What are the delimiters between words?

Words can be separated by whitespace. No whitespace is needed before a non-alphameric ASCII character or after an inflection.

What gives the value of a word?

The value of a primitive is defined by the language in NuVoc; the value of a constant is given by the rules of constant formation; the value of a name is the value most recently assigned by the execution of name =: value or name =. value. Punctuation has no value.

What kinds of values are there?

Every value is of one of the four primary parts of speech: noun, adverb, conjunction, and verb. Modifiers comprise adverbs and conjunctions. A noun holds data, and corresponds to a variable in other languages. A verb operates on one or two noun arguments to produce a noun result, and corresponds to a function in other languages. A modifier operates on one or two noun or verb operands to produce a result of any part of speech, usually a verb that applies the operands in a predefined pattern.

How is a sentence executed?

Execution of a sentence proceeds from right to left, depending on the parts of speech of the words encountered. When an executable fragment is encountered, comprising an executable value (i. e. adverb, conjunction, or verb) along with its operands, the fragment is executed and replaced in the sentence by a single word holding the value of its result. An adverb is executable when it has a noun or verb to its left; a conjunction when it has a noun or verb to its left and right; a verb when is has a noun to its right. If a verb has nouns to its left and right, the executable fragment comprises the three words and executes the dyadic valence of the verb; if a verb has a noun only to its right, the executable fragment comprises those two words and executes the monadic valence. After an executed fragment has been replaced by its result, the modified sentence is rescanned for its rightmost executable fragment. Verb executions associate right-to-left. Modifier executions associate left-to-right, causing the result verbs of a long compound to execute in right-to-left order.

How are nouns described?

A noun is either an atom or an array. An atom is a single number, character, or box. An array comprises 0 or more atoms arranged along any number of axes. The shape of a noun is the array of the lengths its axes. The rank of a noun is the number of its axes. A k-cell of an array is a subarray of rank k (or if -k, a subarray whose rank is k less than the rank of the array). An item of an array is a _1-cell of the array. An atom has one item, itself. An array of rank 1 or 2 is called a list or table respectively. An array can be referred to as a list of its items. An array containing no atoms is said to be empty; it must necessarily contain a 0 in its shape. Each atom in an array of length k can be uniquely specified by an index list of k numbers indicating positions along successive axes. An incomplete index list specifies a (-k)-cell of the array.

How are verbs described?

The operation of a primitive verb is defined in its NuVoc entry; for other verbs in the verb's value. In addition, a verb has three verb ranks, one for its monadic valence and two for its dyadic, giving the maximum rank supported by the corresponding argument. A verb presented with an argument whose rank exceeds the verb rank will be executed repeatedly on argument cells of the maximum rank. In such cases the surplus shape of the argument is called the frame and the results of the repeated executions are assembled into an array whose shape begins with the frame. If the execution is dyadic, the frames of the two arguments must agree in that the shorter must be a prefix of the longer; during execution the cells of the shorter are repeated as needed.

What is meant by the result of a sentence?

When all the fragments of a valid sentence have been executed, only one word remains, and its value is the result of the sentence. The result of a sentence typed at the console is displayed unless the last fragment executed was assignment to a name. [It follows that when a single word is typed as a sentence, its value is displayed immediately.]

Can a program be longer than a single sentence?

Sentences may be grouped into explicit entities by enclosing them in {{ }}. The entity so defined may be of any primary part of speech and may optionally be assigned to a name. Before an explicit entity is executed, a private namespace is created for it into which the arguments are assigned as the initial values of special names: u is assigned the value of the left operand of a modifier, v the right operand of a conjunction, y the right argument of a verb, and x the left argument of a dyadic verb. If u or v is assigned a noun value, the same value is assigned to the corresponding name of m or n.

These one-letter names (m n u v x y) are used throughout J documentation to refer to the arguments of any executing entity, not just explicit ones. Assignments using =. while an explicit entity is running are assigned to names in the private namespace of the executing entity. Names in a private namespace are inaccessible outside the entity they were assigned in.

Can a program contain loops?

Sentences of an explicit entity are executed one by one under the direction of control structures: patterns of control words each of which is a sentence delimiter. The result of the last sentence executed becomes the result of the explicit entity.