问题
I have this SML/NJ code that reads a single line from a text file and then it will return a list for me, but I am having trouble making it do the same thing to every single line and stop when there are no more lines. Can anyone please help me by giving me a looping sample here?
fun readlist(infile : string) =
let val ins = TextIO.openIn infile
val list = []
fun listing() = [TextIO.inputLine ins]::list;
in listing()
end
回答1:
How about something like this:
fun readlist (infile : string) = let
val ins = TextIO.openIn infile
fun loop ins =
case TextIO.inputLine ins of
SOME line => line :: loop ins
| NONE => []
in
loop ins before TextIO.closeIn ins
end
来源:https://stackoverflow.com/questions/680916/looping-over-lines-of-a-text-file-in-sml-nj