From: Erick Matsen <ematsen@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Cartesian product
Date: Thu, 30 Jul 2009 13:08:20 -0700 [thread overview]
Message-ID: <243054520907301308r76c865e5v477fbea3aaa74af0@mail.gmail.com> (raw)
In-Reply-To: <200907301453.41250.peng.zang@gmail.com>
Hello Ligia---
The following code takes cartesian products of lists:
let listListPrepend x ll = List.map (fun l -> x :: l) ll
let rec cartesianProduct = function
| aList :: listList ->
let prev = cartesianProduct listList in
List.flatten (
List.map (fun x -> listListPrepend x prev) aList)
| [] -> [[]]
and could be easily adapted to the case of sets.
Erick
On Thu, Jul 30, 2009 at 11:53 AM, Peng Zang<peng.zang@gmail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I really should provide a bit more context. It's a general implementation for
> crossing [Enum.t]s. Eg.
>
> let a = List.enum [1;2;3;4]
> let b = List.enum ['a'; 'b'; 'c']
>
> let c = cross2 a b
>
> yields [(1,'a'); (2,'a'); (3,'a'); (4,'a'); (1,'b'); ..]
>
>
> There are implementations for converting Set to Enum in the same library(s)
> that provide Enum.
>
> Peng
>
> On Thursday 30 July 2009 02:46:41 pm Peng Zang wrote:
>> 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)
>
> iD8DBQFKcew1fIRcEFL/JewRArLiAKCYGbfEHY5igi0IlM6f71tNAL2OqACeMUPR
> qR8nC+F5QNRgaX19gSVtMsA=
> =8JeB
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
next prev parent reply other threads:[~2009-07-30 20:08 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 [this message]
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=243054520907301308r76c865e5v477fbea3aaa74af0@mail.gmail.com \
--to=ematsen@gmail.com \
--cc=caml-list@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