Array notation

From APL Wiki
Revision as of 02:27, 19 August 2022 by Marshall (talk | contribs) (list → vector)
Jump to navigation Jump to search

Array notation is a way to write most arrays literally, with no or minimal use of primitive functions, possibly over multiple code lines. It differs from the strand notation existing since APL\360 in that it can be used to write arrays of rank greater than one. Array notation is supported in dzaima/APL and BQN, and by some tools for Dyalog APL, where it is planned as an eventual language feature.

Array notation generally consists of a vector notation written with parentheses (), roughly equivalent to stranding, and a high-rank notation using square brackets [], indicating the Mix of a vector. It may also support namespaces, with dzaima/APL and Dyalog using name:value syntax in parentheses for this. In each case, elements are separated by statement separators such as or line breaks.


Medium-sized array constants are often needed in code. Due to the lack of a native multi-line notation, programmers have resorted to various ad-hoc methods of approximating such, usually at the cost of reduced readability. A very common technique is repeated concatenation:

poss1 2'fns'  ((0 1)(0.7 0)(0.7 0)×size)
poss   'fnd'  ((0 1)(0   0)(0   0)×size)
poss   'lines'((0 0)(0.7 0)(0.7 0)×size)
poss   'lnd'  ((0 0)(0   0)(0   0)×size)

Array notation allows this multi-line construction to be made with a single assignment, and also provides an alternate syntax for the inner vectors of vectors:

poss['fns'  ((0 1  0.7 0  0.7 0)×size)
      'fnd'  ((0 1  0   0  0   0)×size)
      'lines'((0 0  0.7 0  0.7 0)×size)
      'lnd'  ((0 0  0   0  0   0)×size)]


The notation is added to the language by giving meaning to previously invalid statements. The added syntax consists of three constructs that are currently SYNTAX ERRORs:

  • broken round parentheses
  • broken square brackets
  • empty round parentheses: ()

where broken means interrupted by one or more diamonds () or line breaks (outside of dfns).

  • A broken round parenthesis creates a namespace if every diamond/line break-separated statement is a name-value pair.
  • A broken round parenthesis creates a vector if every diamond/line break-separated statement is a value expression. In that case, every such statement forms an element in the resulting vector.
  • A broken square bracket creates a an array where every diamond/line break-separated statement forms a major cell in the resulting array.
  • () is equivalent to (⎕NS 0⍴⊂'')
  • A name-value pair consist of a valid APL identifier, followed by a : and a value expression.


See also Array notation in Dyalog APL

A predecessor of array notation is the list notation used by both A+ and K, with parenthesis-and-semicolon syntax like (a;b;c). In A+ this is related to bracket indexing and an "expression group" notation written with curly braces and semicolons. It allows line breaks, but in addition to rather than in place of semicolons. The later K version corresponds more closely to APL: the semicolon is a statement separator and is interchangeable with a line break, and because K represents arrays with nested lists, it corresponds to both vector and high-rank array notation.


APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structure