1

我有这个 SML/NJ 代码,它从文本文件中读取一行,然后它会为我返回一个列表,但是我很难让它对每一行都做同样的事情,并在没有更多行时停止。任何人都可以在这里给我一个循环样本来帮助我吗?

fun readlist(infile : string) =
let val ins = TextIO.openIn infile

    val list = []
     fun listing() = [TextIO.inputLine ins]::list;

in listing()
end
4

1 回答 1

7

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
于 2009-04-12T22:38:23.330 回答