I have this simple code:
module Matrix where
matrix :: a -> (Int, Int) -> [[a]]
matrix x (width, height) = replicate height (replicate width x)
mapMatrix :: (a -> b) -> [[a]] -> [[b]]
mapMatrix f m = map (map f) m
When I do:
mapMatrix (+1) (matrix 0 (2,2))
I get, as expected:
[[1,1],[1,1]]
Probably I'm misunderstanding monads and/or the >>=
operator but I was expecting the following to have the same output:
matrix 0 (2,2) >>= mapMatrix (+1)
Instead I get:
Non type-variable argument in the constraint: Num [b] (Use FlexibleContexts to permit this) When checking the inferred type It :: forall b. (Num [b], Num b) => [[b]]
How can I write mapMatrix (+1) (matrix 0 (2,2))
with monads, so I can read and write the code from left-to-right instead of in-to-out because as you can imagine, I'm planning on using mapMatrix
a lot on the same matrix, something like:
matrix ... >>= mapMatrix ... >>= mapMatrix .. >>= ...
f x
is never the same asx >>= f
, just asf 1
is not the same as1 + f
:>>=
is an operator that does a specific thing. – amalloybind
operation that you get when using monads, at least as I learnt them in other languages. By the way I'm doing this just for learning purposes, but I could really use some guidance right now – Marco Scabbiolobind
asbind(x, f) <=> f(x)
or evenx.bind(f) <=> f(x)
, I guess I have to learn the specifics of Haskell. – Marco Scabbiolo(&)
(inData.Function
)? That is defined asx & f = f x
. – Alec