caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ligia Nistor <ligia.nicoleta@gmail.com>
To: Brian Hurt <bhurt@spnz.org>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Cartesian product
Date: Thu, 30 Jul 2009 22:54:55 +0100	[thread overview]
Message-ID: <8401c38a0907301454q1c60ac2frf8b0ab9027a6475d@mail.gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.00.0907301612210.19900@sergyar>

[-- Attachment #1: Type: text/plain, Size: 1763 bytes --]

Thanks for the reply. This is how I thought of doing it, but in the module
TypeType, type t should be a list of types( the list has to be ref, so that
it can change its length). This way, you can do the cartesian product of an
arbitrary number of sets, not only of 2.

Ligia

On Thu, Jul 30, 2009 at 9:22 PM, Brian Hurt <bhurt@spnz.org> wrote:

>
>
> On Thu, 30 Jul 2009, Ligia Nistor wrote:
>
>  Hi,
>>
>> Is there an already implemented way of doing the Cartesian product of 2
>> sets
>> in OCaml? My sets are of type Set.Make(Types), where Types is a module I
>> have defined.
>>
>
> The biggest problem with implementing the cartesian product is that the
> type t*t is not the same as the type t.  But it's still not that hard. Say
> you have:
>
> module Type : sig
>        type t;;
>        val compare : t -> t -> int;;
> end = struct
>        ...
> end;;
>
> module TypeSet = Set.Make(Type);;
>
> Then you could do:
>
> module TypeType = struct
>        type t = Type.t * Type.t;;
>        let compare (x1, x2) (y1, y2) =
>                let r = Type.compare x1 y1 in
>                if r == 0 then
>                        Type.compare x2 y2
>                else
>                        r
>        ;;
> end;;
>
> module TypeTypeSet = Set.Make(TypeType);;
>
> As a side note, you could simply use Pervasives.compare there instead of
> the code I wrote, but that wouldn't necessarily preserve the ordering given
> by Type.compare.
>
> Once you have the set module for the cartesian products, you can use nested
> folds to create one:
>
> let cartesian_product s1 s2 =
>        let f x y t = TypeTypeSet.add (x, y) t in
>        let g x t = TypeSet.fold (f x) s2 t in
>        TypeSet.fold g s1 TypeTypeSet.empty
> ;;
>
> Hope this helps.
>
> Brian
>
>

[-- Attachment #2: Type: text/html, Size: 2415 bytes --]

  reply	other threads:[~2009-07-30 21:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-30 17:56 Ligia Nistor
2009-07-30 18:46 ` [Caml-list] " Peng Zang
2009-07-30 18:53   ` Peng Zang
2009-07-30 20:08     ` Erick Matsen
2009-07-30 20:22 ` Brian Hurt
2009-07-30 21:54   ` Ligia Nistor [this message]
2009-07-31 16:23     ` Michael Ekstrand
2009-07-30 21:26 ` [Caml-list] " Damien Guichard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8401c38a0907301454q1c60ac2frf8b0ab9027a6475d@mail.gmail.com \
    --to=ligia.nicoleta@gmail.com \
    --cc=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).