From: Peng Zang <peng.zang@gmail.com>
To: caml-list@yquem.inria.fr
Cc: Ligia Nistor <ligia.nicoleta@gmail.com>
Subject: Re: [Caml-list] Cartesian product
Date: Thu, 30 Jul 2009 14:46:41 -0400 [thread overview]
Message-ID: <200907301446.47427.peng.zang@gmail.com> (raw)
In-Reply-To: <8401c38a0907301056x470687aah94b2b1295c6d1068@mail.gmail.com>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Not that I know of. But you can use this general implementation. It assumes
you have Enum (from Batteries, and ExtLib before). The (~~) prefix operator
is "Obj.magic".
Peng
(* makes the cross product of the given array of enumerations *)
let crossproductM enums initcount =
let numenums = Array.length enums in
let clean = Array.map clone enums in
let initstate = enums in
let rec cpmk cur curcount =
let reseti i = cur.(i) <- clone clean.(i) in
let tick () =
let rec tick_aux place =
if place >= numenums then ()
else
let ple = cur.(place) in
ignore (get ple);
if is_empty ple then (
reseti place;
tick_aux (place + 1)
)
in tick_aux 0; decr curcount in
let get () = Array.init numenums (fun i -> Option.get (peek cur.(i))) in
let nx () = match !curcount with
| 0 -> raise No_more_elements
| 1 -> decr curcount; get ()
| x -> assert (x >= 0); let ans = get () in tick (); ans in
let ct () = !curcount in
let cl () = cpmk (Array.init numenums (fun i -> clone cur.(i)))
(ref !curcount) in
make nx ct cl
in
( if initcount == 0 then empty ()
else cpmk initstate (ref initcount) )
let crossproduct enums =
let copy = Array.copy enums in
let initcount = Array.fold_left (fun acc e -> count e * acc) 1 copy in
crossproductM copy initcount
let cross2 (t1:'a t) (t2:'b t) : ('a * 'b) t =
~~(crossproduct [|~~t1; ~~t2|])
let cross3 (t1:'a t) (t2:'b t) (t3:'c t) : ('a * 'b * 'c) t =
~~(crossproduct [|~~t1; ~~t2; ~~t3|])
On Thursday 30 July 2009 01:56:50 pm 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.
>
> Thanks,
>
> Ligia
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)
iD8DBQFKceqXfIRcEFL/JewRAnM5AKC3dnxh9uWl7kdRBKW68koS1f3dhACgxau4
ubBf3OXzaxkMeU3Cb848lJY=
=XZ4Q
-----END PGP SIGNATURE-----
next prev parent reply other threads:[~2009-07-30 18:47 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 ` Peng Zang [this message]
2009-07-30 18:53 ` [Caml-list] " Peng Zang
2009-07-30 20:08 ` Erick Matsen
2009-07-30 20:22 ` Brian Hurt
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=200907301446.47427.peng.zang@gmail.com \
--to=peng.zang@gmail.com \
--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