From 248e364f396b38e067d386cbf348c7dcd208f928 Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Fri, 19 Dec 2014 20:23:45 +0100 Subject: [PATCH] Generalize show for matrix --- Matrix.hs | 4 ++++ life.hs | 8 -------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Matrix.hs b/Matrix.hs index c004746..17440d4 100644 --- a/Matrix.hs +++ b/Matrix.hs @@ -1,4 +1,5 @@ module Matrix where +import Data.List (intercalate) data Mat a = Mat [[a]] type Pos = (Int, Int) @@ -6,6 +7,9 @@ type Pos = (Int, Int) instance Functor Mat where fmap f (Mat m) = Mat ((map . map) f m) +instance (Show a) => Show (Mat a) where + show (Mat m) = concatMap ((++"\n") . intercalate " " . map show) m + -- | Safely access a list -- [4,1,5,9] ?? 2 == Just 5 -- [5,7] ?? 3 == Nothing diff --git a/life.hs b/life.hs index 702f067..835d622 100644 --- a/life.hs +++ b/life.hs @@ -1,16 +1,8 @@ -{-#LANGUAGE TypeSynonymInstances, FlexibleInstances#-} -import Data.Maybe (listToMaybe) -import Data.List (intercalate) import Matrix type Cell = Int type Grid = Mat Cell -instance Show Grid where - show (Mat m) = concatMap ((++"\n") . intercalate " " . map replace) m - where - replace 1 = "■" - replace 0 = "'" -- | Get the state of a cell -- 0 when out of the grid