Contents - J Introduction and Dictionary Introduction Sample Topics Dictionary Vocabulary Foreign Conjunction References Acknowledgments Copyright J User Manual Introduction Contents 1 Introduction 2 Mnemonics 3 Ambivalence 4 Verb and Adverbs 5 Punctuation 6 Forks 7 Programs 8 Bond Conjunction 9 Atop Conjunction 10 Vocabulary 11 Housekeeping 12 Power and Inverse 13 Reading and Writing 14 Format 15 Partitions 16 Defined Adverbs 17 Word Formation 18 Names and Displays 19 Explicit Definition 20 Tacit Equivalents 21 Rank 22 Gerund and Agenda 23 Recursion 24 Iteration 25 Trains 26 Permutations 27 Linear Functions 28 Obverse and Under 29 Identity Functions 30 Secondaries Sample Topics Contents Sample Topics Spelling Alphabet & Numbers Grammer Function Tables Bordering a Table Tables (Letter Frequency) Tables Classification Disjoint Classification Classification (Inner Product) Classification (Sets) Sorting Compositions (Conjunctions) Composition (Forks) Junctions Partitions (Adverbs) Partitions (Cuts) Geometry Symbolic Functions Directed Graphs Distance Polynomials (Coefficients) Polynomials (Roots) Polynomials (Newtons Method) Polynomials (Kerners Method) Polynomials (Stopes) Dictionary Contents Dictionary Alphabet and Words Grammar Nouns Verbs Adverbs and Conjunctions Comparitives Parsing and Execution Trains Frets and Scripts Locatives Errors and Suspension Definitions Vocabulary = Self-Classify - Equal =. Is (Local) =: Is (Global) < Box - Less Than <. Floor - Lesser of <: Decrem - Less Or Eq > Open - Larger Than >. Ceiling - Larger of >: Increm - Larg Or Eq _ Negative Sign /Infinit _. Indeterminate _: Infinity + Conjugate - Plus +. Real/Imag - GCD (Or) +: Double - Not-Or * Signum - Times *. Len/Angle - LCM (And) *: Square - Not-And - Negate - Minus -. Not (1) - Less -: Halve - Match % Reciprocal - Divided by %. Matrix Inv - Mat Divide %: Square Root - Root ^ Exponential - Power ^. Natural Log - Logarithm ^: Power $ Shape Of - Shape $. $: Self-Reference ~ Reflex-Pass-EVOKE ~. Nub ~: Nub Sieve - Not-Eq | Magnitude - Residue |. Reverse - Rotate (Shift) |: Transpose . Det - Dot Product .. Even .: Odd : Explicit - Monad/Dyad :. Obverse :: Adverse , Ravel - Append ,. Ravel Items - Stitch ,: Itemize - Laminate ; Raze - Link ;. Cut ;: Word formation # Tally - Copy #. Base 2 - Base #: Antibase 2 - Antib ! Factorial - Out Of !. Fit (Customize) !: Foreign / Insert-Table-INSERT /. Oblique-Key-APPEND /: Grade Up - Sort \ Prefix-Infix-TRAIN \. Suffix - Outfix \: Grade Down - Sort [ Same - Left [. Lev [: Cap ] Same - Right ]. Dex ]: { Catalogue - From {. Head - Take {: Tail } Item Amend - Amend }. Behead - Drop }: Curtail " Rank - CONSTANT ". Do ": Format ` Tie (Gerund) `. `: Evoke Gerund @ Atop @. Agenda @: At & Bond /Compose &. Under (Dual) &: Appose ? Roll - Deal ?. ?: a. Alphabet a: Ace (boxed empty) A. Atomic Permute b. Boolean/Basic c. Characteristic values C. Cycle-Dir - Perm D. Derivative D: Secant Slope e. Raze In - Memb E. - Member of Interval f. Fix i. Integ - Index Of j. Imaginary - Complex NB. Comment o. Pi Times - Circlr p. Polynomial r. Angle - Polar t. Taylor coef T. Taylor Function x. Left Argument y. Right Argument Constant functions Constants 1: INTRODUCTION J is a general-purpose programming language available on a wide variety of computers. Although it has a simple structure and is readily learned by anyone familiar with mathematical notions and notation, its distinctive features may make it difficult for anyone familiar with more conventional programming languages. This introduction is designed to present J in a manner that makes it easily accessible to programmers, by emphasizing those aspects that distinguish it from other languages. These include: 1. A mnemonic one- or two-character spelling for primitives. 2. No order-of-execution hierarchy among functions. 3. The systematic use of ambivalent functions that, like the minus sign in arithmetic, can denote one function when used with two arguments (subtraction in the case of -), and another when used with one argument (negation in the case of -). 4. The adoption of terms from English grammar that better fit the grammar of J than do the terms commonly used in mathematics and in programming languages. Thus, a function such as addition is also called a verb (because it performs an action), and an entity that modifies a verb (not available in most programming languages) is accordingly called an adverb. 5. The systematic use of adverbs and conjunctions to modify verbs, so as to provide a rich set of operations based upon a rather small set of verbs. For example, +/a denotes the sum over a list a, and */a denotes the product over a, and a */ b is the multiplication table of a and b. 6. The treatment of vectors, matrices, and other arrays as single entities. 7. The use of functional or tacit programming that requires no explicit mention of the arguments of a function (program) being defined, and the use of assignment to assign names to functions (as in sum=.+/ and mean=.sum % #). The following lessons are records of actual J sessions, accompanied by commentary that should be read only after studying the corresponding session (and perhaps experimenting with variations on the computer). The lessons should be studied with the Jsystem and dictionary at hand. The reckless reader may go directly to the sample topics on page 31. 2: MNEMONICS The left side of the page shows an actual computer session with the result of each sentence shown at the left margin. First cover the comments at the right, and then attempt to state in English the meaning of each primitive so as to make clear the relations between related symbols. For example, < is less than and <. is lesser of (that is, minimum). Then uncover the comments and compare with your own. 7<5 Less than 0 A zero is interpreted as false. 7<.5 Lesser of 5 7>5 Greater than 1 A one is true (€ la George Boole) 7>.5 Greater of 7 10^3 Power (€ la Augustus de Morgan) 1000 10^.1000 Logarithm 3 7=5 Equals 0 b=. 5 Is (assignmentáor copula) 7<. b 5 Min=. <. Min is <. power=. ^ Power is ^ gt=. > gt is > 10 power (5 Min 3) 1000 Exercises: 2.1 Enter the following sentences on the computer, observe the results, give suitable names to any new primitives (such as * and +. and *.), and comment on their behaviour. a=.0 1 2 3 b=.3 2 1 0 a+b a*b a-b a%b a^b a^.b ab (ab) (ab) Compare your comments with the following: a) Negative 3 is denoted by _3. The underbar _ is part of the representation of a negative number in the same sense that the decimal point is part of the representation of one-half when written in the form 0.5, and the negative sign _ must not be confused with the symbol used to denote subtraction (i.e., -). b) Division (%) by zero yields infinity, denoted by the underbar alone. c) Log of 2 to the base 1 is infinite, and log of 0 to the base 3 is negative infinity (__). d) Since the relation 5<7 is true, and the result of 5<7 is 1, it may be said that true and false are represented by the ordinary integers 1 and 0. George Boole used this same convention, together with the symbol + to represent the booleanáfunction or. We use the distinct representation +. to avoid conflict with the analogous (but different) addition (denoted by +). 2.2 Following the example Min=. <. , invent, assign, and use names for each of the primitives encountered thus far. 3: AMBIVALENCE Cover the comments on the right and provide your own. 7-5 The function in the sentence 7-5 applies to two 2 arguments to perform subtraction, but in the sentence -5 it applies to a single argument to -5 perform negation. _5 Adopting from chemistry the term valence, we say that the symbol - is ambivalent, its effective 7%5 binding power being determined by context. 1.4 %5 The ambivalence of - is familiar in arithmetic; 0.2 it is here extended to other functions. 3^2 9 ^2 Exponential (that is, 2.71828^2) 7.38906 a=. i. 5 The function integer or integer list a 0 1 2 3 4 Listáor vector a i. 3 1 The function index or index of 3 1 b=. 'Canada' Enclosing quotes denote literal characters b i. 'da' 4 1 $ a Shape function 5 3 4 $ a Reshape function 0 1 2 3 Table or matrix 4 0 1 2 3 4 0 1 3 4 $ b Cana daCa nada %a Functions apply to lists _ 1 0.5 0.333333 0.25 The symbol _ alone denotes infinity Exercises: 3.1 Enter the following sentences (and perhaps related sentences using different arguments), observe the results, and state what the two cases (monadicáand dyadic) of each function do: a=. 3 1 4 1 5 9 b=. 'Canada' #a 1 0 1 0 1 3 # a 1 0 1 0 1 3 # b /: a /:b a /: a a /: b b /: a b /: b c=. 'can''t' c #c c /: c 3.2 Make a summary table of the functions used thus far. Then compare it with the following table (in which a dot separates the monadic case from the dyadic, as in Negate . Subtract): . : + . Add . Or - Negate . Subtract * .Times . And % Reciprocal . Divide ^ Exponential . Power . Log < . Less Than . Lesser Of > . Greater Than . Greater Of = . Equals Is (Copula) i Integers . Index Of $ Shape . Reshape / Grade . Sort # Number of items . Replicate 3.3 Try to fill some of the gaps in the table of Exercise 3.2 by experimenting on the computer with appropriate expressions. For example, enter ^. 10 and ^.2.71828 to determine the missing (monadic) case of ^. and enter %: 4 and %: -4 and +%: -4 to determine the case of % followed by a colon. that are still beyond your grasp; it may be better to return to them after working through later lessons. Note that the effects of certain functions become evident only when applied to arguments other than positive integers: try <.1 2 3 4 and <.3.4 5.2 3.6 to determine the effect of the monad <. . 3.4 If b=.3.4 5.2 3.6 , then <.b yields the argument b rounded down to the nearest integer. Write and test a sentence that rounds the argument b to the nearest integer. ANSWER: <.(b+0.5) or <.b+0.5 or <.b+1r2 3.5 Enter 2 4 3 $ i. 5 to see an example of a rank 3 array or report (for two years of four quarters of three months each). 3.6 Enter ?9 repeatedly and state what the function ? does. Then enter t=. ?3 5 $ 9 to make a table for use in further experiments. ANSWER: ? is a (pseudo-) random number generator; ?n produces an element from the population i.n 4: VERBS AND ADVERBS In the sentence %a of Lesson 3, the % acts upon a to produce a result, and %a is therefore analogous to the notion in English of a verb acting upon a noun or pronoun. We will hereafter adopt the term verb instead of (or in addition to) the mathematical term function used thus far. The sentence +/ 1 2 3 4 is equivalent to 1+2+3+4; the adverb / applies to its verb argument + to produce a new verb whose argument is 1 2 3 4, and which is defined by inserting the verb + between the items of its argument. Other arguments of the insert adverb are treated similarly: */b=.2 7 1 8 2 8 1792 <./b 1 >./b 8 The verb resulting from the application of an adverb may (like a primitive verb) have both monadic and dyadic cases. In the present instance of / the dyadic case produces a table. For example: 2 3 5 +/ 0 1 2 3 2 3 4 5 3 4 5 6 5 6 7 8 The verbs over=.({.;}.)@":@, and by=.' '&;@,.@[,.] can be entered as utilities (for use rather than for immediate study), and can clarify the interpretation of function tables such as the addition table produced above. For example: a=. 2 3 5 b=. 0 1 2 3 a by b over a +/ b b by b over b and the lesser-of and greater-of. 4.3 Apply the verbs |. and |: to various tables, and try to state what they do. 4.4 The transpose function |: changes the subtraction table, but appears to have no effect on the multiplication table. State the property of those functions whose tables remain unchanged when transposed. ANSWER: They are commutative 4.5 Enter d by d over d!/d and state the definition of the dyad ! . ANSWER: ! is the binomial coefficientáor outoffunction 3!5 is the number of ways that three things can be chosen from five. 5: PUNCTUATION English employs various symbols to punctuate a sentence, to indicate the order in which its phrases are to be interpreted. Thus: The teacher said he was stupid. The teacher, said he, was stupid. Math also employs various devices (primarily parentheses) to specify order of interpretation or, as it is usually called, order of execution. It also employs a set of rules for an unparenthesized phrase, including a hierarchy amongst functions. For example, power is executed before times, which is executed before addition. J uses only parentheses for punctuation, together with the following rules for unparenthesized phrases: The right argument of a verb is the value of the entire phrase to its right. Adverbs are applied first. Thus, the phrase a */ b is equivalent to a (*/) b, not to a (*/b). For example: a=.5 b=.3 (a*a)+(b*b) 34 a*a+b*b 70 a*(a+(b*b)) 70 (a+b)*(a-b) 16 a (+*-) b 16 The last sentence above includes the isolated phrase +*- which has thus far not been assigned a meaning. It is called a trident or fork , and is equivalent to the sentence that precedes it. A fork also has a monadic meaning, as illustrated for the mean below: c=.2 3 4 5 6 (+/ % #) c The verb # yields the number 4 of items in its argument. (+/c)%(#c) 4 Exercises: 5.1 In math, the expression 3x4+4x3+5x2 is called a polynomial. Enter: x=. 2 (3*x^4)+(4*x^3)+(5*x^2) to evaluate the polynomial for the case where x is 2. 5.2 Note that the hierarchy among functions used in math is such that no parentheses are necessary in writing a polynomial. Write an equivalent sentence using no parentheses. ANSWER: +/3 4 5 * x ^ 4 3 2 or (first assigning names to the coefficients 3 4 5 and the exponents 4 3 2), as +/c*x^e 6: FORKS As illustrated in the preceding lesson, an g g isolated sequence of three verbs is called / \ / \ a fork; its monadic and dyadic cases are f h f h defined by: | | / \ / \ y y x y x y (f g h) y is (f y) g (h y) x (f g h) y is (x f y) g (x h y) The diagrams at the upper right provide visual images of the fork. Before reading the notes at the right (and by using the facts that %: denotes the root function and ] denotes the identity function), try to state in English the significance of each of the following sentences: a=. 8 7 6 5 4 3 b=. 4 5 6 7 8 9