Essays/JAspects

From J Wiki
Jump to navigation Jump to search

Coming to J from a background of ten different programming systems, I nonetheless encountered almost-familiar features which required some study to apply properly, and some new ideas to expand my knowledge of what could be done more efficiently. These notes relate to some of the basics.

Context

  • A J session operates in a "workspace" of definitions and values
  • A session may use one or more windows for entry and output display
  • A session window record may be saved for future reference
  • Data values may be saved between sessions only by explicit action
  • Program-operation definitions are copied to and from script files
  • Script files may be prepared with the J editor or any other
  • Except in definition mode, each line is interpreted after entry
  • Formatted results may be captured from screen or written externally

Syntax

  • Each sentence must be contained on a single line
  • On any line, text following symbol NB. is commentary
  • Empty lines are acceptable


  • Brackets, braces, commas, and semicolons do not delimit groups
  • Period and semicolon also appear in names of many J symbols


  • Numbers must show a digit before any decimal point
  • Negative numbers are preceded by a "low minus" (underscore)
  • Names, symbols, and values may be separated by 0 or more blanks
  • Numbers must be separated from any following unrelated letter
  • Numbers may be decimal, rational, exponential, extended precision
  • Complex numbers may be specified, either Cartesian or polar


  • Stored character values may contain any character in "alphabet" a.
  • Character-literal expression may not contain newline or return


  • Lists of numbers are separated/joined by blanks, not commas
  • Numbers separated by commas specify re-computation at each use
  • A number list following a (rank) number needs a separator
  • Operations may apply to a number list without need for parentheses
  • Mere succession of computed values does not specify a list


  • All operations expect one data-value argument on the right
  • Many operations also accept or require a data argument on the left
  • A named program is activated via the same syntax as for symbols
  • Arguments may be atomic or arbitrarily structured as needed
  • A verb must be supplied some argument value to be executed
  • A symbol with only a right argument will be interpreted monadically
  • Presence also of a left argument will cause dyadic interpretation


  • There are no execution precedences among operations (verbs)
  • Parentheses enclose expressions to be evaluated before others
  • In a multi-operation expression, the right-most evaluates first
  • Evaluation results are substituted in the remaining phrase


  • Modifying adverbs and conjunctions do have "precedence" over verbs
  • Conjunctions and/or parentheses form new compound operations
  • Conjunctions are required for most non-standard noun-verb sequences
  • Multiple-verb sequences are evaluated by hook and/or fork rules


  • A user-defined value or operation name must start with a letter
  • A name may contain mixed-case letters, digits, one embedded underscore
  • No legal user-definable names are reserved for system use
  • Locale tags distinguish similar names loaded in different locales
  • A name ending with a tag between underscores belongs to that locale
  • A locale-valued variable may follow a double underscore as an ending tag
  • Re-defining a name used in another loaded script may cause confusion

