
Is the idea here merely an exercise in using the state monad? This can be easily performed using pure code. Bob On 3 Feb 2011, at 19:18, 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?
Michael
==================
import Control.Monad.State
type GeneratorState = State (Double,Double)
sqrtST :: GeneratorState Double sqrtST = do (a,b0) <- get let b1 = (b0**2.0+a)/(2.0*b0) (if (abs (a-b1**2.0)) < 0.000001 then return b1 else do put (a,b1) sqrtST)
mySqrt a = let b = a/2.0 in fst ( runState sqrtST (a,b) )
{- *Main> mySqrt 2.0 1.4142135623746899 -}
==================
import Control.Monad.Reader import Control.Monad.State
type GeneratorState = State Double
sqrtST :: GeneratorState Double sqrtST = do b0 <- get let a = ? b1 = (b0**2.0+a)/(2.0*b0) (if (abs (a-b1**2.0)) < 0.000001 then return b1 else do put b1 sqrtST)
mySqrt a = let b = a/2.0 in runReaderT (runState sqrtST b) a
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe