2

我仍然有这个问题,所以我可以寻求更多帮助。

我们得到:

datatype which = STRING of string | INT of int

第 1 部分。我们被告知需要为包含“which”类型值的二叉树创建另一个名为 whichTree 的数据类型,其中数据仅位于树的叶子处。

我认为这将是正确的答案:

datatype whichTree = Empty | Leaf of which | Node of whichTree*whichTree;

事实证明它不是我跑步时的样子:

val inttree = Node(Leaf(1), Leaf(2));

我得到一个错误。

我能够弄清楚我可以做这样的事情:

datatype 'a whichTree = Empty | Leaf of 'a | Node of 'a whichTree * 'a whichTree;

或者

datatype whichTree = Empty | Leaf of int | Node of whichTree*whichTree;

但这些对我来说似乎都不正确,因为在我的第一种情况下,我的数据类型名称中有一个 'a,而在我的另一种情况下,我说的是 int 的叶子,当我应该能够使用 which 来指定 string 或 int 时。

谁能告诉我正确答案是什么或为我提供一些帮助?

4

1 回答 1

4

您的定义是正确的,whichTree您只是缺少以下构造函数:whichinttree

val inttree = Node(Leaf(INT 1), Leaf(INT 2))
于 2010-11-05T03:41:35.833 回答