open TextIO; datatype intOrEof = Eof | Int of int; fun digit(c) = c >= #"0" andalso c <= #"9"; fun startInt(file) = startInt1(file, input1(file)) and startInt1(file, NONE) = Eof | startInt1(file, SOME c) = if digit(c) then Int(ord(c)-ord(#"0")) else startInt(file); fun finishInt(i,file) = if i = Eof then Eof else finishInt1(i,file,input1(file)) and finishInt1(i, file, NONE) = i | finishInt1(i as Int(j), file, SOME c) = if digit(c) then finishInt(Int(10*j+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 = Eof then 0 else let val Int(j) = i in j + sumInts1(file) end end; fun sumInts(filename) = sumInts1(openIn(filename));