OCaml 中的递归类型?

发布于 2024-10-13 09:49:43 字数 393 浏览 4 评论 0原文

大家好,这是我第一次在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2024-10-20 09:49:43
type ('a, 'b) tree = Empty | Leaf of 'b | Node of 'a * ('a, 'b) tree

你可能希望你的两个节点有多个子节点,尽管

type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a, 'b) tree * 'a * ('a, 'b) tree

PS:注意类型声明中, Foo of bar * bazFoo of (bar * baz) 是不一样:第一个是具有两个字段的构造函数 Foo,第二个只有一个字段,其类型为 (bar * baz)

type ('a, 'b) tree = Empty | Leaf of 'b | Node of 'a * ('a, 'b) tree

You probably want your Nodes two have more than one child, though

type ('a, 'b) tree = Empty | Leaf of 'b | Node of ('a, 'b) tree * 'a * ('a, 'b) tree

PS : Beware than in a type declaration, Foo of bar * baz and Foo of (bar * baz) are not the same : the first is a constructor Foo with two fields, the second has only one field, which is of type (bar * baz).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文