[Haskell-cafe] Re: Re: Can we come out of a monad?
Jason Catena
jason.catena at gmail.com
Fri Jul 30 12:57:00 EDT 2010
On Jul 30, 11:17 am, Anton van Straaten wrote:
> Prelude> :m Control.Monad.State
> Prelude Control.Monad.State> let addToState :: Int -> State Int ();
> addToState x = do s <- get; put (s+x)
> Prelude Control.Monad.State> let mAdd4 = addToState 4
> Prelude Control.Monad.State> :t mAdd4
> m :: State Int ()
> Prelude Control.Monad.State> let s = execState mAdd4 2
> Prelude Control.Monad.State> :t s
> s :: Int
> Prelude Control.Monad.State> s
> 6
By this example State doesn't seem to give you anything more than a
closure would, since it doesn't act like much of an accumulator (by,
for example, storing 6 as its new internal value).
Could you use State for something like storing the latest two values
of a Fibonacci series? For example, each time you call it, it
generates the next term, discards the oldest term, and stores the
newly-generated term?
And could you then use this Fibonacci State monad in a lazy
computation, to grab for example the first twenty even Fibonacci
numbers, without computing and storing the series beyond what the
filter asks for?
We can generate Fibonacci series double-recursively in a lazy
computation. Would it be more or less efficient to use a Fibonacci
State monad instead? Would the State implementation provide a larger
range before it blew the stack (which tail-recursion should prevent),
or became too slow for impatient people?
Would Haskell memoize already-generated values in either case? Could
we write a general memoizer across both the recursive and State
implementations, or must we write a specific one to each case?
Jason Catena
More information about the Haskell-Cafe
mailing list