[Haskell-cafe] using an external application
lemming at henning-thielemann.de
Fri Nov 2 04:35:24 EDT 2007
On Fri, 2 Nov 2007, Petr Hoffmann wrote:
> I'm solving the following problem - I need to use an external
> application - give it the input data and receive its output.
> However, when multiple calls are made, the results are not
> as expected. The simplified version of the problem is given
> import System.Cmd
> main = do
> System.Cmd.system "echo hello >output.txt" -- use the external
> application to create an output file
> o1 <- readFile "output.txt"
> System.Cmd.system "echo bye >output.txt" -- the second call to
> the external application
> o2 <- readFile "output.txt"
> putStr o1 -- "hello" expected, but "bye" printed
> return 0
You fell into the trap, that 'readFile' works lazily, that is, data is
only read when needed. If o1 is not completely evalutated until the second
write to output.txt, it will not be read correctly from the disk.
I think it was not a good choice to make the lazy version of 'readFile'
the default. I think it would have been better to provide a strict
'readFile' and another 'readFileLazy' with warnings in the documentation.
More information about the Haskell-Cafe