【问题标题】:Write a SML function that take the name of a file and return a list of char without spaces编写一个 SML 函数,该函数采用文件名并返回不带空格的 char 列表
【发布时间】:2020-01-13 13:01:53
【问题描述】:

在一次考试中,我发现了这个练习: “编写一个函数,获取文件名(即“text.txt”)并返回不带空格的字符列表”

例如:

“text.txt”包含“ab e ad c” 函数必须返回 -> [#"a",#"b",#"e",#"a",#"d",#"c"]

解题最简单的方法是什么?

我尝试使用库“TextIO”和函数“input1”,但我被卡住了。我不知道如何递归地实现该功能。有人可以帮忙吗?

【问题讨论】:

    标签: sml ml polyml


    【解决方案1】:
    fun chars filename =
      let
        val f = TextIO.openIn filename
        val s = TextIO.inputAll f
      in
        TextIO.closeIn f;
        List.filter (fn c => c <> #" ") (explode s)
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      相关资源
      最近更新 更多