A woodblock print on a cream scroll: five trees of glowing blue cubes rise from one tangle of shared roots, from a gnarled old tree hung with small glyph ornaments, through two slim angular trees and a tall neat one, to a small sapling; an arrow-shaped stream beneath them flows right to left

Episode 6 of my video series on array programming is a tour of the array-language family. Episode 5 was about idioms, short phrases for patterns that keep coming back. This one is about the languages behind those idioms: APL, J, K, BQN and X_eTaL, side by side on the same small problems.

Five languages

  • APL showed that a language could treat arrays and math as its basic vocabulary.
  • J explored an ASCII notation, and powerful function composition.
  • K kept a compact vocabulary, and short expressions.
  • BQN, after them, revisited many of these ideas with a modern notation and combinators.
  • X_eTaL takes ideas from this family, but makes different choices: checked types, ASCII source drawn with typography, and composition.

X_eTaL is not a replacement for any one of them. It borrows from the family, then makes its own choices.

Running totals in five languages

Start with running totals, from the last episode. Every language here has a scan. The spelling differs; the answer is the same.

APL      +\3 1 4 1 5 9
J        +/\ 3 1 4 1 5 9
K        +\3 1 4 1 5 9
BQN      +`3‿1‿4‿1‿5‿9
x ← 3 1 4 1 5 9
'+ s̲\ x   ⍝ 3 4 8 9 14 23

All five give 3 4 8 9 14 23. BQN writes a list with ligatures, 3‿1‿4‿1‿5‿9, where the others use spaces.

Notation: reverse

The video's reverse panel: APL and BQN with the circle-bar glyph, J and K with ASCII bars, and X_eTaL's underlined rev, its boxed result 9 5 1 4 1 3
Reverse in five languages, from the video. Click to enlarge.

Reverse shows the notation. APL and BQN use a glyph, a circle with a bar. J and K use ASCII symbols. X_eTaL uses a word, with its first letter underlined.

APL      ⌽3 1 4 1 5 9
BQN      ⌽3‿1‿4‿1‿5‿9
J        |. 3 1 4 1 5 9
K        |3 1 4 1 5 9
r̲ev x   ⍝ 9 5 1 4 1 3

Right to left

All five read from right to left, with no precedence among functions. So three plus four comes first, then two times seven gives fourteen. In J and K, times is typed as a star.

APL      2×3+4
J        2*3+4
K        2*3+4
BQN      2×3+4
2 × 3 + 4   ⍝ 14

X_eTaL’s source has the star too; it is drawn as ×.

A mean, as a train

A train joins functions without naming the argument. Sum, divided by count, is the mean. APL, J and BQN write it as a train; X_eTaL writes the same fork in square brackets. K writes a small function instead, with a named argument, x.

APL      (+/÷≢)3 1 4 1 5 9
J        (+/ % #) 3 1 4 1 5 9
BQN      (+´÷≠)3‿1‿4‿1‿5‿9
K        {(+/x)%#x}3 1 4 1 5 9
['+ r̲/ ÷ t̲ally] x   ⍝ 3.8333333333333335

The APL train is Dyalog APL’s, from 2014. APL2-style APLs such as GNU APL have no trains, so there the mean names its argument, as in K: (+/x)÷⍴x gives 3.833333333.

One catch if you try the K line: which K you run matters. ngn/k gives 3.8333333333333335, but Kona, an older open-source K, divides two integers into an integer and gives 3.

Explicit or tacit

The video's explicit-or-tacit panel: the spread in J both ways, and in X_eTaL as a braced lambda and as a bracketed train, with the inferred type Num a => a -> a and the result 8
Spread, explicit and tacit, from the video. Click to enlarge.

What is tacit programming? Tacit means unspoken: a tacit definition never mentions its argument. Instead of saying what to do with x, it says how functions combine, and the argument is implied. Elsewhere it is called point-free style; the idea goes back to function-level programming, as in John Backus’s FP.

In the array languages, functions combine by position, in trains. Three functions in a row make a fork: (f g h) x means (f x) g (h x). So (+/ % #) is the sum of x, divided by the count of x: the mean. Two in a row make a hook in J: (f g) x means x f (g x). Trains came with J, around 1990; Dyalog APL added them in 2014, and BQN has them too, and X_eTaL writes its trains in square brackets.

The gain is a definition that reads as what it computes (“max minus min”), with nothing to name. The cost is that a long train can be hard to follow, so programmers in these languages switch between the two styles as suits the line.

A function can name its argument (explicit), or leave it out and just combine functions (tacit). Like J, X_eTaL supports both styles. Here is the spread of a list, its largest minus its smallest, both ways in each:

3 : '(>./y) - <./y'      J, explicit
(>./ - <./)              J, tacit
ᵘs̲pread ← { ('m̲ax r̲/ ⍵) − 'm̲in r̲/ ⍵ }             ⍝ explicit
ᵘs̲pread ← ['m̲ax r̲/ − 'm̲in r̲/]                    ⍝ tacit
ᵘs̲pread 3 1 4 1 5 9                                ⍝ 8

All four give 8 for 3 1 4 1 5 9. X_eTaL infers the same type for both forms, Num a => a -> a: any kind of number in, the same kind out.

Types, checked before it runs

The four older languages check types as the program runs. X_eTaL infers a type for every function first. Name the mean, and X_eTaL works out that it takes integers and gives a float:

ᵘm̲ean ← ['+ r̲/ ÷ t̲ally]
u:m_ean : Int -> Float

A program that gives it floats is rejected before it runs:

['+ r̲/ ÷ t̲ally] f̲loat x
error[type-mismatch]: expected Int, found Float

The family at a glance

  Notation Reading order Trains Types
APL glyphs right to left yes dynamic
J ASCII right to left yes dynamic
K ASCII right to left no dynamic
BQN glyphs right to left yes dynamic
X_eTaL ASCII, drawn right to left yes static

Glyphs or ASCII; right to left in every one; trains in four of them, counting X_eTaL’s brackets; types checked as the program runs, or before. X_eTaL is the one with static types.

Counting from one, or zero

Ask each language for the first five numbers. APL starts at one. J, K and BQN start at zero.

⍳5                  APL       1 2 3 4 5
i.5                 J         0 1 2 3 4
!5                  K         0 1 2 3 4
↕5                  BQN       ⟨ 0 1 2 3 4 ⟩

X_eTaL starts at one, as APL does:

r̲ange 5                                            ⍝ 1 2 3 4 5

(APL’s start can be changed, by setting ⎕IO to 0; one is the default. COBOL, Fortran, Julia, Lua, MATLAB/Octave, R, SAS, Wolfram Language and X_eTaL also use 1 as the index origin.)

One symbol, two meanings

The video's panel of shape and reshape: APL rho, J dollar, K hash, BQN's two glyphs, and X_eTaL's shape and reshape as words
Shape and reshape, from the video. Click to enlarge.

In APL, J and K, the same mark often does two jobs. With one argument, APL’s ⍴ gives a shape; with two, it changes the shape. J’s $ does the same. K’s # counts with one argument, and takes with two. BQN gives shape and reshape their own glyphs, ≢ and ⥊.

⍴M      2 3⍴⍳6      APL    shape; reshape
$M      2 3$i.6     J      shape; reshape
#v      2#v         K      count; take
≢m      2‿3⥊↕6      BQN    shape; reshape

X_eTaL gives them two names:

M ← 2 3 r̲eshape r̲ange 6                            ⍝ 1 2 3, 4 5 6
s̲hape M                                            ⍝ 2 3

How many built-ins

  Functions Modifiers Written as
APL (Dyalog 20) 56 glyphs 22 operators glyphs
J (the J Dictionary) 118 spellings, verbs and modifiers together   ASCII, one or two characters
K (ngn/k) 22 verbs 6 adverbs ASCII
BQN 44 functions 20 modifiers 64 glyphs
X_eTaL 75 functions 11 higher-order functions 13 symbols, 73 words

K has the fewest, but most of its verbs mean several things. X_eTaL has no operators as such: functions are values, so a function that takes a function does the job.

What each does best

  • APL: notation as a tool of thought, in the words of its inventor, Kenneth Iverson (his 1979 Turing Award lecture).
  • J: made tacit programming central, all in ASCII.
  • K: small and fast; kdb+ and q are built on it, and it is popular in finance.
  • BQN: fewer primitives, and less overloading, than APL or J.
  • X_eTaL: metaprogramming, with macros written in X_eTaL itself that turn source into source, and static types, inferred for every function before it runs. Also: plain ASCII source drawn as typography, and extensions in Rust (Blender, the assembly cell) called like any library.

Where X_eTaL fits

  • Typed in ASCII, like J and K.
  • Read drawn, like APL and BQN.
  • Built-ins mostly words, with one meaning each, not overloaded symbols.
  • Functions passed as values, so no separate operators.
  • Types checked before it runs, which none of the others do.

ASCII in, decorated out

The video's panel of the mean as typed in ASCII, u:m_ean := ['+ r_/ / t_ally], and as drawn by X_eTaL, with underlines and a division sign
The mean, typed and drawn, from the video. Click to enlarge.

The episode ends where X_eTaL differs most visibly. Here is the mean as you type it, in plain ASCII:

u:m_ean := ['+ r_/ / t_ally]

And as X_eTaL draws it:

ᵘm̲ean ← ['+ r̲/ ÷ t̲ally]

The underlines mark functions. The slash for divide is drawn as a division sign, and := as an arrow. The small mark before the name says it belongs to this program. It is one program, not two: the text stays simple for editors, diffs and search, and the drawing carries more to the reader. Reading and writing X_eTaL itself, names and decorations and what each one promises, is where the series goes next.

Before the next episode, try this: find the mean of 3 1 4 1 5 9 in two of these languages. TryAPL, the BQN REPL and X_eTaL in your browser all run without installing anything.

Closing

There are several other array languages, and I plan to eventually compare X_eTaL to them too. But first, I will go into more detail about X_eTaL syntax and capabilities in upcoming videos and blog posts.