Looping over lines of a text file in SML/NJ

smlnj

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

Best Answer

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
Related Topic