我有一个元组列表[("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中使用多个表达式,必须将多行括在括号中,如下所示:
(expr 1; expr2; ...; exprn);
因此,为了修正我的错误,我只是对我的打印行使用了这个形式:
fun outputTable [] = []
| outputTable ((hd, n)::tl) =
(print "------------------n";
print ("| " ^ hd ^ " | " ^ Int.toString(n) ^ " |n");
print "-------------------n";
outputTable tl);