
Hi Daniel,
Ok, but what I was looking for was ReaderT on top, State on the bottom. This is very confusing material, with no apparent conceptual commonality (ad hoc comes to mind) among the many examples I've looked at. Sometimes lift is used, other times a lift helper function, and in this case no use of lift at all.
Michael
--- On Thu, 2/3/11, Daniel Fischer
Given the first program, it seems that the unchanging first element of the tuple could be handled by a Reader monad, leading to the second program, where b becomes the state, but how do I get the constant a from the Reader monad?
You need a monad-transformer to use both, Reader and State. You can use either ReaderT Double (State Double) or StateT Double (Reader Double) (they're isomorphic). Then you can query the modifiable state with get (from the MonadState class) and the immutable with ask (from the MonadReader class) type Heron = StateT Double (Reader Double) sqrtH :: Heron Double sqrtH = do a <- ask b <- get let c = 0.5*(b + a/b) if (good enough) then return c else put c >> sqrtH mySqrt a = runReader (evalStateT sqrtH (a*0.5)) a