2017-03-25 66 views
1

我稍微困惑,因爲編譯器告訴我,1, [5;2] 不是這兩種情況下一個元組:插入元組進入ocaml的名單

(1,[5;2])::[6,[5;1]; 2,[16;1]]

這不起作用

(爲什麼?)

1,[5;2]::[6,[5;1]; 2,[16;1]]

我問這個,因爲我需要解決我的問題:

type node = int 
type edge = node * node 
type graph = (node * node list) list 

let has_node g n = List.exists ((=) n) g 

let insert_node g n = 
    if has_node g n then g else (n, [])::g (*here is where the compiler complains*) 

回答

3

::具有比,更高的優先級。因此,第二行沒有定義與第一行相同的值。相反,它定義了1,([5;2]::[6,[5;1]; 2,[16;1]])

與您的代碼的問題是,has_node預計列表g包含的n類型的元素,同時(n,[])是不同類型的。