[Haskell] Files and lazyness
Diego y tal
deigote at tiscali.es
Thu Jul 28 14:01:17 EDT 2005
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)
$ 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.
I understand that this is caused by the lazyness, that doesn't evaluate
the expression "x <- readFile fEntrada" until it's necessary, but.. is
it normal that we have to think about this "problem" when programming?
In my case, this behavior caused my program to fail (and it was really
complicated to find why) and the only solution I found was indeed
printing to a scratch file the string I was reading inmediatly after
reading it (really, after telling hugs to read it). I find this a great
disadvantage as oppose of the imperative paradigm, overall because is
like having to control something (near to concurrence if you ask me)
that has not been asked for! I 'll be thankful of any comments or
replies you send to me.
Greetings, Diego y tal <deigote at gmail.com>
More information about the Haskell