标准 ML 二叉树
我仍然遇到这个问题,所以我可以寻求更多帮助。
我们得到:
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 ,而在我的另一个情况下,当我说 Leaf of int 时应该能够使用 which 来指定 string 或 int。
谁能告诉我正确的答案是什么或为我提供一些帮助?
I am still having problems with this so I can going to ask for more help.
We are given:
datatype which = STRING of string | INT of int
Part 1. We are told we need to created another datatype named whichTree for a binary tree containing the values of type "which" where data is only at the leaves of the tree.
I assumed this would be the correct answer:
datatype whichTree = Empty | Leaf of which | Node of whichTree*whichTree;
It turns out it is not as when I run:
val inttree = Node(Leaf(1), Leaf(2));
I get an error.
I was able to figure out that I can do something like this:
datatype 'a whichTree = Empty | Leaf of 'a | Node of 'a whichTree * 'a whichTree;
or
datatype whichTree = Empty | Leaf of int | Node of whichTree*whichTree;
But neither of these seem correct to me as in my first case my datatype name has a 'a in it and in my other one I am saying Leaf of int when I should be able to specify string or int by using which.
Can anyone tell me what the correct answer it or provide me with some help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对
whichTree
的定义是正确的,您只是缺少inttree
中which
的构造函数:Your definition of
whichTree
is correct, you're just missing the constructor forwhich
in yourinttree
: