Reference no: EM13166800
Using SML: Generalize the program below to allow negative integers on the input. We assume that negative integers are preceded by the minus sign(-) rather than the tilde (~). Note that we not only have to recognize negative integers but we can no longer use -1 as a convenient value to indicate that the end of file has been reached. Hint: Return an integer option that tells whether the end of file has been reached.
open TextIO;
val END = ~1;
fun digit(c) = c >= #"0" andalso c <= #"9";
fun startInt(file) = startInt1(file,input1(file))
and startInt1(file, NONE) = END
| startInt1(file, SOME c) =
if digit(c) then ord(c) - ord(#"0")
else startInt(file);
fun finishInt(i,file) =
if i = END then END
else finishInt1(i,file,input1(file))
and finishInt1(i, file, NONE) = i
| finishInt1(i, file, SOME c) =
if digit(c) then
finishInt(10*i+ord(c)-ord(#"0"), file)
else i;
fun getInt(file) = finishInt(startInt(file), file)
fun sumInts1(file) =
let
val i = getInt(file)
in
if i = END then 0
else i + sumInts1(file)
end;
fun sumInts(filename) = sumInts1(openIn(filename));