diff options
Diffstat (limited to 'js/baba-yaga/life-example.baba')
-rw-r--r-- | js/baba-yaga/life-example.baba | 241 |
1 files changed, 0 insertions, 241 deletions
diff --git a/js/baba-yaga/life-example.baba b/js/baba-yaga/life-example.baba deleted file mode 100644 index cb2938e..0000000 --- a/js/baba-yaga/life-example.baba +++ /dev/null @@ -1,241 +0,0 @@ -// Conway's Game of Life in Baba Yaga -// This file demonstrates Baba Yaga syntax and features through cellular automata -// -// BABA YAGA FEATURES DEMONSTRATED: -// ================================= -// 1. Function definitions and currying -// 2. with blocks for computed values and local bindings -// 3. when expressions for pattern matching and control flow -// 4. List literals and operations (map, reduce, filter, append, prepend) -// 5. Higher-order functions and anonymous functions -// 6. Recursion and functional programming patterns -// 7. Immutable data structures -// 8. Complex nested expressions and function composition -// 9. Grid-based data manipulation -// 10. Mathematical operations and comparisons -// -// GAME OF LIFE PATTERNS DEMONSTRATED: -// =================================== -// - Oscillators: Blinker, Toad, Beacon, Pulsar -// - Still Lifes: Block, Beehive -// - Spaceships: Glider -// - Complex Patterns: Gosper Glider Gun (simplified) - -// headAt: return the element at index i from list xs -// Demonstrates: with blocks, slice function, list indexing -headAt : xs i -> with (tmp : slice xs i (i + 1)) -> tmp.0; - -// get2: 2D index for a grid (list of lists) -get2 : grid r c -> headAt (headAt grid r) c; - -// safeGet2: bounds-checked 2D get; returns 0 when out of bounds -// Demonstrates: with blocks, logical operators, when expressions, pattern matching -safeGet2 : grid r c -> - with ( - rows : length grid; - cols : length (headAt grid 0); - rOk : (r >= 0) and (r < rows); - cOk : (c >= 0) and (c < cols); - ) -> - when (rOk and cOk) is - true then get2 grid r c - _ then 0; - -// range [lo, hi) as a list of Int -// Demonstrates: recursion, when expressions, list construction with prepend -range : lo hi -> - when (lo >= hi) is - true then [] - _ then prepend lo (range (lo + 1) hi); - -// sum a list of numbers -// Demonstrates: higher-order functions, reduce, anonymous functions -sum : xs -> reduce (acc x -> acc + x) 0 xs; - -// deltas for neighborhood offsets -// Demonstrates: list literals, constant definitions -deltas : [-1, 0, 1]; - -// neighborsValues: list of neighbor cell values for (r,c) -// Demonstrates: nested reduce, complex when expressions, function composition -neighborsValues : grid r c -> - reduce (acc dr -> - reduce (acc2 dc -> - when ((dr = 0) and (dc = 0)) is - true then acc2 - _ then append acc2 (safeGet2 grid (r + dr) (c + dc)) - ) acc deltas - ) [] deltas; - -// count live neighbors at (r,c) -countNeighbors : grid r c -> sum (neighborsValues grid r c); - -// nextCell: apply Game of Life rules at (r,c) -// Demonstrates: with blocks for computed values, nested when expressions, Game of Life rules -nextCell : grid r c -> - with ( - cell : get2 grid r c; - n : countNeighbors grid r c; - isAlive : cell = 1; - born : (cell = 0) and (n = 3); - survive : isAlive and ((n = 2) or (n = 3)); - ) -> - when survive is - true then 1 - _ then when born is true then 1 _ then 0; - -// build next row r given width w -// Demonstrates: map function, partial application, range usage -rowNext : grid w r -> map (c -> nextCell grid r c) (range 0 w); - -// Single simulation step for the entire grid -// Demonstrates: with blocks, map over range, grid dimensions -step : grid -> - with ( - h : length grid; - w : length (headAt grid 0); - ) -> map (r -> rowNext grid w r) (range 0 h); - -// ======================================== -// PATTERN DEFINITIONS -// ======================================== - -// Demo: glider pattern in a 5x5 grid -g0 : [ - [0, 1, 0, 0, 0], - [0, 0, 1, 0, 0], - [1, 1, 1, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0] -]; - -g1 : step g0; -g2 : step g1; -g3 : step g2; -g4 : step g3; - -// Blinker pattern (oscillator) - 3x3 grid -blinker0 : [ - [1, 1, 1], - [0, 0, 0], - [0, 0, 0] -]; - -blinker1 : step blinker0; -blinker2 : step blinker1; - -// Toad pattern (oscillator) - 4x4 grid -toad0 : [ - [0, 1, 1, 1], - [1, 1, 1, 0], - [0, 0, 0, 0], - [0, 0, 0, 0] -]; - -toad1 : step toad0; -toad2 : step toad1; - -// Beacon pattern (oscillator) - 4x4 grid -beacon0 : [ - [1, 1, 0, 0], - [1, 1, 0, 0], - [0, 0, 1, 1], - [0, 0, 1, 1] -]; - -beacon1 : step beacon0; -beacon2 : step beacon1; - -// Block pattern (still life) - 2x2 grid -block : [ - [1, 1], - [1, 1] -]; - -block1 : step block; - -// Beehive pattern (still life) - 4x3 grid -beehive : [ - [0, 1, 1, 0], - [1, 0, 0, 1], - [0, 1, 1, 0] -]; - -beehive1 : step beehive; - -// Pulsar pattern (oscillator with period 3) - 13x13 grid -pulsar0 : [ - [0,0,0,0,0,0,0,0,0,0,0,0,0], - [0,0,0,0,0,1,0,0,0,1,0,0,0], - [0,0,0,0,0,1,0,0,0,1,0,0,0], - [0,0,0,0,0,1,1,0,1,1,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0], - [0,1,1,1,0,0,1,1,1,0,0,1,1], - [0,0,0,1,0,1,0,1,0,1,0,1,0], - [0,0,0,0,0,1,1,1,0,0,1,1,1], - [0,0,0,1,0,1,0,1,0,1,0,1,0], - [0,1,1,1,0,0,1,1,1,0,0,1,1], - [0,0,0,0,0,0,0,0,0,0,0,0,0], - [0,0,0,0,0,1,1,0,1,1,0,0,0], - [0,0,0,0,0,1,0,0,0,1,0,0,0], - [0,0,0,0,0,1,0,0,0,1,0,0,0] -]; - -pulsar1 : step pulsar0; -pulsar2 : step pulsar1; -pulsar3 : step pulsar2; - - - -// ======================================== -// OUTPUT - Simple grid display like original life.baba -// ======================================== - -// Glider evolution -io.out "Glider pattern evolution:"; -io.out g0; -io.out g1; -io.out g2; -io.out g3; -io.out g4; - -// Blinker oscillation -io.out ""; -io.out "Blinker oscillation:"; -io.out blinker0; -io.out blinker1; -io.out blinker2; - -// Toad breathing -io.out ""; -io.out "Toad breathing pattern:"; -io.out toad0; -io.out toad1; -io.out toad2; - -// Beacon blinking -io.out ""; -io.out "Beacon blinking:"; -io.out beacon0; -io.out beacon1; -io.out beacon2; - -// Still lifes -io.out ""; -io.out "Still life patterns:"; -io.out block; -io.out beehive; - -// Pulsar oscillation -io.out ""; -io.out "Pulsar oscillation (period 3):"; -io.out pulsar0; -io.out pulsar1; -io.out pulsar2; -io.out pulsar3; - - - - - - |