From: Fernando Alegre <fernando@cc.gatech.edu>
To: Erik de Castro Lopo <mle+ocaml@mega-nerd.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Combinatorics in a functional way
Date: Wed, 21 Feb 2007 08:46:35 -0500 [thread overview]
Message-ID: <20070221134635.GA28981@gaia.cc.gatech.edu> (raw)
In-Reply-To: <20070221203603.e222647a.mle+ocaml@mega-nerd.com>
Hi,
This is what I would do:
let get i (i0,i1,i2,i3,i4,i5,i6,i7) = match i with
| 0 -> i0 | 1 -> i1 | 2 -> i2 | 3 -> i3 | 4 -> i4 | 5 -> i5 | 6 -> i6
| 7 -> i7 | _ -> invalid_arg "get"
let set i (i0,i1,i2,i3,i4,i5,i6,i7) x = match i with
| 0 -> (x,i1,i2,i3,i4,i5,i6,i7) | 1 -> (i0,x,i2,i3,i4,i5,i6,i7)
| 2 -> (i0,i1,x,i3,i4,i5,i6,i7) | 3 -> (i0,i1,i2,x,i4,i5,i6,i7)
| 4 -> (i0,i1,i2,i3,x,i5,i6,i7) | 5 -> (i0,i1,i2,i3,i4,x,i6,i7)
| 6 -> (i0,i1,i2,i3,i4,i5,x,i7) | 7 -> (i0,i1,i2,i3,i4,i5,i6,x)
| _ -> invalid_arg "set"
let next bounds index =
let rec loop i index =
if i = 8 then index else
let x = get i index in
if x < get i bounds then set i index (x+1)
else loop (i+1) (set i index 0)
in
loop 0 index
let fold f acc bounds =
let rec next i index =
if i = 7 then index
else if index < get i bounds then set i (index+1)
else next (i+1) (set i index 0)
and loop a index =
if i = 8 then acc else loop (f a index) (next bounds index)
in
loop 0 (0,0,0,0,0,0,0)
let find_combi ((p0,p1,p2,p3,p4,p5,p6,p7) as bounds) =
fold (fun l x -> x :: l) [] bounds
Fernando
On Wed, Feb 21, 2007 at 08:36:03PM +1100, Erik de Castro Lopo wrote:
> Hi all,
>
> I'm currently working on something where I need to to generate a
> set of permutations that fit a set of rules. Currently I am doing
> something like:
next prev parent reply other threads:[~2007-02-21 13:46 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-02-21 9:36 Erik de Castro Lopo
2007-02-21 10:36 ` [Caml-list] " David Baelde
2007-02-21 12:17 ` Frédéric van der Plancke
2007-02-21 11:06 ` Pietro Abate
2007-02-21 13:19 ` Jacques Carette
2007-02-21 11:29 ` Nicolas Pouillard
2007-02-21 12:24 ` Gabriel Kerneis
2007-02-21 13:46 ` Fernando Alegre [this message]
2007-02-21 14:36 ` Brian Hurt
2007-02-22 10:01 ` Erik de Castro Lopo
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=20070221134635.GA28981@gaia.cc.gatech.edu \
--to=fernando@cc.gatech.edu \
--cc=caml-list@yquem.inria.fr \
--cc=mle+ocaml@mega-nerd.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