Instead of creating a new board of the same size but with different content, it may be useful to think of this operation as replacing the contents of an existing board. Of course, since Haskell is an immutable language, it is one and the same - the only way to change something is to create a new version of it, but it should help you understand that it is basically a mapping operation.
replaceWithA :: [[a]] -> [[Char]]
replaceWithA xss = map (map (const 'a')) xss
-- or, point-free:
replaceWithA = map (map (const 'a'))
-- or, as a list comprehension:
replaceValues xss = [['a' | x <- xs] | xs <- xss]
If you want to go deeper, you can get the compiler to write this code for you. A type class Functorgeneralizes mapto structures that are not simple lists:
class Functor f where
fmap :: (a -> b) -> f a -> f b
In what sense is this generalization map? If you replace fwith [], you will see that it fmaphas the same signature as map:
fmap :: (a -> b) -> [a] -> [b]
In fact, this is how an instance is implemented [] Functor:
instance Functor [] where
fmap = map
, GHC , Functor . newtype 2D- deriving Functor!
{-
import Data.Functor
newtype TwoDimensional a = TwoDimensional { getTwoDimensional :: [[a]] } deriving Functor
fmap :
fmap :: (a -> b) -> TwoDimensional a -> TwoDimensional b
:
replaceWithA :: TwoDimensional a -> TwoDimensional Char
replaceWithA = ('a' <$)
, Haskell , Functor, , . , .