You can simply use a state monad and then extract the data later
when you don't want the extra state, viz.
sum :: (Num a) => [a] -> a
sum xs = flip execState 0 $
mapM_ (\x -> do { s <- get ; put $ x + s }) xs
I find myself using the state and exception monads at large scale in my programs. For instance I often keep a pseudorandom generator as the state, and use exception handling.
In local computations, I may want to use some additional state. Is there a way to add a little extra state temporarily?
Let's say I have
data State1 = State1 StdGen
type M1 = State State1
data State2 = State2 State1 Int
type M2 = State State2
runM2 :: M2 a -> Int -> M1 a
someFunc :: M1 Double
someFunc = dor <- <compute something pseudorandomly>r2 <- runM2 (deeperFunc r) 3<..etc..>
deeperFunc :: Double -> M2 Double
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.