Generalize show for matrix

This commit is contained in:
Rnhmjoj 2014-12-19 20:23:45 +01:00
parent 299e70e1df
commit 248e364f39
2 changed files with 4 additions and 8 deletions

View File

@ -1,4 +1,5 @@
module Matrix where module Matrix where
import Data.List (intercalate)
data Mat a = Mat [[a]] data Mat a = Mat [[a]]
type Pos = (Int, Int) type Pos = (Int, Int)
@ -6,6 +7,9 @@ type Pos = (Int, Int)
instance Functor Mat where instance Functor Mat where
fmap f (Mat m) = Mat ((map . map) f m) 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 -- | Safely access a list
-- [4,1,5,9] ?? 2 == Just 5 -- [4,1,5,9] ?? 2 == Just 5
-- [5,7] ?? 3 == Nothing -- [5,7] ?? 3 == Nothing

View File

@ -1,16 +1,8 @@
{-#LANGUAGE TypeSynonymInstances, FlexibleInstances#-}
import Data.Maybe (listToMaybe)
import Data.List (intercalate)
import Matrix import Matrix
type Cell = Int type Cell = Int
type Grid = Mat Cell 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 -- | Get the state of a cell
-- 0 when out of the grid -- 0 when out of the grid