
- •Foreword
- •1. Introduction
- •2. Culture Shock
- •3. Preliminaries
- •Notation Used in This Book
- •Terminology
- •Sentences (statements)
- •Word Formation (tokenizing rules)
- •Numbers
- •Characters
- •Valence of Verbs (Binary and Unary Operators)
- •How Names (Identifiers) Get Assigned
- •Order of Evaluation
- •How Names Are Substituted
- •What a verb (function) looks like
- •Running a J program
- •The Execution Window; Script Windows
- •Names Defined at Startup
- •Step-By-Step Learning: Labs
- •J Documentation
- •Getting Help
- •4. A First Look At J Programs
- •Average Daily Balance
- •Calculating Chebyshev Coefficients
- •5. Declarations
- •Arrays
- •Cells
- •Phrases To Memorize
- •Constant Lists
- •Array-creating Verbs
- •6. Loopless Code I—Verbs Have Rank
- •Examples of Implicit Loops
- •The Concept of Verb Rank
- •Verb Execution—How Rank Is Used (Monads)
- •Controlling Verb Execution By Specifying a Rank
- •Examples Of Verb Rank
- •Negative Verb Rank
- •Verb Execution—How Rank Is Used (Dyads)
- •When Dyad Frames Differ: Operand Agreement
- •Order of Execution in Implied Loops
- •A Mistake To Avoid
- •7. Starting To Write In J
- •8. More Verbs
- •Arithmetic Dyads
- •Boolean Dyads
- •Min and Max Dyads
- •Arithmetic Monads
- •Boolean Monad
- •Operations on Arrays
- •9. Loopless Code II—Adverbs / and ~
- •Modifiers
- •The Adverb Monad u/
- •The adverb ~
- •10. Continuing to Write in J
- •11. Boxing (structures)
- •Terminology
- •Boxing As an Equivalent For Structures In C
- •12. Compound Verbs
- •Verb Sequences—u@:v and u@v
- •Making a Monad Into a Dyad: The Verbs [ and ]
- •Making a Dyad Into a Monad: u&n and m&v
- •13. Empty Operands
- •Execution On a Cell Of Fills
- •Empty cells
- •If Fill-Cells Are Not Enough
- •14. Loopless Code III—Adverbs \ and \.
- •15. Verbs for Arithmetic
- •Dyads
- •Monads (all rank 0)
- •16. Loopless Code IV
- •A Few J Tricks
- •Power/If/DoWhile Conjunction u^:n and u^:v
- •Tie and Agenda (switch)
- •17. More Verbs For Boxes
- •Dyad ; (Link) And Monad ; (Raze)
- •Dyad { Revisited: the Full Story
- •Split String Into J Words: Monad ;:
- •Fetch From Structure: Dyad {::
- •Report Boxing Level: Monad L.
- •18. Verb-Definition Revisited
- •What really happens during m :n and verb define
- •Compound Verbs Can Be Assigned
- •Dual-Valence verbs: u :v
- •The Suicide Verb [:
- •Multi-Line Comments Using 0 :0
- •Final Reminder
- •The Obverse u^:_1
- •Apply Under Transformation: u&.v and u&.:v
- •Defined obverses: u :.v
- •An observation about dyadic verbs
- •20. Performance: Measurement & Tips
- •Timing Individual Sentences
- •Compounds Recognized by the Interpreter
- •Use Large Verb-Ranks! and Integrated Rank Support
- •Shining a Light: The J Performance Monitor
- •21. Input And Output
- •Foreigns
- •File Operations 1!:n; Error Handling
- •Treating a File as a Noun: Mapped Files
- •Format Data For Printing: Monad And Dyad ":
- •Format an Array: 8!:n
- •Format binary data: 3!:n
- •printf, sprintf, and qprintf
- •Convert Character To Numeric: Dyad ".
- •22. Calling a DLL Under Windows
- •Memory Management
- •Aliasing of Variables
- •23. Socket Programming
- •Asynchronous Sockets and socket_handler
- •Names and IP Addresses
- •Connecting
- •Listening
- •Other Socket Verbs
- •24. Loopless Code V—Partitions
- •Find Unique Items: Monad ~. and Monad ~:
- •Apply On Subsets: Dyad u/.
- •Apply On Partitions: Monad u;.1 and u;.2
- •Apply On Specified Partitions: Dyad u;.1 and u;.2
- •Apply On Subarray: Dyad u;.0
- •Apply On All Subarrays: Dyad u;.3 and u;._3
- •Extracting Variable-Length Fields Using ^: and ;.1
- •Example: Combining Adjacent Boxes
- •25. When Programs Are Data
- •Calling a Published Name
- •Using the Argument To a Modifier
- •Invoking a Gerund: m`:6
- •Passing the Definition Of a Verb: 128!:2 (Apply)
- •Passing an Executable Sentence: Monad ". and 5!:5
- •26. Loopless Code VI
- •28. Modifying an array: m}
- •Monad I.—Indexes of the 1s in a Boolean Vector
- •29. Control Structures
- •while./do./end. and whilst./do./end.
- •if./do./else./end., if./do./elseif./do./end.
- •try./catch./catcht./end. and throw.
- •return.
- •assert.
- •30. Modular Code
- •Locales And Locatives
- •Assignment
- •Name Lookup
- •Changing The Current Locale
- •The Shared Locale 'z'
- •Using Locales
- •31. Writing Your Own Modifiers
- •Modifiers That Do Not Refer To x. Or y.
- •Modifiers That Refer To x. Or y.
- •32. Applied Mathematics in J
- •Complex Numbers
- •Matrix Operations
- •Calculus: d., D., D:, and p..
- •Taylor Series: t., t:, and T.
- •Hypergeometric Function with H.
- •Sparse Arrays: Monad and Dyad $.
- •Random Numbers: ?
- •Computational Addons
- •Useful Scripts Supplied With J
- •33. Elementary Mathematics in J
- •Verbs for Mathematics
- •Extended Integers, Rational Numbers, and x:
- •Factors and Primes: Monad p:, Monad and Dyad q:
- •Permutations: A. and C.
- •34. Graphics
- •Plot Package
- •2D Graphics: the gl2 Library
- •Displaying Tabular Data: the Grid Control
- •3D Graphics: OpenGL
- •35. Odds And Ends
- •Dyad # Revisited
- •Boxed words to string: Monad ;:^:_1
- •Spread: #^:_1
- •Choose From Lists Item-By-Item: monad m}
- •Recursion: $:
- •Make a Table: Adverb dyad u/
- •Cartesian Product: Monad {
- •Boolean Functions: Dyad m b.
- •Operations Inside Boxes: u L: n, u S: n
- •Comparison Tolerance !.f
- •Right Shift: Monad |.!.f
- •Generalized Transpose: Dyad |:
- •Monad i: and Dyad i:
- •Fast String Searching: s: (Symbols)
- •Fast Searching: m&i.
- •CRC Calculation
- •Unicode Characters: u:
- •Window Driver And Form Editor
- •Tacit Programming
- •36. Tacit Programs
- •37. First Look At Forks
- •38. Parsing and Execution I
- •39. Parsing and Execution II
- •The Parsing Table
- •Examples Of Parsing And Execution
- •Undefined Words
- •40. Forks, Hooks, and Compound Adverbs
- •Tacit and Compound Adverbs
- •Referring To a Noun In a Tacit Verb
- •41. Readable Tacit Definitions
- •Flatten a Verb: Adverb f.
- •Special Verb-Forms Used in Tacit Definitions
- •43. Common Mistakes
- •Mechanics
- •Programming Errors
- •44. Valedictory
- •45. Glossary
- •46. Error Messages
- •47. Index

The verb (dyad ,"1 0) has left rank 1, and the left operand has rank 2, so the operation will be applied to 1-cells of the left operand. The verb has right rank 0, and the right operand has rank 1, so the operation will be applied to 0-cells of the right operand. The left frame is 3, the right frame is 3 .
Think of the left operand |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
as a list of 3 1-cells, and |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0 1 2 |
3 4 5 |
|
6 7 8 |
|
|
|
|
0 |
1 |
|
2 |
|
|
||||
the right operand as a list |
|
|
|
|
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
of 3 0-cells: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The corresponding left |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
and right operand cells are |
|
0 1 2 |
|
0 |
|
|
3 4 5 |
1 |
|
|
|
6 7 8 |
|
2 |
|
|
|
paired: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The operation dyad , is
performed on each pair of 0 1 2 0 3 4 5 1 6 7 8 2 cells:
|
Since each result is a |
0 1 |
2 0 |
|
1-cell of shape 4, and the |
||
|
3 4 |
5 1 |
|
|
frame is 3, the result is an |
||
|
6 7 |
8 2 |
|
|
array with shape 3 4 |
||
|
|
|
|
|
Figure 8. |
Execution of (i. 3 3) ,"1 0 i. 3 |
|
|
defverbs 'comma' |
|
|
+ |
(i. 3 3) comma"1 0 i. 3 |
+ |
|
-------------+------------- |
+------------- |
|0 1 2 comma 0|3 4 5 comma 1|6 7 8 comma 2| +-------------+-------------+-------------+
(i. 3 3) ,"1 0 i. 3 0 1 2 0 3 4 5 1 6 7 8 2
When Dyad Frames Differ: Operand Agreement
The processing of dyads has an extra step not present for monads, namely the pairing of corresponding cells of the left and right operands. As long as the frames lf and rf are the same, as in the examples so far, this is straightforward. If the frames are different, J may still be able to pair left and right cells, using another level of implicit looping, one that provides considerable additional programming power. The formal description that
45
follows is not easy to follow—you might want to skim over it and read it in detail after you have studied the examples that follow.
J requires that one of the frames be a prefix of the other (if the frames are identical, each is a prefix of the other and all the following reduces to the simple case we have studied). The common frame cf is the part of the frames that is identical, namely the shorter of the two frames; its length is designated rcf. If we look at the cells of the operands relative to this common frame (i. e. the (-rcf)-cells), we see that for the operand with the shorter frame, these cells are exactly the rank that will be operated on, while for the operand with the longer frame, each (-rcf)-cell contains multiple operand cells.
First, the (-rcf)-cells of the two operands are paired one-to-one (because they have the same frame), leaving each shorter-frame operand cell paired with a longer-frame (-rcf)-cell. Then, the longer-frame (-rcf)-cells are broken up into operand cells, with each operand cell being paired with a copy of the shorter-frame operand cell that was paired with the (-rcf)-cell (an equivalent statement is that the cells of the shorter-frame operand are replicated to match the surplus frame of the longer-frame operand). This completes the pairing of operand cells, and the operation is then performed on the paired operand cells, and collected using the longer frame. Maybe some examples will help.
46
100 200 + i. 2 3
100 101 102
203 204 205
|
The verb (dyad +) has left rank 0, and the left operand has rank 1, so the operation |
||||||||||||||||||||||||
|
will be applied to 0-cells of the left operand. The verb has right rank 0, and the |
||||||||||||||||||||||||
|
right operand has rank 2, so the operation will be applied to 0-cells of the right |
||||||||||||||||||||||||
|
operand. The left frame is 2, the right frame is 2 3 . |
|
|
|
|
||||||||||||||||||||
|
The common frame is 2, with |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
length 1, so think of each operand |
|
100 |
200 |
|
|
|
|
|
|
|
0 1 2 |
|
3 4 5 |
|
|
|
||||||||
|
as a list of 2 |
_1-cells |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The _1-cells of the operands are |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
100 |
|
0 |
1 2 |
|
|
|
200 |
3 4 5 |
|
|
|
|
|
||||||||||
|
paired: |
|
|
|
|
|
|
|
|
|
|
|
|||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The longer-frame operand (the right |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
one) is broken up into operand |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
0 |
|
|
|
100 |
|
1 |
|
|
100 |
|
2 |
|
|
|
|||||||
|
0-cells, each being paired with a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
copy of the shorter-frame operand |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
3 |
|
|
|
200 |
|
4 |
|
|
200 |
|
5 |
|
|
|
|||||||
|
cell. Each paired _1-cell becomes a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
row of paired operand cells: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The operation dyad |
+ is performed |
|
100 |
101 |
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
on each pair of cells: |
|
203 |
204 |
|
205 |
|
|
|
|
|
|
|
|
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Since each result is an atom, and |
|
|
|
|
|
|
|
|
100 |
101 |
102 |
|
|
|
|
|||||||||
|
the longer frame is 2 3, the result |
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
|
|
|
203 |
204 |
205 |
|
|
|
|
||||||||||
|
is an array with shape 2 3 |
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
Figure 9. Execution of 100 200 + i. 2 3 |
|
|
|
|
|
|
|
|
|
||||||||||||||
|
defverbs 'plus"0' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
+ |
100 200 plus i. 2 3 |
|
|
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
+---------- |
+---------- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|100 plus 0|100 plus 1|100 plus 2| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||
+---------- |
+---------- |
+---------- |
|
|
|
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|200 plus 3|200 plus 4|200 plus 5| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||
+---------- |
+---------- |
+---------- |
|
|
|
+ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The simplest and most common case of different-length frames is when the shorter frame is of zero length; in other words, when one of the operands has only one cell. In that case, the single cell is replicated to match every cell of the longer operand. An easy way to force an operand to be viewed as a single cell is to make the verb have infinite rank for that operand. This is not a special case—the behavior follows from the rules already given—but it's worth an example:
47

'abc' ,"_ 0 'defg' abcd
abce abcf abcg
The verb (dyad ,"_ 0) has left rank _, and the left operand has rank 1, so the operation will be applied to 1-cells of the left operand. The verb has right rank 0, and the right operand has rank 1, so the operation will be applied to 0-cells of the right operand. The left frame is (empty), the right frame is 4 .
The common frame is |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(empty), with length 0, so |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defg |
|
|
|
|
|
|
||||
take each operand in its |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
entirety: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The cells of the operands |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abc |
defg |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
are paired: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The longer-frame operand |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(the right one) is broken up |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
into operand 0-cells, each |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abc |
d |
|
|
|
abc |
e |
|
|
|
|
abc |
f |
|
|
abc |
g |
|
|
|||||
being paired with a copy of |
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
the shorter-frame operand |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cell: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The operation dyad , is |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abcd |
abce |
|
abcf |
abcg |
|
|
|
|
|
|
|
|
|||||||||||
performed on each pair of |
|
|
|
|
|
|
|
|
|
|
|||||||||||||||
cells: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
Since each result is a 1-cell |
|
|
|
|
|
|
|
|
|
|
|
|
|
abcd |
|
|
|
|
|
|
|||||
with length 4, and the |
|
|
|
|
|
|
|
|
|
|
|
|
|
abce |
|
|
|
|
|
|
|||||
longer frame is 4, the result |
|
|
|
|
|
|
|
|
|
|
|
|
|
abcf |
|
|
|
|
|
|
|||||
is an array with shape 4 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
abcg |
|
|
|
|
|
|
|||||
Figure 10. Execution of 'abc' ,"_ 0 'defg' |
|
|
|
|
|
|
|||||||||||||||||||
defverbs 'comma' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'abc' comma"_ 0 'defg' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+-----------+-----------+-----------+-----------+ |abc comma d|abc comma e|abc comma f|abc comma g| +-----------+-----------+-----------+-----------+
You must thoroughly understand this example, where one operand has only one cell, because it occurs frequently. The handling of the general case of dissimilar frames is uncommon enough that you do not need to understand it perfectly right now—you'll
48