The Six Parts Of Speech
J is a language and its different components or words fall into a number of categories. These categories have many similarities to the parts of speech described in natural languages, so J chooses to leverage those analogies rather than create new terms.
As with natural languages it is not necessary to know or use all the parts of speech or all the words of J to make yourself understood. A lot can be accomplished with only nouns and a handful of verbs.
Definition: The primary parts of speech are noun, verb, adverb and conjunction.
- Vocabulary/Words#Parts_Of_Speech for a brief paragraph on the 6 parts of speech
- Grammar — for what the J Dictionary says about the "6 parts of speech".
In the simplest terms a noun is a "thing". For example both dog and cat in the sentence "The dog bites the cat."
Nouns in J are the objects manipulated by verbs.
5 # 'b' NB. Both 'b' and 5 are nouns. bbbbb
(The verb Copy (x # y) makes x copies of the items of y .)
A noun can be assigned a name, in which case it corresponds to what other computer languages call a "variable".
city=: 'Toronto' NB. The name city is assigned to the literal list 'Toronto' # city NB. Tally (count) the number of items in city 7
Nouns are atoms or arrays. All nouns have a noun rank (the number of dimensions it has), a shape (the sizes/lengths of each of its dimensions) and a type (numeric, character, boxed, symbol).
Using the verb Shape ($), create a noun called dates as an array of four dates arranged in three columns (Year, Month and Day)
dates=: 4 3 $ 2008 12 22 2007 6 15 2010 4 9 1999 11 17 dates 2008 12 22 2007 6 15 2010 4 9 1999 11 17
See these properties of dates like this
# dates NB. Tally (the number of items in dates) 4 $ dates NB. Shape Of (the length of each dimension - four rows and 3 columns) 4 3 #$ dates NB. Rank of noun (the number of dimensions) 2 datatype dates NB. Type (could also use the foreign conjunction: 3!:0 dates) integer
For further details about nouns in general, see Vocabulary/Nouns.
Verbs are "doing words" such as chases in the sentence "The dog chases the cat.".
Verbs in J are usually referred to as functions in other programming languages and mathematics.
Verbs can have both monadic (takes only a right argument) or dyadic (takes both a left and right argument) forms (Vocabulary/Valence).
Verbs have a rank associated with each of their arguments.
A modifier is either an adverb or a conjunction:
- Adverb: a modifier that takes one operand
- Conjunction: a modifier that takes two operands .
Adverbs describe a verb, generally providing more detail about "How" an action is being performed, for example eagerly in the sentence "The dog eagerly chases the cat".
Adverbs in J combine with the verb or noun immediately to their left to create a new verb. The adverb / (Insert) combines with + (Plus) to form the new verb +/ (Sum), but can also combine with * (Times) to form the new verb */ (Product).
Conjunctions connect or combine two words. For example and in the sentence "The dog chases and catches the cat".
Conjunctions in J combine the verb or noun on their right with the verb or noun on their left to create a new word. For example the adverb &(Bond) can be used to combine the number (a noun) 0.5 with the verb + (Plus) to create a new verb 0.5&+ that we could call "Add one-half".
A copula assigns the current result to one or more names.
There are two copulas (copulae)
Punctuation consists of:
- The Single-Quote (') — to surround a string or a character atom
- Parentheses: () — to control order of evaluation of a sentence
- The primitive (NB.) — to begin a comment.
Control words are words to control program flow, e.g. if. do. else. elseif. end.
Call by name and Call by reference
Nouns are always passed by reference. The data is not copied. The number of references to each noun value is used to decide when the value is no longer needed.
Other parts of speech are passed by name: if a word is a name, the name is passed and evaluated as needed. If a word has no name (a primitive, for example), an anonymous reference to the word is created as a means of referring to the word.