[Haskell] Files and lazyness

Wolfgang Jeltsch wolfgang at jeltsch.net
Mon Aug 1 12:28:06 EDT 2005


Am Donnerstag, 28. Juli 2005 20:01 schrieb Diego y tal:
> I was developing a web site using haskell programs as cgi's, and I found
> a strange behavior that I would like to know whether it is normal. I
> have reduced the "problem" to the next program:
>
> fEntrada = "fich.txt"
> fSalida = "fich.txt"
>
> creaFich :: IO()
> creaFich = writeFile fEntrada "me molo"
>
> main :: IO ()
> main = do x <- readFile fEntrada
> --          print x -- In the second try, uncomment this line
>           writeFile fSalida ""
>           writeFile fSalida x
>
> Running the next commands (suposing that $ is the prompt of a linux
> shell and main> is the prompt of hugs)
>
> main> creaFich
> main> main
> $ cat fich.txt
>
> will give us different results if we comment or uncomment the second
> line of the main body, although the meaning of the program is the same.

In my opinion, the program should signal an error if the print x line is 
commented out since then the read handle isn't closed but semi-closed at the 
point of the first writeFile, and therefore write access shouldn't be allowed 
(see library documentation of System.IO).  In fact, GHC and GHCi behave this 
way.

> [...]

Regards,
Wolfgang


More information about the Haskell mailing list