caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Erik de Castro Lopo <mle+ocaml@mega-nerd.com>
To: caml-list@yquem.inria.fr
Subject: Combinatorics in a functional way
Date: Wed, 21 Feb 2007 20:36:03 +1100	[thread overview]
Message-ID: <20070221203603.e222647a.mle+ocaml@mega-nerd.com> (raw)

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:

    let find_combi p0 p1 =
        let out = ref [] in
	for i0 = 1 to p0 do
            for i1 = 1 to p0 do
                for i2 = 1 to p0 do
                    for i3 = 1 to p1 do
                        for i4 = 1 to p1 do
                            for i5 = 1 to p1 do
                                for i6 = 1 to p1 do
                                    for i7 = 1 to p1 do
                                        if test_combi i0 i1 i2 i3 i4 i5 i6 i7 then
                                            lst := (i0, i1, i2, i3, i4, i5, i6, i7) :: lst
                                    done ;
                                done ;
                            done ;
                        done ;
                    done ;
                done ;
            done ;
        done ;
        !lst

This works, but I find it excessively ugly. It feels like I'm coding
in C again!

Can anyone come up with a cleaner, more functional way of solving 
problems like this? I'm thinking that something like lazy lists
might be a solution.

Any tips appreciated.

Cheers,
Erik
-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo
+-----------------------------------------------------------+
Microsoft VISTA : Virus Infection Spyware Trojans and Adware!


             reply	other threads:[~2007-02-21  9:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-21  9:36 Erik de Castro Lopo [this message]
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
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=20070221203603.e222647a.mle+ocaml@mega-nerd.com \
    --to=mle+ocaml@mega-nerd.com \
    --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;
as well as URLs for NNTP newsgroup(s).