data T b = E | N b (T b) (T b)
f :: T b -> Reader Int (T Int)
f (N i l r) = ask >>= \x -> local ((-)4) (f l) >>= \l' -> local ((-)1) (f r) >>= \r' -> return (N x l' r')
f E = return E
I have a problem with understanding how this code works. Especially, how does ask
know where the environment is (in our case just Int
)?
To be more precise: I am an imperative programmer and in such languages it is easy. Methods can be called on any object like: obj.f()
, or we have to pass data by argument when we want function use external data.