Data structure

  • Every data value in J is an array
  • Each array has a rank of 0 (atom), 1 (list), 2 (table), or more
  • A positive-rank array is stored as a list of lower-rank "items"
  • All items in an array are of one type--numeric, character, boxed, or symbol
  • Every array has a rectangular shape--the sizes of successive axes
  • Each array has a level (box depth) of 0 or more


  • Arrays of level 2 or higher cannot be entered directly
  • Lists and other arrays may be constructed with Append, Link, etc.
  • Tables may be constructed with Stitch, Laminate, Table, etc.
  • Sub-arrays may be rotated at any rank level
  • Array axes may transposed into any order


  • The Box of an array creates an atom, of level one greater
  • The Open of a box array may be of one level less, but never negative
  • The Open of an array will be filled to the greatest rank found
  • The Open of a box array will fail when it finds mixed data types
  • A box value is always distinct from and unequal to its Opened form


  • "Symbol" values are character strings beginning with ` (accent grave)
  • Symbol values may only be created, structured, stored, selected, matched


  • Specified elements or sub-arrays may be copied from an array
  • Consecutive head or tail elements may be selected along any axis
  • Consecutive head or tail elements may be omitted from a copy
  • Elements may be selected by index along an axis or axes
  • Indices for an n-element axis always run 0 through n-1 (0-origin)
  • Indices _1 through -n run backward from the end of the axis


  • Elements may be selected from a box at any level by specifying path
  • An array may be copied with specified substitutions (Amend)
  • The data value of an evaluated expression may be stored as a named noun
  • An array value may be modified only by total replacement (Is)


  • Any axes of an array may have length 0--a form of empty array
  • Reducing axis length by Drop, even to 1 or 0, does not reduce rank
  • Selecting with single atoms from n axes yields a subarray of rank reduced by n
  • An array with a single item, a singleton, is not an atom if of positive-rank
  • A singleton may be processed like an atom, or may give unwanted rank


  • Displays of certain arrays are problematic:
  • --A leading axis of length 0 show no displayed lines
  • --Leading axes, if all of length 1, are not apparent on displayed values
  • --The shape of a character array with many blanks may not be apparent
  • --Distinguishing between character and numeric is difficult in boxes
  • --The rank and shape of a displayed empty value may not be apparent

Operation application

  • J-verbs operate on programmed, stored, and/or computed-result values
  • Only Is (Gets)--local or global--copies a value to named storage
  • A final computed entry, not ending as stored, will be displayed
  • No basic operation specifies output
  • --The "Foreign" verb connects to system devices and values, other programs
  • --smoutput causes immediate display of its argument during program execution


  • Each verb has default ranks of application: monadic, or left and right
  • A Rank adverb may be applied to any verb to change from its defaults
  • A Rank adverb may be applied to any noun to direct its use


  • A rank-n array may be viewed as a frame of rank n-2 holding arrays of rank 2
  • An operation on rank-2 subarrays composes results in the same frame
  • With two arguments, one frame must match a prefix of the other
  • --Values in the smaller frame will repeat to fill the larger
  • --Smaller partial results will be filled to shape of largest
  • Performing operations within box values obviates fill above that level


  • Most arithmetic operations use rank 0, applying to individual atoms
  • Count and Shape Of use infinite rank (_), measuring a whole array
  • Rotate uses ranks 0 1; multiple left values give multiple rotations


  • A rank 1 (or _1) operation on a rank-2 table treats whole rows together
  • --A whole-row operation may, in turn, process individual atoms
  • -- +/ yields sums of table rows ("down columns"): row+row+...+row
  • -- +/"1 yields sums "along" each table row: (item+...+item),...,(item+...)


  • Some verbs apply rank-1 structural arguments to successive axes

Program structure

  • The Right verb (]) can isolate an expression yet deliver its value
  • The Left verb ([) can hide the result of a trailing expression
  • Display of a result may be requested:
  • --Interactively, by prefixing either Identity (monadic ] or [)
  • --Within program operation, by prefixing smoutput


  • An expression accepting argument(s) acts as a verb definition
  • Longer programs may be introduced by the Explicit verb (:)
  • --Its right argument may be a quoted expression, or 0 for following keyed input
  • --A line of a single closing parenthesis terminates definition input
  • --Successive lines become sentences in the stored program
  • Control phrases (if., for., while., etc.) may punctuate the program
  • An if-test value is True--unless it has a leading atom of unboxed 0


  • Arguments to a defined verb initialize local variables y (right), x (left)
  • --Values assigned locally (=.) are not accessible anywhere outside its verb
  • A break. can terminate iteration before the stated end condition
  • A return. can terminate verb execution before reaching its physical end
  • The last non-control expression evaluated becomes the verb's result

Tacit definition--not naming argument values

  • A sequence of primitives may define a verb, adverb, or conjunction
  • --A defined verb may combine its arguments via other verbs and modifiers
  • --The most common forms are a nest of Atops, and a chain of hook and forks
  • The first-acting verbs act on the external argument(s)
  • Later verbs may apply to the results, when indicated by Atop, At, Hook, or Fork
  • --The full right argument to Atop or At usually needs parenthesization


  • In a three-verb sequence, a fork:
  • --Last and third-last verbs act on argument(s), middle one on their results
  • --A verb may be Right (]) or Left ([) to select either argument
  • A verb may introduce an additional value, via Bond
  • --A value Bonded to Left (2&[) yields itself, except with another left argument
  • --A value Bonded to Left Atop Right (2&[@]) yields itself, always
  • --Fork arguments are not delivered into a phrase Atop a fork's verb
  • x f@g y is equivalent to f (x g y) or x f@([ g ]) y
  • Some nests of Atops can be strung out as successive forks


  • A verb in a fork may itself be a fork, often parenthesized
  • The leftmost verb of a fork may be Cap ([:) to force the middle one monadic
  • --Omitting a leftmost verb leaves a hook, which presumes it to be Right

Contributed by Art Anger