Can you give me a solution to create a function that reads the next character in a file stream, ignoring whitespace? (in SML/NJ).
You need to use the following libraries:
Char for your whitespace needs.
Text_IO for your streaming needs.
The following code is an example, which is not my finest code :)
let
(* Use TextIO library to open instream to file *)
val ins = TextIO.openIn("C:\\Test.txt")
(* This function takes the instream, and repeats
calling itself untill we are the end of the file *)
fun handleStream (ins : TextIO.instream) =
let
(* Read one character, hence the name, input1*)
val character = TextIO.input1 (ins)
(* This function decides whether to close the stream
or continue reading, using the handleStream function
recursively.
*)
fun helper (copt : char option) =
case copt of
(* We reached the end of the file*)
NONE => TextIO.closeIn(ins)
(* There is something! *)
| SOME(c) =>
((if (Char.isSpace c)
then
(*ignore the space*)
()
else
(* Handle the character c here *)
())
(* Recursive call *)
; handleStream(ins))
in
(* start the recursion *)
helper (character)
end
in
(* start handling the stream *)
handleStream( ins )
end