Vocabulary/Words - J Wiki (original) (raw)

Back to: Vocabulary

Strings, Numbers and Names are all Words

The characters used to form words and sentences in J are:

Primitive objects in J, listed in NuVoc, are words of 1 to 3 characters, denoting various operations (verbs), quantities and/or texts (nouns), and related actions. Users may assign a name to an object which they create, according to these rules:

Each name is defined in one of several computation environments (or namespaces) known in J as Locales, which are identified by similar sorts of names, or by assigned numbers. Objects' names may be qualified by an attached suffix which is bracketed by underscores, designating a particular locale of operation. Absence of a name between the underscores implies the locale 'base'.

When a sentence is executed, J first converts it into words. Use the verb Words (;:) to separate the words in a given sentence:

sentence=: 0 : 0 'abc ''d'' efg' -:<name , 0 1 2+(z-1) NB. (comment) )

;: sentence +---------------+--+-+----+-+-----+-+-+-+-+-+-+-------------+ |'abc ''d'' efg'|-:|<|name|,|0 1 2|+|(|z|-|1|)|NB. (comment)| +---------------+--+-+----+-+-----+-+-+-+-+-+-+-------------+

Summary of word formation:

A name may not end in a single underscore unless it is a valid locative.

There is one tremendously important, counter-intuitive, special case:

The process of recognizing words, summarized above, can be seen in more detail at JDic:d332.


Sentences

The sentence is the executable element in a J script. Basically, it is one line of code.

Sentences cannot span more than one line. There is no "continuation character" as in Basic, and no "statement delimiter" as in Java or C.

The executable line is treated as a single sentence, unless it contains control words, when the control words and sections of the line between control words are each treated as separate sentences.

if. T do. sentence1 else. sentence2 end.

T (above) is called a T-block. Typically a T-block is a single "test" sentence, e.g. 0=#y , but it can consist of several sentences, i.e. a block of code.

The primitive (NB.) starts a comment. The leftmost NB. in a line causes the rest of the line to be ignored by the interpreter. Whatever stands to the left of it is the executable line.

1 + 1 2 1 + 1 NB. The executable line is the first 1 + 1 2 NB. You can use NB. to "comment out" a sentence inside a script, like this ... NB. 1 + 1 NB. ... J ignored that!


J Code, Strings, Lines, Sentences and Textfiles

Definition:

(assert CRLF -: CR,LF)

Textfiles are the most elementary kind of files used by today's computers to store data on disk. But there are many different standards for the simple textfile. Of these, just two are in common use:

CRLF, CR, LF are

Internally, J uses the UNIX standard (LF-separated).

A noun containing J Code is typically a string separated into lines by "newline" ("linefeed") characters (LF). This holds true also for verb definitions which you've asked J to convert into a noun, e.g. by using Foreign (5!:5).

More precisely, J Code is a list of bytes, separated into individual lines by LF.

If you ask J to give you the definition of a verb as a string, e.g. using Foreign (5!:5), the result is a LF-separated string, i.e. a string of bytes, with each code line terminated by LF:

out=. 3 : 0 smoutput '--- executing: y' tryexec y smoutput '... (datatype y) is: ' , (datatype y) smoutput '--- executing: uucp y' tryexec uucp y ) ] jcode=: 5!:5 <'out' NB. using the tool defined above as an example 3 : 0 smoutput '--- executing: y' tryexec y smoutput '... (datatype y) is: ' , (datatype y) smoutput '--- executing: uucp y' tryexec uucp y ) $ jcode NB. jcode is a string, i.e. a list of rank 1 141 datatype jcode NB. The precision of jcode is: byte literal CR e. jcode NB. (Even on a Windows platform) it doesn't contain CR 0 LF e. jcode NB. but it does contain LF 1

Define another tool (showLF) to emphasize the whereabouts of LF in the noun jcode:

showLF=: verb define require 'strings' z=. quote y z rplc LF ; ''' , LF , ''' )

