- •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
load 'system\packages\misc\jforc.ijs'
load reads lines from the script and executes them. These lines will normally be all the verb and noun definitions your application needs, possibly including load commands for other scripts. A script may end with a line executing one of the verbs it defined, thereby launching the application; or, it may end after defining names, leaving you in control at the keyboard to type sentences for J to execute.
Note: Names defined by private assignment (using =.) when a script is loaded are not available outside the script. If you want to define names for use elsewhere, make sure you use =: for your assignments within a script.
If you are used to debugging with Visual C++™ or the like, you will find the environment less glitzy and more friendly. If you want to change a verb (function), you simply edit the script, using the editor of your choice (I use the built-in editor provided with J), and rerun it. The verb will be updated, but all defined nouns (objects) will be unchanged. Even if you are running a large application—yea, even if the application is in the middle of reading from an asynchronous socket—you can change the program, without recompiling, relinking, or reinitializing. If you'd like to add some debugging code while the system is running, go right ahead. This easy interaction with an executing program is one of the great benefits of programming in J.
Interrupting Execution
If a J verb is taking too long to run, signal it to stop by running the Jbreak program that is supplied as part of the J installation. Control will return to the keyboard.
Errors
When a sentence contains an error, J stops and displays the sentence along with a terse error message. Refer to the chapter on Error Messages for explanation of the error.
The Execution Window; Script Windows
When J starts it displays its execution window. The title of the execution window ends with the characters '.ijx'. The only way to have a sentence executed is to have the sentence sent to the execution window. The simplest way to do that is by typing the sentence into the execution window, as we have been doing in the examples so far.
The execution window is an edit window and a session log as well as a place to type sentences for execution. If you put the cursor on some line other than the last and press ENTER, the line you were on will be copied to the bottom of the session log as if you had typed it for execution. You can then edit the line before pressing ENTER again to execute it.
For convenience in editing, you may create other windows which will be script windows. Usually these windows will contain J scripts that you are working on, and the editor that manages the script windows is familiar with the syntax of J. You create a script window by clicking File on the Menu Bar and then selecting New ijs, Open, or Recent.
14
Sentences that you type into a script window are not automatically executed by J; you must copy them into the execution window to have them executed. You can use the script-window editor to send lines from a script to the execution window: click Run on the Menu Bar and then File, Selection, or Window as appropriate.
To run a selection of lines from a script window, be sure to use Run|Selection rather than cut-and-paste. If you paste a number of lines into the execution window, only the last one will be executed.
It is important to remember that the script windows exist only for your convenience in editing and are not used during execution. If you make a change to a script window, you need to Run that window to cause the lines in the script to be executed.
If you are debugging a script and you remove a definition from the script and Run it, the definition will not be removed from the J session. Running the script is like entering the sentences one by one from the keyboard, and not-defining the name does nothing to expunge an established definition. To remove the old definition, use
4!:55 <'expiredname' or start a fresh session of J.
Names Defined at Startup
When J starts, a number of useful names are defined. Rather than discuss them all, I will show you how they come to be defined so you can study them when you need to.
When J starts, it executes the script
J-directory\system\extras\config\profile.ijs which then executes the script J-directory\system\extras\util\boot.ijs . boot.ijs in turn executes a series of scripts in J-directory\system\main which define the starting environment. Look at these scripts to see what they define.
If you want to add your own initial definitions, do so either by adding commands at the end of profile.ijs or by creating your own startup script in
J-directory\system\extras\config\startup.ijs .
Step-By-Step Learning: Labs
The Labs are interactive demos describing various topics in J. To run the lab for printf, start a J session, on the menu bar select Studio|Labs…, then select the lab you are interested in, then press 'Run'. The lab provides explanatory text interspersed with examples executed in your J session which you are free to experiment with as you step through the lab.
I recommend that every now and again you tarry a while among the labs, running whichever ones seem interesting. Much of the description of the J system can be found only there.
15
J Documentation
The J documentation is available online. Pressing F1 brings up the Vocabulary page, from which you can quickly go to the Dictionary's description of each J primitive. At the top of each page of documentation are links to the manuals distributed with J: these are:
The Index to all documentation;
The User Manual which describes components of J that are not in the language itself, including system libraries and external interfaces;
The J Primer, an introduction to J;
J Phrases, a collection of useful fragments of J (you will need to finish this book before trying to use J Phrases);
The J Dictionary, the official definition of the language; Release Notes for all releases of J;
A description of foreign conjunctions (!:);
A description of the operands to the wd verb (Windows interface).
Getting Help
Your first step in learning J should be to sign up for the J Forum at www.jsoftware.com. A great many experienced J users monitor messages sent to the Forum and are willing to answer your questions on J, from the trivial to the profound.
16