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 ) ?
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With