Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of Sets in ocaml

Tags:

set

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

like image 221
kafka Avatar asked Nov 18 '10 09:11

kafka


People also ask

What is a sequence in Ocaml?

Sequences. A sequence of type 'a Seq. t can be thought of as a delayed list, that is, a list whose elements are computed only when they are demanded by a consumer. This allows sequences to be produced and transformed lazily (one element at a time) rather than eagerly (all elements at once).

What is :: In Ocaml?

It is treated as an alphabetical character, so the following is equivalent: let rec sum xs = match xs with | [] -> 0 | x :: ys -> x + sum ys. Note that :: is technically a type constructor which is why you can use it in both patterns and expressions.

What is a signature in Ocaml?

Signatures are interfaces for structures. A signature specifies which components of a structure are accessible from the outside, and with which type. It can be used to hide some components of a structure (e.g. local function definitions) or export some components with a restricted type.


1 Answers

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.

like image 81
nlucaroni Avatar answered Oct 06 '22 00:10

nlucaroni