My students have been writing a toy ATM request processing program
that updates a list of bank accounts, stored in a file.

However, we have noticed that if you try to write the data back
into the same file that you read it from, the original contents
of the file get overwritten before they are read.

As a simplified example of this, the following Haskell command

   do {s<- readFile "data"; writeFile "data" "123"; putStrLn s}

always displays "123", rather than the original contents of the file.

I think this is a bug?  I/O commands should conceptually be done
in the order that they appear in the 'do' sequence?
If it is a bug, it exists in Hugs 1.3, Jan98 1.4, and June98 1.4.

(It seems to me that this might be non-trivial to fix though,
because the lazy evaluation means that s is not evaluated until
needed, and for efficiency we do not usually want the entire "data" 
file to be read immediately.  However, if we detect that the "data"
file is about to be overwritten by a following command, we must read 
its contents first, or take a copy of the file.  Tricky!)

Mark.

Dr Mark Utting, Lecturer
Department of Computer Science
School of Computing and Mathematical Sciences
The University of Waikato       Tel:   +64 7 838 4791
Private Bag 3105                Fax:   +64 7 838 4155
Hamilton                        Email: [EMAIL PROTECTED]
New Zealand                     Web:   http://www.cs.waikato.ac.nz/~marku

The highest bandwidth output from a human is the notes pouring
from a concert pianist's fingers.


Reply via email to