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



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


  parent reply	other threads:[~2009-07-30 20:22 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 [this message]
2009-07-30 21:54   ` Ligia Nistor
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=alpine.DEB.2.00.0907301612210.19900@sergyar \
    --to=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=ligia.nicoleta@gmail.com \
    /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).