2012-03-28 64 views
3

下面的代码将报告中的语法错误消息:如何在OCaml中定义彼此相关的两种类型?

type 'a edge = 
    |Empty 
    |End of 'a * 'a vertex * 'a vertex and 
type 'a vertex = 
    |Empty 
    |Vertex of 'a * 'a edge list;; 

如何定义两种称呼对方?

+1

这是一个常见的问题。这里有一个类似的链接:http://stackoverflow.com/questions/3026123/ocaml-forward-declaration – 2012-03-28 01:18:49

+0

谢谢。没有意识到前进宣言的职位。 – lkahtz 2012-03-28 01:23:36

回答

6

第二type不是语法正确:

type 'a edge = 
    |Empty 
    |End of 'a * 'a vertex * 'a vertex 
and 'a vertex = 
    |Empty 
    |Vertex of 'a * 'a edge list;; 
+0

(关键是你需要用'和'来定义相互递归的类型。) – 2012-03-28 01:21:58