This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Combinatorics in a functional way
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-02-21 (09:36) From: Erik de Castro Lopo Subject: Combinatorics in a functional way
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!