2013-12-08 54 views
0

如何更正此錯誤?OCaml我在哪裏犯了一個錯誤?

type 'a drzewo = | Puste | Wezel of 'a * 'a drzewo * 'a drzewo 

let rec inorder t = 
    match t with 
    | Puste -> print_int (-1) 
    | Wezel (v, l, r) -> (inorder l; print_int v; inorder r) 

let tree = 
    (1, 
    (Wezel (2, (Wezel (3, Puste, Puste)), 
     (Wezel (4, (Wezel (5, Puste, Puste)), Puste)))), 
    (Wezel (6, Puste, Puste))) 

let _ = inorder tree 

和日誌錯誤:

let _ = inorder tree;; Error: This expression has type int * int drzewo * int drzewo but an expression was expected of type int drzewo

回答

1

的問題是在這裏

let tree = 
    (1, 
    (Wezel (2, (Wezel (3, Puste, Puste)), 
     (Wezel (4, (Wezel (5, Puste, Puste)), Puste)))), 
    (Wezel (6, Puste, Puste))) 

你沒有在前面加Wezel


它應該是:

let tree = 
     Wezel (1, 
     (Wezel (2, (Wezel (3, Puste, Puste)), 
      (Wezel (4, (Wezel (5, Puste, Puste)), Puste)))), 
     (Wezel (6, Puste, Puste))) 
0

你的錯誤信息已經是非常豐富的,只是再仔細重新閱讀。 'tree'的類型是'int * int drzewo * int drzewo'(一個三元組),但它必須是'int drzewo'(因爲inorder的類型是'int drzewo - > unit')。

所以才更正:

let tree = Wezel (1, 
      (Wezel (2, (Wezel (3, Puste, Puste)), 
       (Wezel (4, (Wezel (5, Puste, Puste)), Puste)))), 
      (Wezel (6, Puste, Puste))) 
相關問題