我如何参数化此函数以接受一个函数并将TextIO.closeOut作为输入?

问题描述 投票:0回答:1

是否可以对该辅助功能进行参数设置以接受TextIO.closeOut outstreamreadFileList xs outstream n作为输入?还是我必须在TextIO中使用该append函数来拥有一个不太丑陋的函数体?是否可以匹配它们的类型?

两个主体基本相同,唯一的区别是在NONE情况下,第二个返回函数。

    fun readFileList (x::xs) outstream n =
    case xs of
        [] => (let
              val instream = TextIO.openIn x
              val readline = TextIO.inputLine instream
              fun aux readline n =
                      case readline of
                          NONE => (TextIO.closeIn instream; TextIO.closeOut outstream)
                        | SOME s => (TextIO.output(outstream, (getLineWriteCode s n));
                                     aux (TextIO.inputLine instream) (n + 1))
              in
                  aux readline n
              end)
      | _ => (let
             val instream = TextIO.openIn x
             val readline = TextIO.inputLine instream
             fun aux readline n =
                 case readline of
                     NONE => (TextIO.closeIn instream; readFileList xs outstream n)
                   | SOME s => (TextIO.output(outstream, (getLineWriteCode s n));
                                aux (TextIO.inputLine instream) (n + 1))
            in
                aux readline n
            end)

function functional-programming sml smlnj
1个回答
0
投票

只需使用部分应用程序。邓诺(Dunno)为什么花这么长时间才弄清楚,但是行得通。我唯一想知道的是匿名lambda函数。我可以在其中放通配符之类的东西吗?

fun readFileList (x::xs) n outstream =  
    let
        val part = readFileList xs n
        val instream = TextIO.openIn x
        val readline = TextIO.inputLine instream
        fun aux readline n function =
            case readline of
                NONE => (TextIO.closeIn instream; function outstream)
              | SOME s => (TextIO.output(outstream, (getLineWriteCode s n));
                         aux (TextIO.inputLine instream) (n + 1) (fn s => ()))
    in
    case xs of
        [] => (aux readline n TextIO.closeOut)
      | _ => (aux readline n part)
    end

© www.soinside.com 2019 - 2024. All rights reserved.