programdemos/life.xtl
Life: Conway's Game of Life in one line, u:l_ife, then a blinker and
a glider run through it. Run it with "xetal run demos/life.xtl" (or
"./demos/life.xtl"); it prints a blinker after one and two steps, 1s
showing that two steps restore it, then a glider's five generations
a blank line apart. Read u:l_ife for reduce and rotate along both
axes at once (the _12 subscripts).
The rules
ᵘl̲ife : (Num a, Truthy a) => a -> a
u:l_ife board: the next generation of a 0/1 board. The board ⍵
rotated by every offset in -1 0 1 along both axes and summed gives
S, each cell plus its neighbors; a cell lives next when S is 3, or
when it is alive and S is 4.
ᵘl̲ife ← { ('+ r̲/₁₂ -1 0 1 o̲-₁₂ ⍵) { (⍺ = 3) + ⍵ × ⍺ = 4 } ⍵ }
ᵘs̲teps! : (Any a, Num b) => (a -> a) -> b -> a -> a
n 'f_ u:s_teps! x: applies f to x n times and gives the last result, printing x and then a blank line at every generation on the way, the first and the last included.
ᵘs̲teps! ← { f̲ n x → _ ← p̲rint! x _ ← p̲rint! "" n = 0 ? x (n − 1) 'f̲ ᵘs̲teps! f̲ x }
A blinker
blinker : Int
blinker: a vertical bar of three live cells in a 5 by 5 board. It has
period 2: one step turns it, two steps restore it. A superscript on a
function repeats it (function power): ᵘl̲ife².
blinker ← 5 5 r̲eshape 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0