2014-06-18 48 views
0

當我嘗試編譯我的ML程序時,出現錯誤:「操作員和操作數不同意」。錯誤SML NJ-操作符和操作數不同意

candidates.sml:78.8-78.40 Error: operator and operand don't agree [tycon mismatch] 
operator domain: int * (int * 'Z * 'Y) list 
operand:   int * (int * real) list 
in expression: 
tr (n,candidates) 

我明白錯誤,但我找不到解決方案。 在那裏我得到錯誤代碼的部分是:

fun agonas fileName = 
    let 
     fun tr(n,[])=[] 
     | tr(n,((a,b,c)::d))=((n-a+1),b,c)::(tr(n,d)) 
     val (n,l,candidates) = parse fileName 
     val cand = tr(n,candidates) 
    in 
     my_solution(l,cand) 
    end; 

,那裏的考生與下面的部分相關:

fun parse file = 
let 
(* a function to read an integer from an input stream *) 
    fun next_int input = 
    Option.valOf (TextIO.scanStream (Int.scan StringCvt.DEC) input) 
(* a function to read a real that spans till the end of line *) 
    fun next_real input = 
    Option.valOf (TextIO.inputLine input) 
(* open input file and read the two integers in the first line *) 
    val stream = TextIO.openIn file 
    val n = next_int stream 
    val l = next_int stream 
val _ = TextIO.inputLine stream 
(* a function to read the pair of integer & real in subsequent lines *) 
    fun scanner 0 acc = acc 
     | scanner i acc = 
     let 
      val d = next_int stream 
      val (SOME v) = Real.fromString (next_real stream) 
     in 
      scanner (i - 1) ((d, v) :: acc) 
     end 
in 
    (n, l, rev(scanner n [])) 
end; 

fun my_solution (n , l ,candidates) = [2 ,3 ,5 ,4 ,6] 
fun agonas fileName = my_solution (parse fileName) 

我將不勝感激,如果你能找到mistake.Thanks提前。

回答

0

的問題是,parse,使用scanner,建立對列表 - (int * real) list - 而tr期望得到三倍的名單 - (int * 'Z * 'Y) list

不知道什麼tr是應該做的,快速和骯髒的解決將是改變

tr(n,((a,b,c)::d))=((n-a+1),b,c)::(tr(n,d)) 

tr(n,((a,b)::d))=((n-a+1),b)::(tr(n,d)) 

但是,這可能是錯誤的解決方案 - 這取決於什麼代碼應該這樣做。

(有時,它幫助明確地寫出自己的類型 - 甚至寫代碼之前 - 而不是依靠類型推斷趕上,你需要做一些更多的思考的地方)

0

錯誤消息說明了這一切:違規行呼叫trans,這是一個函數,期望兩個參數,第二個是三元組列表。但是,您正在向其傳遞一個的列表(由您的scanner函數生成)。

您沒有向我們展示trans函數,所以我不能更具體地說明適當的修補程序是什麼。

+0

對不起,功能TR是同功能trans.I現在編輯。你可以看到錯誤?感謝您的時間。 – user3754251

+0

那麼,這個錯誤就是我已經描述過的:你的''parse'''''scanner'函數和你的'tr'函數不同意'candidates'列表的結構。很難說如何解決它而不知道這段代碼試圖達到什麼目的。但對於初學者來說,你可以嘗試在函數tr中刪除元組中的'c'組件。 –