开发者

Recursive types in OCaml?

开发者 https://www.devze.com 2023-02-06 18:20 出处:网络
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

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!


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号