caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Combinatorics in a functional way
@ 2007-02-21  9:36 Erik de Castro Lopo
  2007-02-21 10:36 ` [Caml-list] " David Baelde
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Erik de Castro Lopo @ 2007-02-21  9:36 UTC (permalink / raw)
  To: caml-list

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!


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2007-02-22 10:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-21  9:36 Combinatorics in a functional way 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
2007-02-21 14:36   ` Brian Hurt
2007-02-22 10:01     ` Erik de Castro Lopo

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).