Looping over lines of a text file in SML/NJ

谁说我不能喝 提交于 2019-12-20 04:13:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!