- •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
#. a
31
42
The verb rank is 1 and the noun rank is 3, so we will be applying the verb to 1-cells. The frame f is 2 2
|
|
|
|
|
|
|
Think of the operand as a 2×2 array of |
|
0 0 1 1 |
|
0 0 0 1 |
||
|
|
|
|
|
|
|
|
|
|
|
|
||
1-cells: |
|
0 1 0 0 |
|
0 0 1 0 |
||
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The verb is applied to each cell: |
|
3 |
1 |
|
|
|
|
|
|
|
|
|
|
|
4 |
2 |
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
Since each result is an atom, i. e. a 0-cell, the |
|
|
|
|
3 1 |
|
result is a 2×2 array of 0-cells, i. e. an array of |
|
|
|
|
||
|
|
|
|
4 2 |
||
shape 2 2 |
|
|
|
|
||
|
|
|
|
|
|
Figure 2. Execution of #. 2 2 4 $ 0 0 1 1 0 0 0 1 0 1 0 0 0 0 1 0
Controlling Verb Execution By Specifying a Rank
The implicit loops we have used so far are interesting, but they are not powerful enough for our mission of replacing all explicit loops. To understand the deficiency and its remedy, consider the new verb monad +/, which creates the total of the items of its operand (just think of it as 'monad SumItems'):
+/ 1 2 3
6
The result was 1 + 2 + 3, as expected.
0 |
1 |
i. |
2 3 |
2 |
|
||
3 |
4 |
5 |
|
3 |
5 |
+/ i. 2 3 |
|
7 |
|
The result was 0 1 2 + 3 4 5, as expected (remember that the items are added, and the items of i. 2 3 are 1-cells). Adding together a pair of 1-cells adds the respective atoms, as we will soon learn.
This application of monad +/ to a rank-2 array corresponds to the C code fragment: for(j = 0;j<3;++j)sum[j] = 0;
for(i = 0;i<2;++i)
for(j = 0;j<3;++j)sum[j] += array[i][j];
Suppose we wanted to add up the items of each row, as in the C code fragment
36
for(i = 0;i<2;++i) { sum[i] = 0;
for(j = 0;j<3;++j)sum[i] += array[i][j];
}
to produce the result 3 12? How can we do it in J? What we have learned so far is not enough, but if we had a way to make monad +/ apply to 1-cells—if we could make monad +/ have rank 1—our problem would be solved: the implicit looping would cause each row to be summed and the results collected.
You will not be surprised to learn that J does indeed provide a way to apply monad +/ on 1-cells. That way is the rank conjunction " .
We will learn all about conjunctions later on—the syntax is a little different than for verbs—but for now, we'll try to understand this " . It's used like this:
u"n
to produce a new verb that is u applied to n-cells individually. This is a simple idea, but its ramifications spread wide. As a first example:
+/"1 i. 2 3 3 12
This is what we were looking for. It happened this way:
The verb rank is 1 and the noun rank is 2, so we will be applying the verb to 1-cells. The frame f is 2
|
|
|
|
|
|
|
Think of the operand as a list of 2 1-cells: |
|
0 1 2 |
3 4 5 |
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The verb monad +/ is applied to each cell: |
|
|
|
|
|
|
|
3 |
12 |
|
|
|
|
|
|
|
|
|
|
|
Since each result is an atom, i. e. a 0-cell, the |
|
3 12 |
|
|||
result is a list of 2 0-cells, i. e. an array of shape |
|
|
||||
2 |
|
|
|
|
|
|
Figure 3. Execution of +/"1 i. 2 3 |
|
|
|
|
|
Examples Of Verb Rank
Here are some more examples using a rank-3 array as data:
37
0 |
i. |
2 3 4 |
|
1 |
2 |
3 |
|
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
+/"1 i. 2 3 4 6 22 38 54 70 86
The verb rank is 1 and the noun rank is 3, so we will be applying the verb to 1-cells. The frame f is 2 3
|
|
|
|
|
|
|
|
|
|
|
|
|
Think of the operand |
|
|
|
0 1 2 3 |
|
4 5 6 7 |
|
8 9 10 11 |
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
as a 2×3 array of 1-cells: |
|
|
12 13 14 15 |
16 17 18 19 |
|
20 21 22 23 |
|
|||||
|
|
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The verb monad +/ is |
|
|
6 |
|
22 |
|
38 |
|
|
|
|
|
applied to each cell: |
|
|
54 |
|
70 |
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Since each result is an |
|
|
|
|
|
|
|
|
|
|
|
|
atom, i. e. a 0-cell, the |
|
|
|
|
|
|
6 22 38 |
|
|
|
||
result is a 2×3 array of |
|
|
|
|
|
|
54 |
70 86 |
|
|
||
0-cells, i. e. an array of |
|
|
|
|
|
|
|
|
|
|||
shape 2 3 |
|
|
|
|
|
|
|
|
|
|
|
|
Figure 4. |
Execution of +/"1 i. 2 3 4 |
|
|
38
+/"2 i. 2 3 4 12 15 18 21 48 51 54 57
The verb rank is 2 and the noun rank is 3, so we will be applying the verb to 2-cells. The frame f is 2
Think of the operand |
0 1 2 3 |
12 13 14 15 |
|
4 5 6 7 |
16 17 18 19 |
||
as a list of 2 2-cells: |
|||
8 9 10 11 |
20 21 22 23 |
||
|
The verb monad +/ is
applied to each cell. As
we have learned, this sums 12 15 18 21 48 51 54 57 the items, making each
result a rank-1 list
Since each result is a |
|
|
|
rank-1 list, i. e. a 1-cell, |
12 |
15 18 |
21 |
the result is a list of 2 |
48 |
51 54 |
57 |
1-cells, i. e. an array of |
|||
shape 2 4 |
|
|
|
Figure 5. Execution of +/"2 i. 2 3 4
+/"3 i. 2 3 4 12 14 16 18 20 22 24 26 28 30 32 34
The verb is applied to the single 3-cell. Its items, which are 2-cells, are added, leaving a single 2-cell as the result.
How about i."0 (2 2 2)—can you figure out what that will produce? (Notice I put parentheses around the numeric list 2 2 2 so that the rank 0 wouldn't be treated as part of the list)
39
The verb rank is 0 and the noun rank is 1, so we will be applying the verb to 0-cells. The frame f is 3
|
|
Think of the operand as a list of 3 0-cells |
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
2 |
|
2 |
|
|
||
|
|
(i. e. atoms): |
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
The verb monad i. is applied to each cell: |
|
|
|
|
|
|
|
|
|
|
|
|
0 1 |
|
|
0 1 |
0 1 |
|
|||
|
|
|
|
|
|
|
|
|
|
||
|
|
Since each result is a list, i. e. a 1-cell, the |
|
|
|
|
|
0 1 |
|
||
|
|
result is a list of 3 1-cells each with shape 2, i. e. |
|
|
|
|
|
0 1 |
|
||
|
|
an array of shape 3 2 |
|
|
|
|
|
0 1 |
|
||
|
|
Figure 6. Execution of i."0 (2 2 2) |
|
|
|
|
|
|
|
|
|
0 |
i."0 (2 2 2) |
|
|
|
|
|
|
|
|
||
1 |
|
|
|
|
|
|
|
|
|
|
|
0 |
1 |
|
|
|
|
|
|
|
|
|
|
0 |
1 |
|
|
|
|
|
|
|
|
|
|
If you worked through that, it might have occurred to you that the shape of each result cell depended on the value of the operand cell, and that if those cells had not been identical, there would be some rough edges showing when it came time at the end to join the dissimilar result cells together. If so, full marks to you! That can indeed happen. If it does, then just before the cells are joined together to make the final result, the interpreter will bulk up the smaller results to bring them up to the shape of the largest. First, if the ranks of the results are not identical, each result will have leading axes of length 1 added as needed to bring all the results up to the same rank (e. g. if one result has shape 2 5 and another has shape 5, the second will be converted to shape 1 5, leaving the data unchanged). Then, if the lengths of the axes are not identical, the interpreter will extend each axis to the maximum length found at that axis in any result: this requires adding atoms, called fills, which are always 0 for numeric results and ' ' for literal results. Example:
i."0 (0 1 2 3)
0 0 0 NB. original result was empty list; 3 fills added 0 0 0 NB. original result was 0; 2 fills added
0 1 0 NB. original result was 0 1; 1 fill added
0 1 2 NB. this was the longest result, no fill added
fndisplay—A Utility for Understanding Evaluation
J contains a script that we will use to expose the workings of evaluation. You define verbs which, instead of operating on their operands, accumulate character strings indicating what operations were being performed. This gives you a way of seeing the operations at different cells rather than just the results.
Start by loading the script:
40