I have a datatype which keeps track of a 'block' of numbers (kinda like a matrix).
newtype Block a = Block [[a]]
I would like to make it an instance of Functor
. However, I am trying to do it in such a way that fmap can apply to the whole list block, so that fmap has type fmap :: [[a]] -> [[b]]
instead of type fmap :: a -> b
.
The reason is because I would like to map into my Block
functor functions like transpose
which apply to the list block [[a]]
not to each element a
. That is, I would like to be able to define functions like
transposeBlock :: Block a -> Block a
transposeBlock = (transpose <$>)
I tried to declare my functor instance as the following.
instance Functor (Block [[a]]) where
fmap f (Block x) = Block (f x)
But I ran into the type errors that arise from attempting to compile this.
error:
• Expecting one fewer argument to ‘Block [[a]]’
Expected kind ‘* -> *’, but ‘Block [[a]]’ has kind ‘*’
• In the first argument of ‘Functor’, namely ‘Block [[a]]’
In the instance declaration for ‘Functor (Block [[a]])’
What are some ways that I could map functions into the list block [[a]]
of my Block
type?
fmap . fmap :: (a -> b) -> [[a]] -> [[b]]
? – AJF