TIL: The most powerful one-line program in the world

Well, the most powerful I’ve found so far, anyway.

Given my current obsession with Lisp you might reasonably expect it to be in that language. But it isn’t: it’s in APL, and it performs one complete generation of Conway’ Game of Life in one line of code:

  Life←{↑↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵}

…and does so inexplicably to anyone who doesn’t know APL, obviously, but the basic algorithm is simple:

  1. Take an array with 1 in each occupied cell and 0 elsewhere
  2. Build four new arrays by exchanging each element with its neighbour up, down, left, and right
  3. Sum these arrays, which places the number of neighbours into each cell
  4. Cut-off these values to be 1 if the cell has a value of 3 or 4, and 0 otherwise
  5. Re-format the arrays back into the starting configuration

I checked it out using GNU APL and it works fine.

I discovered this gem by accident, actually implemented in APL in Forth where someone has developed APL as an embedded DSL within Forth (another language with which I have history). After a bit of digging I found a similar APL in Lisp, April, which clearly needs exploring.

In many ways APL and Lisp are parallel tracks within programming language evolution, taking a single data structure (lists or arrays) and providing powerful ways to manipulate them. Lisp of course has been extended with other data structures, including arrays, which makes the fusion of array- and list-based programming rather attractive.

I can’t help asking myself what would have happened if APL hadn’t fallen by the wayside. (I think this was inevitable, incidentally, once the syntax became fixed: any language that requires its own character set was always going to struggle.) We now have huge applications for array processing, from graphics to machine learning, and GPUs are from one perspective just APL accelerator co-processors. The ideas are still massively relevant.