
- •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
How Names (Identifiers) Get Assigned
Assignment in J is performed by expressions of the form name =. entity NB. private
and
name =: entity NB. public
Names assigned by public assignment are visible outside the entity in which they are defined; names assigned by private assignment usually are not; we will learn the details when we discuss modular code. The difference between the two forms of assignment is in the character following the = . Just as in C, the assignment expression is considered to produce as its result the value that was assigned, so expressions like
a =. 1 + b =. 5
are legal. J calls =. and =: copulas. Just as in C, the entity that is assigned to the name can be the result of evaluating an expression.
There are a number of additional capabilities of J assignment that you can read about in the Dictionary. One that has no counterpart in C is that the name being assigned can itself be a variable, i. e. you can calculate the name that you want to assign the value to.
The value assigned can be a noun (object), verb (function), adverb, or conjunction; the name then becomes whatever part of speech was assigned to it (even if it was previously defined as a different part of speech!). For example,
n =: 5
creates a noun, and
v =: verb define x. + y.
)
creates a verb (more below).
Note: the J Dictionary uses the terms 'local' and 'global' instead of 'private' and 'public'. I think 'private' and 'public' are more accurate terms, because there is another dimension to name scope in J, using the J notions locale and path, that causes public variables to be visible only in certain entities. It will be a long time before we learn about locales; until then, public names will be global.
Order of Evaluation
Forget the table of operator precedence! All J verbs (functions and operators) have the same priority and associate right-to-left. For example, a * b + c is equivalent to a * (b + c), not (a * b) + c. Use care when copying mathematical formulas. Note that the negative sign _ is a part of the number, not a verb.
_5 + _4 is _9, while -5 + -4 is _1.
The executable bits of a sentence (statement) are called fragments (subexpressions). A verb with its operand(s) is a fragment, as is a copula with its name and value. We will meet other types of fragment later. Execution of a sentence consists of the right-to-left execution of its fragments, with the result of each fragment's execution replacing the fragment and being passed as an operand into the next fragment. The result of the last execution becomes the result of the sentence. This result is usually
10
a noun but it can be any of the primary parts of speech. As an example, execution of the sentence
a =. 3 + b =. 4 * 5
consists of execution of the following fragments: 4 * 5 with result 20; b =. 20 with result 20; 3 + 20 with result 23; a =. 23 with result 23 . The names a and b are assigned when the assignment fragments are executed.
If a verb has a noun on its left, it is executed as a dyadic verb with a left and right operand. If the verb does not have a noun on its left, it is executed as monadic with just a right operand. You must know the part of speech of the names in a sentence to understand the execution order. In the sentence
result =. name1 verb2 5
you must know name1 is a verb, in which case verb2 is executed monadically and the result is name1(verb2(5)), or name1 is a noun, in which case verb2 is dyadic and the result is (name1 verb2 5) .
How Names Are Substituted
When a sentence contains names, the sentence is executed as if each name were enclosed in parentheses and then replaced by its value. If a has the value 4 and b has the value 5,
a + b
is equivalent to
(4) + (5)
Enclosing the value in parentheses is a necessary step to get the order of evaluation right, just as in ordinary arithmetic, where if a is x+2 and b is y, ab is not x+2y, but (x+2)y.
If you read the Dictionary you will soon encounter the example mean=: +/ % #
mean 2 3 4 5 6
4
and you will probably follow in the footsteps of countless J neophytes, type into your J session
+/ % # 2 3 4 5 6
0.2
and be baffled by the result. Why 0.2 instead of 4? Because you left out the parentheses:
(+/ % #) 2 3 4 5 6
4
In this book, the discussion of what (+/ % #) means will be deferred for 200 pages, until the chapters on Tacit Programming. At that time it will also be revealed that our notion of replacing a name with its parenthesized value is a simplification of the actual process of execution. Forget that for now—you are highly unlikely to encounter a situation where the simplification leads you into error.
Using the parenthesized-substitution rule, we can justify the difference between
11
4 5 and a b
when a is 4 and b is 5 . 4 5 is two adjacent numbers, which are always treated as a single word. a b is equivalent to (4) (5), which is not two adjacent numbers but rather a syntax error.
What a verb (function) looks like
As we saw, a J verb (function) is defined by lines that look like: name =: verb define
J sentences here
)
The result of the verb define is a verb, and normally you will assign the result to a name so you can execute the verb by name when you need it. Subsequent lines, starting with the one after verb define and ending before the next line containing only the word ')', are read and saved as the text of the verb (heaven help you if you leave out the )!). The verb is not 'compiled'—only the most rudimentary syntax checking is performed; the text is saved and will be interpreted when the verb is executed.
Each line of the verb is a sentence (statement). The result of the last sentence executed becomes the result of the whole verb (this is not precisely true but it's close enough for now—details will be revealed in 'Control Structures').
Since a J verb has only one or two operands, there is no need for you to provide a list of parameter names as you do in a function definition in C; instead, J names them for you. At the start of a verb's execution, the private name y. is initialized with the value of the right operand of the verb. If the verb is dyadic, the private name x. is initialized with the value of the left operand. Many programmers like to start their verbs by assigning these values to more descriptive names.
If your verb is going to define only a monadic or dyadic form, you should use monad define or dyad define instead of verb define . If you are going to define both valences, the way to do so is:
name =: verb define monadic case here
:
dyadic case here
)
where a line with the single word : separates the two cases. If you use verb define and don't have the :, the verb will be monadic.
If your verb is only one line long (not at all unusual in J!) you can define it all in one line by using the appropriate one of the forms
12
name =: monad : 'text of verb' name =: dyad : 'text of verb'
Running a J program
No compiling. No linking. No makefiles. No debugger. You simply type J sentences and the interpreter executes them and displays any result. At the very simplest, you can use it as a desk calculator:
22 + 55
77
J prints 3 spaces as a prompt, so when you scroll through the log of a session, your input will be indented 3 spaces while J's typeout will be unindented. The result of a sentence typed on the keyboard is displayed, except that to avoid excessive typeout nothing is displayed if the last fragment executed in the sentence is an assignment. If you are at the keyboard while you are reading this book, you can type the examples and see the responses, or experiment on your own.
Here is a simple program to add twice the left argument to three times the right argument:
add2x3y =: dyad : '(2 * x.) + 3 * y.'
We can run this program by giving it operands:
1 2 3 add2x3y 4 5 6
14 19 24
Instead of simply displaying the result, we can assign it to a noun: a =: 1 2 3 add2x3y 4 5 6
We can inspect the value assigned to the noun by typing the name of the noun: a
14 19 24
We can use the noun in an expression:
2 * a
28 38 48
We can create a new verb that operates on the noun: twicea =: monad : '2 * a' twicea ''
28 38 48
Notice the '' after the invocation of twicea. Remember, to invoke a verb you must give it an operand, even if the verb doesn't use an operand. '' is just an empty string; 0 or any other value would work too. If you leave out the operand, J will show you the value of the name; since twicea is a verb, its value is the definition of the verb:
twicea 3 : '2*a'
Of course, in any practical application you will need to have most of your programs in a library so you can quickly make them all available to J. J calls these libraries scripts (filename extension '.ijs') and runs them with the load verb, for example:
13