From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id CB704BC6B for ; Wed, 21 Feb 2007 10:36:11 +0100 (CET) Received: from smtp.syd.people.net.au (smtp.syd.people.net.au [218.214.225.98]) by concorde.inria.fr (8.13.6/8.13.6) with SMTP id l1L9a8Nw027397 for ; Wed, 21 Feb 2007 10:36:10 +0100 Received: (qmail 5031 invoked from network); 21 Feb 2007 09:36:19 -0000 Received: from unknown (HELO coltrane.mega-nerd.net) (218.214.64.136) by smtp.syd.people.net.au with SMTP; 21 Feb 2007 09:36:19 -0000 Received: from coltrane (coltrane [192.168.1.101]) by coltrane.mega-nerd.net (Postfix) with SMTP id E229F7B68 for ; Wed, 21 Feb 2007 20:36:03 +1100 (EST) Date: Wed, 21 Feb 2007 20:36:03 +1100 From: Erik de Castro Lopo To: caml-list@yquem.inria.fr Subject: Combinatorics in a functional way Message-Id: <20070221203603.e222647a.mle+ocaml@mega-nerd.com> Organization: Erik Conspiracy Secret Labs X-Mailer: Sylpheed version 2.3.0beta5 (GTK+ 2.8.20; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 45DC1288.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 permutations:01 cheers:01 spyware:98 adware:98 lazy:02 functional:02 functional:02 ugly:03 let:03 let:03 erik:04 erik:04 coding:05 i'm:09 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!