开发者

Definition of Sets in ocaml

I have a problem with the creation of a collection containing heterogeneous elements, in particular element will开发者_运维百科 be structured as follows:

(a,1), ((a,1),1)), ((a,1),1),1) and so on....

can I do this using the module Set of ocaml?

moreover is there also some function that allows me to make the Cartesian product between sets (also heterogeneous ) ?


You cannot build sets of heterogeneous elements. Of course you can define a type to unify the types if you know them in advance. It looks like you do, and it may be the recursive type defined by:

type ('a,'b) r = | L of 'a 
                 | N of (('a,'b) r * 'b)

Thus, your examples would constructed as,

N (L a,1)
N ( N (L a,1),1)
N ( N ( N (L a,1),1),1)

Then you would just build the Ordered module to encompass the compare function.

In the case of creating the Cartesian product, you wouldn't be dealing with heterogeneous elements at this point, but a tuple of the previous type. This would require a new Ordered module to deal with those compares.


No, from http://caml.inria.fr/pub/docs/manual-ocaml/libref/Set.S.html you can see that the sets in module Set are homogeneous.

You can use the approach descibed in http://alan.petitepomme.net/cwn/2010.02.09.html for dictionaries instead.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