[Haskell-cafe] Make strict (IO String) lazy

Donald Bruce Stewart dons at cse.unsw.edu.au
Thu Mar 15 10:10:17 EDT 2007


lemming:
> 
>  On the one hand, in the standard libraries there are functions like
> readFile, getContents, hGetContents which read a file lazily. This is
> often a nice feature, but sometimes lead to unexpected results, say when
> reading a file and overwriting it with modified contents. Unfortunately
> the standard libraries provide no functions for strict reading, and one
> has to do this manually.

Data.ByteString.readFile is strict, and then:

    import qualified Data.ByteString

    strictReadFile :: FilePath -> IO String
    strictReadFile = liftM B.unpack B.readFile

>  On the other hand, when I write some IO function that returns a String, I
> easily end up with a function which produces the String in a strict way.
> (Say I call some shell commands and concatenate their outputs.)
>  What is the preferred way to turn a strict (IO String) into a lazy one?
> forkIO? forkOS? How would one derive readFile from a hypothetical
> strictReadFile?

unsafeInterleaveIO strict reads?

-- Don


More information about the Haskell-Cafe mailing list