2015-10-25 64 views
1

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

回答

0

要在SML中使用多個表達式,您必須在括號中包含多行,如下所示:

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

所以修復我的錯誤,我只是用這種形式爲我打印線:

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