How do you define a function of signature h :: M Int -> M Int -> M Int so that h (M x) (M y) = M (x+y) without unwrapping the monad?
haskell, monads
Solution
Now I am a little confused, how can you put x in g if it takes an Int as first parameter but x is M Int?
There are two different `x` variables and the inner one is shadowing the outer one inside the lambda expression. A clearer way to write the code would be something like
h mx my = mx >>= (\x -> g x my)
Problem
This question is from the article "Trivial Monad" found at http://blog.sigfpe.com/2007/04/trivial-monad.html. The provided answer is ``` h x y = x >>= (\x -> g x y) ``` or equivalently ( in context of the article ) ``` h :: W Int -> W Int -> W Int h x y = bind ( \x-> g x y ) x ``` where g is ``` g :: Int -> W Int -> W Int g x y = y >>= (return . (+x)) ``` for the monad: `data W a = W a deriving Show` Now I am a little confused, how can you put x in g if it takes an `Int` as first parameter but x is `W Int`?