List 将元组插入Ocaml列表
我有点困惑,因为编译器告诉我List 将元组插入Ocaml列表,list,ocaml,List,Ocaml,我有点困惑,因为编译器告诉我1[5;2] 不是两种情况下的元组: (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
1[5;2]
不是两种情况下的元组:
(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*)
::
的优先级高于,
。因此,第二行定义的值与第一行定义的值不同。相反,它定义了1,([5;2]:[6[5;1];2[16;1]])
代码的问题是,has_node
希望列表g
包含n
类型的元素,而(n,[])
则是另一种类型