← The Lab
// The Lab

Elementary cellular automaton

A purely functional Python implementation. No loops, no mutable variables, just functions.

functional Python

An elementary cellular automaton is a row of cells, each on or off. Every step, each cell looks at itself and its two neighbours, and a rule decides whether it’s on in the next row. Three cells give eight possible neighbourhoods, so a rule is just 8 bits: there are 256 of them, numbered 0 to 255. Some make stripes, some make triangles, and rule 30 makes chaos from a single cell.

I wrote it the functional way: the program has no loops, and every variable is immutable. Everything a loop would do is done with recursion instead, down to counting the length of a list, and each row is built from the last one. It’s plain Python with no dependencies, and it was a fun challenge that changed how I think about programming.

Generate your own

Type a rule from 0 to 255, or click the eight patterns below to flip what each one makes.