45 lines
1.1 KiB
Haskell
45 lines
1.1 KiB
Haskell
import Matrix
|
|
|
|
type Cell = Int
|
|
type Grid = Mat Cell
|
|
|
|
|
|
-- | Get the state of a cell
|
|
-- 0 when out of the grid
|
|
(!) :: Grid -> Pos -> Cell
|
|
(Mat g) ! (x, y) = case g ?? y >>= (?? x) of
|
|
Nothing -> 0
|
|
Just v -> v
|
|
|
|
-- | List of neighbours cells
|
|
near :: Grid -> Pos -> [Cell]
|
|
near g (x, y) = [g ! (x+x', y+y') | x' <- [-1..1],
|
|
y' <- [-1..1],
|
|
(x',y') /= (0,0)]
|
|
|
|
-- | Find if a cell will be alive in the next generation
|
|
alive :: Grid -> Pos -> Cell
|
|
alive g p
|
|
| v == 0 && n == 3 = 1
|
|
| v == 1 && (n == 2 || n == 3) = 1
|
|
| otherwise = 0
|
|
where
|
|
(n, v) = (sum (near g p), g ! p)
|
|
|
|
-- | Compute next generation
|
|
next :: Grid -> Grid
|
|
next g = fmap (alive g) (indeces g)
|
|
|
|
main :: IO ()
|
|
main = mapM_ print (iterate next grid)
|
|
|
|
grid = Mat [
|
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
, [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
, [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
]
|