Hello, I am trying to learn SML, but drowning in its streams.
The following function does what I want in SML/NJ, but not in PolySML. Is the function wrong, but SML/NJ lenient and PolySML not? If the function is wrong, what is the best way to get input from stdin without a newline?
fun askName () = let val a = TextIO.inputLine TextIO.stdIn val b = String.translate (fn x => case x of #"\n" => ""|_ => str(x)) in case a of SOME("\n") => NONE | SOME(s) => SOME (b s) | NONE => NONE end
Thanks Johan
Johan, There's nothing wrong with the function or Poly/ML or SML/NJ's implementation. I think the difficulty you are having has to do with differences in way Poly/ML and SML/NJ handle their read-eval-print loops and the interaction between this and TextIO. From your previous message to me it was clear that you are running this interactively. If you type askName(); followed by a carriage return to Poly/ML the read-eval-print loop reads up to and including the semicolon, leaving the newline introduced by the carriage return in the buffer. Your call to TextIO.inputLine then reads this. The next line you type then goes to Poly/ML.
A quick experiment with SML/NJ shows that it does things slightly differently. It seems to read the whole line into its own buffer and then compile parts of it. You can see the difference with
Poly/ML
askName(); fred;
val it = SOME " fred;": string option joe; Error-Value or constructor (joe) has not been declared Found near joe Static Errors
SML/NJ - askName(); fred; joe; val it = SOME "joe;" : string option stdIn:12.12-12.16 Error: unbound variable or constructor: fred
The handling of the read-eval-print loop is not standardised so different compilers can perfectly legitimately implement this differently. If you have two programs both reading the same stream you need to be clear on exactly what they are doing.
David
Johan wrote:
Hello, I am trying to learn SML, but drowning in its streams.
The following function does what I want in SML/NJ, but not in PolySML. Is the function wrong, but SML/NJ lenient and PolySML not? If the function is wrong, what is the best way to get input from stdin without a newline?
fun askName () = let val a = TextIO.inputLine TextIO.stdIn val b = String.translate (fn x => case x of #"\n" => ""|_ => str(x)) in case a of SOME("\n") => NONE | SOME(s) => SOME (b s) | NONE => NONE end
Thanks Johan
polyml mailing list polyml@inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml