sourcedemos/life.xtl

1⍝!/usr/bin/env xetal 2⍝# Life: Conway's Game of Life in one line, u:l_ife, then a blinker and 3⍝# a glider run through it. Run it with "xetal run demos/life.xtl" (or 4⍝# "./demos/life.xtl"); it prints a blinker after one and two steps, 1s 5⍝# showing that two steps restore it, then a glider's five generations 6⍝# a blank line apart. Read u:l_ife for reduce and rotate along both 7⍝# axes at once (the _12 subscripts). 8 9⍝## The rules 10 11⍝# u:l_ife board: the next generation of a 0/1 board. The board ⍵ 12⍝# rotated by every offset in -1 0 1 along both axes and summed gives 13⍝# S, each cell plus its neighbors; a cell lives next when S is 3, or 14⍝# when it is alive and S is 4. 15ᵘl̲ife ← { ('+ r̲/₁₂ -1 0 1 o̲-₁₂ ⍵) { (⍺ = 3) + ⍵ × ⍺ = 4 } ⍵ } 16 17⍝# n 'f_ u:s_teps x: applies f to x n times and gives the last result, 18⍝# printing x and then a blank line at every generation on the way, the 19⍝# first and the last included. 20ᵘs̲teps ← { f̲ n x → 21 shown ← p̲rint! x 22 gap ← p̲rint! "" 23 n = 0 ? x 24 (n − 1) 'f̲ ᵘs̲teps f̲ x 25} 26 27⍝## A blinker 28 29⍝# blinker: a vertical bar of three live cells in a 5 by 5 board. It has 30⍝# period 2: one step turns it, two steps restore it. A superscript on a 31⍝# function repeats it (function power): ᵘl̲ife². 32blinker ← 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 33ᵘl̲ife blinker 34ᵘl̲ife² blinker 35(ᵘl̲ife² blinker) = blinker 36 37⍝## A glider 38 39⍝# glider: a glider in a 6 by 6 board; it moves one cell diagonally 40⍝# every 4 generations. 41glider ← 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 42 43⍝# last: the glider after 4 generations, each printed on the way. 44last ← 4 'ᵘl̲ife ᵘs̲teps glider