Recursive types in OCaml?

Hi, this is my first post on Stack Overflow, and I ran into a problem when trying to build a type in OCaml

I am trying to build a type tree with nodes / leafs / etc. This is what I still have.

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

My node should be a type that contains its name and another tree as a tuple. But when I tried to compile this, the tree took two arguments. So I tried:

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

and I still got the error. Anything you noticed, I did wrong? Thank!

+5
source share
1 answer
type ('a, 'b) tree = Empty | Leaf of 'b | Node of 'a * ('a, 'b) tree

You probably want your nodes to have more than one child, although

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

PS: , , Foo of bar * baz Foo of (bar * baz) : - Foo , , (bar * baz).

+9

All Articles