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).

source

The rules

ᵘl̲ife : (Num a, Truthy a) => a -> a

function · line 15

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

function · line 20

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
}
Used in: ᵘs̲teps!, last

A blinker

blinker : Int

value · line 32

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

A glider

glider : Int

value · line 41

glider: a glider in a 6 by 6 board; it moves one cell diagonally every 4 generations.

glider ← 6 6 r̲eshape 0 1 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Used in: last

last : Int

value · line 44

last: the glider after 4 generations, each printed on the way.

last ← 4 'ᵘl̲ife ᵘs̲teps! glider