将元组列表打印到标准输出表#

时间:2015-10-25 13:35:40

标签: sml smlnj

我有一个元组列表[("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>)

1 个答案:

答案 0 :(得分:0)

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

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

因此,为了解决我的错误,我只是将该表单用于我的打印行:

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