Generalize show for matrix
This commit is contained in:
parent
299e70e1df
commit
248e364f39
@ -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
|
||||
|
8
life.hs
8
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
|
||||
|
Loading…
Reference in New Issue
Block a user