【问题标题】:Print tuple list into a standard output 'table'将元组列表打印到标准输出“表”中
【发布时间】:2015-10-25 22:38:00
【问题描述】:

我有一个元组列表 [("hi", 1), ("yo", 2)];,我想使用 sml 中的标准输出 print s; 将此列表“转换”为表格。

例如上面的元组列表会产生如下输出:

------------
| "hi" | 1 |
------------
etc...
------------

我有这个代码,但它给了我一个错误:

fun outputTable [] = print "Nothing! \n"
  | outputTable ((hd, n)::tl) =
      print "-----------"
      print "| "^hd^" | "^n^" |"
      print "-----------"
      outputTable tl;

这是错误:

stdIn:15.2-17.21 Error: operator is not a function [tycon mismatch]  
operator: unit
  in expression:
    (print "-----------") print
stdIn:15.2-17.21 Error: operator is not a function [tycon mismatch]
operator: string
  in expression:
    " |" outputTable
stdIn:13.5-17.21 Error: right-hand-side of clause doesn't agree with function result type [tycon mismatch]
expression: string
result type:  unit
in declaration:
    outputTable =
      (fn nil =print "Nothing!"
        | :: (<pat>,<pat>) =<exp^ <exp^ <exp<exp>)

【问题讨论】:

    标签: sml smlnj


    【解决方案1】:

    要在 SML 中使用多个表达式,您必须将多行括在括号中,如下所示:

    (expr 1; expr2; ...; exprn);
    

    所以为了解决我的错误,我只是用那个表格来打印我的打印行:

    fun outputTable [] = []
      | outputTable ((hd, n)::tl) =
          (print "------------------\n";
          print ("| " ^ hd ^ " | " ^ Int.toString(n) ^ " |\n");
          print "-------------------\n";
          outputTable tl);
    

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 2018-08-10
      相关资源
      最近更新 更多