[Haskell-cafe] Reader monad wrapping State monad

Daniel Fischer daniel.is.fischer at googlemail.com
Thu Feb 3 20:54:23 CET 2011

On Thursday 03 February 2011 20:18:43, michael rice wrote:
> 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)


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

More information about the Haskell-Cafe mailing list