[Haskell-beginners] how to read file with locking

Isaac Dupree ml at isaac.cedarswampstudios.org
Sat Oct 9 21:52:35 EDT 2010


On 10/09/10 18:49, Joey Hess wrote:
> The function below always returns "", rather than the file's contents.
> _Real World Haskell_ touches on how laziness can make this problimatic,
> without really giving a solution, other than throwing in a putStr to
> force evaluation, which can't be done here. How can I make  hGetContents
> strict, to ensure the file contents are really read before it gets closed?

There are various hacks to do it (including ones with no external 
side-effects that just use "case" / "seq" / etc. quite thoroughly)... 
But realistically you should use a library that doesn't add laziness, if 
laziness is not what you want. For example, "strict" on Hackage
http://hackage.haskell.org/package/strict
http://hackage.haskell.org/packages/archive/strict/0.3.2/doc/html/System-IO-Strict.html

-Isaac


More information about the Beginners mailing list