showLF jcode '3 : 0' , LF , 'smoutput ''--- executing: y''' , LF , 'tryexec y' , LF , 'smoutput ''... (datatype y) is: '' , (datatype y)' , LF , 'smoutput ''--- executing: uucp y''' , LF , 'tryexec uucp y' , LF , ')'

Let's summarize the situation with J code, strings, lines, sentences and textfiles:


Inflection

An ASCII character or a name can be extended with inflections in any order:

The inflected word is a single word. It is different from, and completely independent of, the uninflected word.

Inflected words are used only to designate primitives (including control words), not user-assigned names.


Primitives

Primitives are words whose meaning is built into J, naming its pre-defined nouns, verbs, action modifiers, and controls.

All graphics, inflected graphics, and inflected names are reserved for primitives.


Control Words

Some inflected names, such as if., do., and end., are control words. They are used to control program flow. These words are treated as sentences in themselves. They break up the executable line into a number of separate sentences -- the control words, and the phrases between the control words.

Control words are allowed only inside the body of an explicit definition.


Paired Characters

J executes a sentence from right to left. Parentheses: ( and ) are the only words that change this order.

Parentheses are the only paired characters in J.

We exclude the Apostrophe, or single-quote ('), which some would consider to be "paired" with itself.

The characters <, >, {, }, [, ] and their inflected forms are, counter-intuitively, not paired: they are the names of individual primitives.


Parts Of Speech

Every word in a J sentence, and every value produced during execution of a sentence, has a part of speech. The parts of speech are:

The primary parts of speech are noun, verb, adverb, and conjunction.


The Result Of A Sentence

The result of a sentence is the result of the last execution within the sentence. Normally this is the execution of a verb, which produces a noun.

If the sentence has been typed-in from the keyboard, J will print the result of the sentence on the console (in a J IDE, this will appear before the next prompt for input)except when the last execution was an assignment.


Defining A Name

A name is defined when it appears in an assignment, i.e. when the name appears just before a copula (=.) or (=:). The act of assignment attaches a value to a name to produce a definition.

When a name is assigned, it takes as its value the result of the sentence to the right of the copula. This value may be any part of speech: it is possible to create named verbs by assigning verb values in exactly the same way that assigning noun values creates named nouns.

The phrase 4!:0 <y reports the part of speech of the name y according to this table:

The Result of 4!:0 <y
_2 invalid name <!>
_1 name is valid but undefined
0 noun
1 adverb
2 conjunction
3 verb

<!> An invalid name, in the table above, is a name which

_2 4!:0 <'x123' NB. Valid name, but undefined _1 4!:0 <'x',TAB,'123' NB. Invalid name, has invalid character 2 4!:0 <'x123' NB. Invalid name, ends in a single underscore _2 a=: 5 NB. A noun 4!:0 <'a' 0 slash=: / NB. An adverb 4!:0 <'slash' 1 atsign=: @ NB. A conjunction 4!:0 <'atsign' 2 plus=: + NB. A verb 4!:0 <'plus' 3

Illustration

The ;: verb will show you how J breaks a sentence into words. However, in some contexts it might be more convenient show the sentence with spaces between each word. For example, with the definition:

jwords=: 3 : 0 bchars=: a.{~ 16+i.11 boxy=. bchars-:9!:6'' indent=. ' '#~+/*/' '=y 9!:7]11#' ' r=. indent,deb 1{":;:y bchars 9!:7@([^:boxy)'+++++++++|-' r )

we get:

jwords '+/%#'

/: ~

jwords@>LF cut 5!:5<'jwords' 3 : 0
bchars =: a. { ~ 16 + i. 11
boxy =. bchars -: 9 !: 6 ''
indent =. ' ' # ~ + / * / \ ' ' = y
9 !: 7 ] 11 # ' '
r =. indent , deb 1 { ": ;: y
bchars 9 !: 7 @ ( [ ^: boxy ) '+++++++++|-' r
)

One thing to remember, though, when using this routine: number lists are single words which include embedded spaces:

;:'1 2 3' +-----+ |1 2 3| +-----+