This commit is contained in:
Rnhmjoj 2015-03-08 01:49:19 +01:00
parent 57120a5dae
commit c4c90e021e

14
life.hs
View File

@ -1,5 +1,6 @@
import Control.Applicative
import Data.List
import Data.List ((\\))
import Data.Maybe (fromMaybe)
import Matrix
type Cell = Int
@ -16,9 +17,7 @@ gprint = putStrLn . map replace . show
-- | 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
(Mat g) ! (x, y) = fromMaybe 0 (g ?? y >>= (?? x))
-- | List of neighbours cells
near :: Grid -> Pos -> [Cell]
@ -42,12 +41,11 @@ next g = alive g <$> indeces g
main :: IO ()
main = mapM_ gprint (iterate next grid)
grid = Mat [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
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]
]
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]