Created
November 28, 2012 20:03
-
-
Save muratg/4163803 to your computer and use it in GitHub Desktop.
Wolfram's 1D cellular automation in F# console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
A simple F# script to display Wolfram's basic 1D cellular automaton rules as text output [1] | |
-mg | |
*) | |
let runWolframRule rule cells iterations = | |
let compute cells ith rule = | |
let withBorders = 0 :: cells @ [0]; | |
(withBorders.[ith], withBorders.[ith+1], withBorders.[ith+2]) | |
|> fun (a,b,c) -> 0 ||| (a <<< 2) ||| (b <<< 1) ||| (c <<< 0) | |
|> fun bit -> if (rule &&& (1 <<< bit) <> 0 ) then 1 else 0 | |
let toStr cells = | |
cells |> List.map (function | 0 -> '.' | 1 | _ -> '#') | |
|> List.fold (sprintf "%s%c") "" | |
let computeAndPrint cells = | |
printfn "%s" <| toStr cells | |
cells |> List.mapi (fun i _ -> compute cells i rule) | |
[0..iterations] | |
|> List.fold (fun st _ -> computeAndPrint st) cells | |
let sampleCells x = (List.replicate x 0) @ [1] @ (List.replicate x 0) | |
(* | |
Here's a sample output: | |
runWolframRule 30 sampleCells 40 | |
........................................#........................................ | |
.......................................###....................................... | |
......................................##..#...................................... | |
.....................................##.####..................................... | |
....................................##..#...#.................................... | |
...................................##.####.###................................... | |
..................................##..#....#..#.................................. | |
.................................##.####..######................................. | |
................................##..#...###.....#................................ | |
...............................##.####.##..#...###............................... | |
..............................##..#....#.####.##..#.............................. | |
.............................##.####..##.#....#.####............................. | |
............................##..#...###..##..##.#...#............................ | |
...........................##.####.##..###.###..##.###........................... | |
..........................##..#....#.###...#..###..#..#.......................... | |
.........................##.####..##.#..#.#####..#######......................... | |
........................##..#...###..####.#....###......#........................ | |
.......................##.####.##..###....##..##..#....###....................... | |
......................##..#....#.###..#..##.###.####..##..#...................... | |
.....................##.####..##.#..######..#...#...###.####..................... | |
....................##..#...###..####.....####.###.##...#...#.................... | |
...................##.####.##..###...#...##....#...#.#.###.###................... | |
..................##..#....#.###..#.###.##.#..###.##.#.#...#..#.................. | |
.................##.####..##.#..###.#...#..####...#..#.##.######................. | |
................##..#...###..####...##.#####...#.#####.#..#.....#................ | |
...............##.####.##..###...#.##..#....#.##.#.....#####...###............... | |
..............##..#....#.###..#.##.#.####..##.#..##...##....#.##..#.............. | |
.............##.####..##.#..###.#..#.#...###..####.#.##.#..##.#.####............. | |
............##..#...###..####...####.##.##..###....#.#..####..#.#...#............ | |
...........##.####.##..###...#.##....#..#.###..#..##.####...###.##.###........... | |
..........##..#....#.###..#.##.#.#..#####.#..######..#...#.##...#..#..#.......... | |
.........##.####..##.#..###.#..#.####.....####.....####.##.#.#.#########......... | |
........##..#...###..####...####.#...#...##...#...##....#..#.#.#........#........ | |
.......##.####.##..###...#.##....##.###.##.#.###.##.#..#####.#.##......###....... | |
......##..#....#.###..#.##.#.#..##..#...#..#.#...#..####.....#.#.#....##..#...... | |
.....##.####..##.#..###.#..#.####.####.#####.##.#####...#...##.#.##..##.####..... | |
....##..#...###..####...####.#....#....#.....#..#....#.###.##..#.#.###..#...#.... | |
...##.####.##..###...#.##....##..###..###...######..##.#...#.###.#.#..####.###... | |
..##..#....#.###..#.##.#.#..##.###..###..#.##.....###..##.##.#...#.####....#..#.. | |
.##.####..##.#..###.#..#.####..#..###..###.#.#...##..###..#..##.##.#...#..######. | |
##..#...###..####...####.#...######..###...#.##.##.###..######..#..##.#####.....# | |
[1] http://en.wikipedia.org/wiki/Wolfram_code | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment