[Haskell-cafe] IO Put confusion
Chad Scherrer
chad.scherrer at gmail.com
Tue Sep 14 19:45:10 EDT 2010
Hello,
I need to be able to use strict bytestrings to efficiently build a
lazy bytestring, so I'm using putByteString in Data.Binary. But I also
need random numbers, so I'm using mwc-random. I end up in the "IO Put"
monad, and it's giving me some issues.
To build a random document, I need a random length, and a collection
of random words. So I have
docLength :: IO Int
word :: IO Put
Oh, also
putSpace :: Put
My first attempt:
doc :: IO Put
doc = docLength >>= go
where
go 1 = word
go n = word >> return putSpace >> go (n-1)
Unfortunately, with this approach, you end up with a one-word
document. I think this makes sense because of the monad laws, but I
haven't checked it.
Second attempt:
doc :: IO Put
doc = docLength >>= go
where
go 1 = word
go n = do
w <- word
ws <- go (n-1)
return (w >> putSpace >> ws)
This one actually works, but it holds onto everything in memory
instead of outputting as it goes. If docLength tends to be large, this
leads to big problems.
Oh, yes, and my main is currently
main = L.writeFile "out.txt" =<< fmap runPut doc
This needs to be lazier so disk writing can start sooner, and to avoid
eating up tons of memory. Any ideas?
Thanks!
Chad
More information about the Haskell-Cafe
mailing list