On 20/09/2012 08:20, Lars-Henrik Eriksson wrote:
19 sep 2012 kl. 18.00 skrev David Matthews:
Also the same ML code *will* behave differently when run interactively or stand-alone using Poly/ML:
Poly/ML 5.5.0 Release
fun f() = (print "Say something: "; TextIO.inputLine TextIO.stdIn; print "Thank you.\n");
val f = fn: unit -> unit
PolyML.export("test",f);
val it = (): unit
f();Hello
Say something: Thank you. val it = (): unit
unix> ./test Say something: Hello Thank you.
I believe that the stand-alone behavior is the reasonable one -- it also agrees with SML/NJ and Moscow ML.
If you remember that the read-eval-print loop uses TextIO.input1 rather than TextIO.inputLine it all makes sense. At the end of compiling the next character to be read from stdIn is the character immediately after the closing semicolon. That may or may not be a newline character depending on what the rest of the input was. If the code that has just been compiled wants to read from the stream this is where it starts. The next time round the loop the compiler starts after anything that has been removed. When running as a stand-alone executable the behaviour is exactly the same; it's just that the compiler has not been run so the function starts at the beginning of the stream.
Does this cause problems? Yes, for me it does. We use ML in the introductory programming course for CS majors and it causes unnecessary complications for the students.
I am not completely wedded to the present way of doing things but I feel there has to be a stronger rationale for changing it. I would be interested to know if anyone else has feelings one way or the other.
David