From: "Damien Guichard" <alphablock@orange.fr>
To: "caml-list@yquem.inria.fr" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Cartesian product
Date: Thu, 30 Jul 2009 23:26:07 +0200 [thread overview]
Message-ID: <200907302326059843328@orange.fr> (raw)
In-Reply-To: <8401c38a0907301056x470687aah94b2b1295c6d1068@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1501 bytes --]
Hi Ligia Nistor,
Supposing you want to implement the Cartesian product of 2 sets, and supposing you implement sets as (balanced) sorted binary trees, here is how i would do that :
implement a functor that, given x:A and a set B, maps B to a new set of pairs (x,y), y:B
this functor transforms each item of A into a new balanced tree
if you map this functor to A the result is a tree of trees
supposing you can merge all these trees you then get the cartesian product
howewer you don't actually need the tree of trees, all you need is the merged result
thus, instead of doing (3) you apply the (1) functor as f argument of the following flatten_map function :
type 'a set =
tree option
and tree =
{left: 'a set; item: 'a; right: 'a set}
let rec flatten_map f p = function
| None -> p
| Some n ->
union
(flatten_map f p n.left)
(flatten_map f (f n.item) n.right)
let flatten_map f = flatten_map f None
It seems to me that my solution gives you the cartesian product as an ordered (balanced) binary tree set in optimal time.
- damien
Damien Guichard
2009-07-30
En réponse au message
de : Ligia Nistor
du : 2009-07-30 19:56:57
À : caml-list@yquem.inria.fr
CC :
Sujet : [Caml-list] Cartesian product
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
[-- Attachment #2: Type: text/html, Size: 5253 bytes --]
prev parent reply other threads:[~2009-07-30 21:26 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
2009-07-31 16:23 ` Michael Ekstrand
2009-07-30 21:26 ` Damien Guichard [this message]
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=200907302326059843328@orange.fr \
--to=alphablock@orange.fr \
--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