Reputation: 25812
I wish to create a bst
(binary search tree) union type.
It is either leaf
or node
. For node, it takes 'a
key and 'b
value.
I did this:
type 'a*'b bst =
| Node of 'a * 'b * ('a*'b) bst * ('a*'b) bst
| Leaf;;
but it doesn't work
How should I do it?
Upvotes: 1
Views: 264
Reputation: 2004
In addition, you may get into trouble because 'a
needs a comparison operator. This is why in the standard library, the Map
and Set
modules are implemented as functors, so that it is possible to specify the comparison ordering.
If you decide to go along the 'a
polymorphism way, you'll have to use the "magical" default comparison operator compare
.
The problem is different in F#, because there are ways to attach a comparison operator to a type.
Upvotes: 0
Reputation: 5048
The syntax for multi-parameter polymorphic types is the following:
type ('a, 'b) bst =
| Node of 'a * 'b * ('a, 'b) bst * ('a, 'b) bst
| Leaf;;
Upvotes: 4
Reputation: 80276
The syntax you are looking for is:
# type ('a, 'b) bst =
| Node of 'a * 'b * ('a,'b) bst * ('a,'b) bst
| Leaf;;
type ('a, 'b) bst = Node of 'a * 'b * ('a, 'b) bst * ('a, 'b) bst | Leaf
Upvotes: 4