Array notation: Difference between revisions
(→History: Acre, Dyalog, dzaima/APL, and BQN history) |
(→History: Getting complicated, time for a table) |
||
Line 45: | Line 45: | ||
[[dzaima/APL]] added support for vector notation with parentheses in 2018, namespaces and function arrays in 2019, and high-rank arrays with square brackets in 2020. [[BQN]] supported lists with angle brackets <source inline>⟨⟩</source> in its initial implementation in 2020; square brackets were reserved for high-rank array notation, which was implemented in 2022. | [[dzaima/APL]] added support for vector notation with parentheses in 2018, namespaces and function arrays in 2019, and high-rank arrays with square brackets in 2020. [[BQN]] supported lists with angle brackets <source inline>⟨⟩</source> in its initial implementation in 2020; square brackets were reserved for high-rank array notation, which was implemented in 2022. | ||
=== Language support === | |||
The following languages support list or vector notation in some form. In K, all APLs, and BQN, the separators <source inline>;</source> and <source inline>⋄</source> indicate any separator, including a line break. | |||
{| class=wikitable | |||
! Language !! Vectors !! High-rank !! [[Namespace]]s !! [[Function array]]s | |||
|- | |||
| [[Nial]] || <source inline>[,]</source> || || || {{Yes}} | |||
|- | |||
| [[A+]] || <source inline>[;]</source> || || || {{Maybe|First class}} | |||
|- | |||
| [[K]] || <source inline>[;]</source> || || || {{Maybe|First class}} | |||
|- | |||
| [[dzaima/APL]] || <source inline>(⋄)</source> || <source inline>[⋄]</source> || <source inline>(key:val⋄)</source> || {{Yes}} | |||
|- | |||
| [[BQN]] || <source inline>⟨⋄⟩</source> || <source inline>[⋄]</source> || <source inline>{key⇐val⋄}</source> || {{Maybe|First class}} | |||
|- | |||
| [[Dyalog]] (proposed) || <source inline>(⋄)</source> || <source inline>[⋄]</source> || <source inline>(key:val⋄)</source> || {{No}} | |||
|} | |||
"First class" indicates that functions are first class, so that an array of functions can be formed directly; in Nial and dzaima/APL function arrays are a special form that can be applied to arguments to return a list of results. BQN's namespaces don't use a dedicated construction; instead, any block (like a [[dfn]]) with <source inline>⇐</source> statements returns a namespace reference. | |||
== References == | == References == | ||
<references/> | <references/> | ||
{{APL syntax}}[[Category:APL syntax]][[Category:Nested array model]] | {{APL syntax}}[[Category:APL syntax]][[Category:Nested array model]] |
Revision as of 21:37, 19 August 2022
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.
Examples
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:
poss←1 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)]
Description
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.
History
- See also Array notation in Dyalog APL
One-dimensional list syntax with surrounding brackets and delimiters, matching sequence notation in mathematics, is common in programming. It appears as early as ALGOL 68 with parentheses, and square-bracket lists or arrays feature in languages from the 1970s such as ML and Icon. MATLAB uses matrix syntax with square brackets, semicolons to separate rows, and commas to separate columns.
List notation appears in Nial, with brackets and commas like [a,b,c]
, and in A+ and K, with parentheses and semicolons 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. In all three languages this syntax can be used with functions, as functions are first-class in A+ and K, and arrays of functions called "atlases" can be used for specific purposes in Nial.
The first published proposals that influenced Dyalog APL's array notation were made by Phil Last at Dyalog '15 and later in Vector Journal.[1][2] Last also used the design in Acre Desktop, a project manager for Dyalog APL, to support storing constant arrays and namespaces in text files. Following the conference presentation, Adám Brudzewsky began work on array notation and presented on it in a series of conferences, initially using parentheses for the high-rank notation[3] and later returning to square brackets.[4][5] Because Last's use of ←
to separate namespace keys from values prevented lists from including arbitrary expressions (which might contain assignment), he proposed a change to :
as in JSON. Dyalog APL 18.0, released in 2020, included support for array notation in source files loaded by Link, but not in the language itself.[6]
dzaima/APL added support for vector notation with parentheses in 2018, namespaces and function arrays in 2019, and high-rank arrays with square brackets in 2020. BQN supported lists with angle brackets ⟨⟩
in its initial implementation in 2020; square brackets were reserved for high-rank array notation, which was implemented in 2022.
Language support
The following languages support list or vector notation in some form. In K, all APLs, and BQN, the separators ;
and ⋄
indicate any separator, including a line break.
Language | Vectors | High-rank | Namespaces | Function arrays |
---|---|---|---|---|
Nial | [,] |
Yes | ||
A+ | [;] |
First class | ||
K | [;] |
First class | ||
dzaima/APL | (⋄) |
[⋄] |
(key:val⋄) |
Yes |
BQN | ⟨⋄⟩ |
[⋄] |
{key⇐val⋄} |
First class |
Dyalog (proposed) | (⋄) |
[⋄] |
(key:val⋄) |
No |
"First class" indicates that functions are first class, so that an array of functions can be formed directly; in Nial and dzaima/APL function arrays are a special form that can be applied to arguments to return a list of results. BQN's namespaces don't use a dedicated construction; instead, any block (like a dfn) with ⇐
statements returns a namespace reference.
References
- ↑ Phil Last. APL Array Notation (transcript). Dyalog '15.
- ↑ Phil Last. A Notation for APL array Embedding and Serialization. Vector Journal, Volume 26, number 4. British APL Association. 2016.
- ↑ Adám Brudzewsky. Literal Notation for Arrays and Namespaces (slides). Dyalog '17.
- ↑ Adám Brudzewsky. Array Notation Mk III. Dyalog '18.
- ↑ Adám Brudzewsky. A Notation for APL Arrays. APL-Journal, Volume 2020, number 1-2. APL-Germany e.V. 2020.
- ↑ Adám Brudzewsky. Array Notation RC1 (slides). Dyalog '20.