
- •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
Monad ": also converts boxed operands to the character arrays, including boxing characters, that we have seen in the display of boxed nouns.
Format an Array: 8!:n
To format an entire array, perhaps for display using the Grid Control, use the 8!:n family of foreigns. x 8!:0 y formats each atom of y into a boxed character string under control of the format phrases in x :
'3.0,m<$(>p<$>n<)>q< >c14.2' 8!:0 (10 500.32 ,: 22 _123456)
+--- |
+-------------- |
+ |
| 10| |
$500.32 | |
|
+--- |
+-------------- |
+ |
| |
22| |
$(123,456.00)| |
+ |
---+-------------- |
+ |
The format phrases are separated by commas; here there were two, the simple 3.0 and the more advanced m<$(>p<$>n<)>q< >c13.2 . The last thing in a format phrase is the field-width specifier w.d, which means what you expect. If w is 0, a computed width is used for the entire column, while if w. is omitted, the minimum width is used for each number individually.
Preceding the w.d field come the modifiers. They include the single letters c (put commas in the formatted number) and l (left justify), and a number of modifiers, called format strings, that have a character-string value. These are specified as m<string>, m(string), or m[string], where m is the one-character name of a format string— you pick a bracket character <, (, or [ that doesn't occur in your string.
The format-string modifiers replace the default format strings for the column, except for the s modifier described presently. The format strings have the names b, d, m, n, p, q, r, and s. The default values are s='e,.-*', b='0', d=infinity, m=(2{s if n is omitted, '' if n is given), others='' . The s modifier is set differently: it is in the form s[abab...] where each a character is one of the default e,.-* and the following b character replaces that a character in the format string.
A number is classified according to its value; the value is converted to the value string v according to w.d; then the formatted result is created from a combination of the format strings and the value string, based on the value:
Infinite/indeterminate: d (default is _, __, or _. according to value) Negative: mvn
Zero: b Positive: pvq
The elements of s give characters to use for exponent, thousands separator, decimal point, minus sign, and insufficient field-width. If the formatted result calculated above is shorter than the field width, the field is first initialized with the r format string, repeated cyclically as necessary, and the formatted value is overlaid on that.
129
There are three verbs in the 8!:n family. All have infinite rank. 8!:0 produces a box for each atom of y, 8!:1 produces a box for each column of y, and 8!:2 produces an unboxed character array:
+ |
'3.0,14.2' 8!:0 (10 500.32 ,: 22 _123456) |
|
---+-------------- |
+ |
|
| 10| |
500.32| |
|
+ |
---+-------------- |
+ |
| 22| |
-123456.00| |
|
+ |
---+-------------- |
+ |
+ |
'3.0,14.2' 8!:1 (10 500.32 ,: 22 _123456) |
|
---+-------------- |
+ |
|
| 10| |
500.32| |
|
| 22| |
-123456.00| |
|
+ |
---+-------------- |
+ |
|
'3.0,14.2' 8!:2 (10 500.32 ,: 22 _123456) |
|
|
10 |
500.32 |
|
22 |
-123456.00 |
Format binary data: 3!:n
If you need to write numbers to files in binary format, you need something that will coerce your numbers into character strings without changing the binary representation. J provides the foreigns 3!:4 and 3!:5 for this purpose. Each is a family of conversions, invoked as a dyad where the left operand selects the conversion to be performed.
2 (3!:4) y converts the numeric list y to a character string, representing each item of y by 4 characters whose values come from the binary values of the low-order 32 bits of y :
16b31424334
826426164
This is an integer whose value is 0x31424344. We can convert it to a character string:
2 (3!:4) 826426164 4CB1
The 4 characters (with values 0x34, 0x43, 0x42, 0x31) correspond to the bits of the number 826426164 in little-endian order.
We can use a. i. to look at the codes for each character in case they are not printable:
a. i. 2 (3!:4) 1000 100000 232 3 0 0 160 134 1 0
232 3 0 0 corresponds to 1000 and 160 134 1 0 to 100000 .
_2 (3!:4) y is the inverse of 2 (3!:4) y, converting a character string to integers:
_2 (3!:4) '4CB15CB1' 826426164 826426165
The other integer conversions are similar. bits of each item of y to 2 characters, and _1
1 (3!:4) y converts the low-order 16 (3!:4) y converts back to integers, 2
130
characters per integer. 0 (3!:4) y is like _1 (3!:4) y but the integers are unsigned (i. e. in the range 0-65535).
The floating-point conversions are analogous. 2 (3!:5) y converts each item of y to 8 characters representing long floating-point form, and _2 (3!:5) y converts back; 1 (3!:5) y and _1 (3!:5) y use 4-character short floating-point form.
printf, sprintf, and qprintf
When you need formatted lines for printing you may feel at home using printf and sprintf, which work like their C counterparts. printf displays a line, while sprintf produces a string result:
'The population of %s is %d\n' printf 'Raleigh';240000 The population of Raleigh is 240000
s =. 'The total of %j is %d.\n' sprintf 1 2 3;+/1 2 3 s
The total of 1 2 3 is 6.
You need to execute load 'printf'
to get the printf verbs defined. J's printf contains a few features beyond C's such as the %j field type seen above. You should run the Lab 'Formatting with printf' for details.
One feature with no analogue in C is qprintf, which produces handy typeout for debugging:
a =. 3 4 5 [ b =. 'abc';'def';5 [ c =: i. 3 3 qprintf 'a b c '
a=3 4 5 b= +---+---+-+ |abc|def|5| +---+---+-+ c=
0 1 2
3 4 5
6 7 8
qprintf is described in the 'Formatting with printf' lab.
Convert Character To Numeric: Dyad ".
Dyad ". has infinite rank. x ". y looks for numbers in the words of y, which must be a character array. Words representing valid numbers are converted to numbers; words not representing valid numbers are replaced by x . If 1-cells of y have differing numbers of numbers, x is used to pad short rows. 'Valid numbers' to dyad ". are anything you could type into J as a number, and more: the negative sign may be '-' rather than '_'; numbers may contain commas, which are ignored; and a number may start with a decimal point rather than requiring a digit before the decimal point. With the
131
relaxed rules you can import numbers from a spreadsheet into J, using x&". to convert them to numbers:
999 ". '12 .5 3.6 -4 1,000 x25'
12 0.5 3.6 _4 1000 999
All legal numbers except for 'x25' .
If your character string is a valid J number, either monad ". or dyad ". can be used to convert it, but dyad ". is much faster.
132