OCaml 中的递归类型?
大家好,这是我第一次在 Stack Overflow 上发帖,我在尝试在 OCaml 中构造类型时遇到了问题,
我正在尝试构造一个具有节点/叶子/等的类型树。这是我到目前为止所拥有的。
type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a * tree) | ....
我的节点应该是包含其名称和另一棵树作为元组的类型。但是当我尝试编译它时,它说树需要两个参数。所以我尝试了:
type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a * tree ('a*'b))
但我仍然收到错误。你注意到我做错了什么吗?谢谢!
Hi this is my first time posting on Stack Overflow and I've run into a problem while trying to construct a type in OCaml
I'm trying to construct a type tree that has nodes/leafs/etc. This is what I have so far.
type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a * tree) | ....
My node is supposed to be a type that contains the its name and another tree as a tuple. But when I tried to compile this it said tree required two arguments. So I tried:
type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a * tree ('a*'b))
and I was still getting an error. Anything that you notice I was doing wrong? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可能希望你的两个节点有多个子节点,尽管
PS:注意类型声明中,
Foo of bar * baz
和Foo of (bar * baz)
是不一样:第一个是具有两个字段的构造函数Foo
,第二个只有一个字段,其类型为(bar * baz)
。You probably want your Nodes two have more than one child, though
PS : Beware than in a type declaration,
Foo of bar * baz
andFoo of (bar * baz)
are not the same : the first is a constructorFoo
with two fields, the second has only one field, which is of type(bar * baz)
.