2011-11-02 61 views
0

我知道這個問題之前已經被問過了,但以前的問題都沒有答案爲我工作,所以我會嘗試一種不同的方法。標準ML二叉樹數據類型

我已經做到了這一點:

> datatype which = STRING of string | INT of int; 
datatype which = INT of int | STRING of string 
> datatype whichTree = Empty | Leaf of which | Node of whichTree*whichTree; 
datatype whichTree = Empty | Leaf of which | Node of whichTree * whichTree 

,但是當我嘗試建立一個樹

> val mytree = Node(Leaf(which 2), Leaf(which 6)); 

我得到的錯誤。

Error-Value or constructor (which) has not been declared Found near 
Node(Leaf(which(2)), Leaf(which(6))) 
Error-Value or constructor (which) has not been declared Found near 
Node(Leaf(which(2)), Leaf(which(6))) 
Static errors (pass2) 

回答

1

which是數據類型的名稱;它不是一個構造函數。相反,你必須創建一棵樹如下:

> val mytree = Node(Leaf(INT 2), Leaf(STRING "6")); 
+0

啊,謝謝你的幫助。我想我還有很多要了解ML –