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 <[email protected]> wrote:

From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-cafe] Reader monad wrapping State monad
To: [email protected]
Cc: "michael rice" <[email protected]>
Date: Thursday, February 3, 2011, 2:54 PM

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)

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




      
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to