it took me quite a while to isolate the following. what does this program print? certainly "A" (written by the first system call) is different from "B"? import System main = do system "echo A > foo" a <- readFile "foo" system "echo B > foo" b <- readFile "foo" print (a == b) best regards, -- -- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 -- ---- http://www.imn.htwk-leipzig.de/~waldmann/ -------
The action readFile is a bit unsafe in that it does lazily interleaved IO -- that is, the file is read as you consume the string, and only the part of the string which you use will be read from the file -- if the file is 10G, but you only end up needing the first 100K of it, or only need to consume it a small bit at a time, this is great. On the other hand, if you want a record of the file's contents to be copied into memory before other modifications take place, it doesn't work. What you can do is to force the evaluation of the string before anything else takes place. Defining something like force u = do a <- u return $! a and then replacing readFile "foo" with force (readFile "foo") will result in the program working in the way that you probably expected. Hope this helps, - Cale On 4/18/05, Johannes Waldmann <waldmann@imn.htwk-leipzig.de> wrote:
it took me quite a while to isolate the following.
what does this program print? certainly "A" (written by the first system call) is different from "B"?
import System
main = do system "echo A > foo" a <- readFile "foo" system "echo B > foo" b <- readFile "foo" print (a == b)
best regards, -- -- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 -- ---- http://www.imn.htwk-leipzig.de/~waldmann/ -------
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
At 10:11 PM -0400 4/18/05, Cale Gibbard wrote:
The action readFile is a bit unsafe in that it does lazily interleaved IO -- that is, the file is read as you consume the string, and only the part of the string which you use will be read from the file -- if the file is 10G, but you only end up needing the first 100K of it, or only need to consume it a small bit at a time, this is great. On the other hand, if you want a record of the file's contents to be copied into memory before other modifications take place, it doesn't work.
What you can do is to force the evaluation of the string before anything else takes place.
Defining something like force u = do a <- u return $! a
and then replacing readFile "foo" with force (readFile "foo") will result in the program working in the way that you probably expected.
Well, not quite. ($!) (like `seq`, out of which it's built) forces evaluation only to "weak head normal form": essentially enough to determine the top-level constructor. Here, for String, that means only the first character need be evaluated, which in practice means only the first bufferful of the file is read. You need to use ($!!) or something like it to force evaluation of the entire file contents. [See http://www.mail-archive.com/haskell@haskell.org/msg15819.html.] Dean
Hope this helps, - Cale
On 4/18/05, Johannes Waldmann <waldmann@imn.htwk-leipzig.de> wrote:
it took me quite a while to isolate the following.
what does this program print? certainly "A" (written by the first system call) is different from "B"?
import System
main = do system "echo A > foo" a <- readFile "foo" system "echo B > foo" b <- readFile "foo" print (a == b)
best regards, -- -- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 -- ---- http://www.imn.htwk-leipzig.de/~waldmann/ -------
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
participants (3)
-
Cale Gibbard -
Dean Herington -
Johannes Waldmann