将项目添加到列表 SML

Adding items to list SML

我是 SML 的新手,我正在尝试将一些项目添加到列表中

fun foo(inFile : string, outFile : string) = let
val file = TextIO.openIn inFile
val outStream = TextIO.openOut outFile
val contents = TextIO.inputAll file
val lines = String.tokens (fn c => c = #"\n") contents
val lines' = List.map splitFirstSpace lines
fun helper1(lis : string list) =
    case lis of
          [] => ( TextIO.closeIn file; TextIO.closeOut outStream)
      |  c::lis => ( TextIO.output(outStream, c);
    helper1(lis))
fun helper(lis : (string * string) list, stack : string list) =
    case lis of
          [] => stack
      |  c::lis => ( act(#1 c, #2 c)::stack;
    helper(lis, stack))
val x = helper(lines', [])
in
helper1(x)
end;

每当我 运行 代码时,我都会得到一个空白的输出文件,我无法弄清楚为什么,但我知道辅助函数正在从 "act" 函数,因为我使用 print(action(...))

测试了它

谢谢

这部分有问题:

( act(#1 c, #2 c)::stack; helper(lis, stack) )

这是创建一个新列表,然后在执行递归调用之前立即将其丢弃。你想做的是

helper(lis, act(#1 c, #2 c)::stack)

附加提示:您的两个辅助函数都可以通过 List.appList.foldl.

的简单使用来替换

编辑:进一步提示:事实上,你可以这样写

helper(lis, act(c)::stack)

因为带有 "two arguments" 的函数只是一个接受一对的函数。