[Haskell-cafe] Re: Library design question

Daniel Fischer daniel.is.fischer at web.de
Sat Sep 20 08:56:52 EDT 2008


Am Samstag, 20. September 2008 08:53 schrieb David Menendez:
> On Fri, Sep 19, 2008 at 7:02 PM, Andre Nathan <andre at digirati.com.br> wrote:
> > On Fri, 2008-09-19 at 23:16 +0200, Daniel Fischer wrote:
> >> Yes. What's IO gotta do with it?
> >
> > I did it because of randomIO :(
> >
> >> (or what about StateT (Graph a b) (State StdGen) ?).
> >
> > Now there's something I wouldn't have thought of... I changed the
> > RandomGraph type to
> >
> >  type RandomGraph a b = StateT (Graph a b) (State StdGen) ()
> >
> > and randomFloat to
> >
> >  randomDouble :: State StdGen Double
> >  randomDouble = State random
> >
> > and randomGraph to
> >
> >  randomGraph :: StdGen -> Int -> Double -> Graph Int Int
> >  randomGraph gen n p = evalState (execStateT create Graph.empty) gen
> >    where create = mapM_ (uncurry $ createVertex p) vls
> >          vls    = zip [1..n] (repeat 42)
> >
> > However, when I try to create a graph with 1000 vertices I get a stack
> > overflow, which didn't happen in the IO version. Any idea why that
> > happens?
>
> I believe modify is lazy. Try replacing it with a stricter version,
>
> modify' f = do
>     s <- get
>     put $! f s

Or try Control.Monad.State.Strict.


More information about the Haskell-Cafe mailing list