diff --git a/life.hs b/life.hs index ed40e45..cedbb0e 100644 --- a/life.hs +++ b/life.hs @@ -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] ]