2017-03-25 68 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,[])是不同类型的。