在 SML/NJ 中循环文本文件的行

发布于 2024-07-16 05:33:39 字数 282 浏览 4 评论 0原文

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

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

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

in listing()
end

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-07-23 05:33:39

像这样的事情怎么样:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文