【问题标题】:How to print a list如何打印列表
【发布时间】:2016-04-23 04:01:34
【问题描述】:

我以为我在某处读到了一些关于 List.toString 的内容,但对于 l = [1,2,3]; print (List.toString (l) ),我得到的是 Error: unbound variable or constructor: toString in path List.tostring,而只有 print l 给了我

Error: operator and operand don't agree [tycon mismatch]
  operator domain: string
  operand:         int list
  in expression:
    print a

那么,如何将列表转换为字符串并打印出来呢?这不可能那么难,但是在搜索如何打印一个简单的列表时,我没有找到答案。在这种情况下,我有一个整数列表。

我找到了这个

fun f (x: int list) = (PolyML.print x; ());

Here.

但它给了我Error: unbound structure: PolyML in path PolyML.print

【问题讨论】:

  • 再看一下您链接到的页面,我认为它提供了足够的解释。 PolyML 是另一个 SML 编译器,你找不到那个结构,它在 SML/NJ 编译器中不可用:polyml.org
  • 那么,如何在 SML/NJ 中打印列表?
  • 我讨厌这种语言。谢谢。
  • 很遗憾听到这个消息。我一开始也觉得这很令人沮丧,但它还有其他东西可以提供。

标签: list printing sml smlnj


【解决方案1】:

这是一个可以在 SML/NJ 中使用的 SML 版本:

fun printList xs = print(String.concatWith ", " (map Int.toString xs));

该函数接受一个 int 列表,将其转换为字符串表示形式的列表,然后用“,”连接字符串,最后打印结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    相关资源
    最近更新 更多