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 5586FBC69 for ; Wed, 21 Feb 2007 14:19:36 +0100 (CET) Received: from coriana6.cis.mcmaster.ca (coriana6.CIS.McMaster.CA [130.113.128.17]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l1LDJZIt013109 for ; Wed, 21 Feb 2007 14:19:35 +0100 Received: from Dura7.UTS.McMaster.CA (dura7.UTS.mcmaster.ca [130.113.196.62]) by coriana6.cis.mcmaster.ca (8.13.7/8.13.7) with ESMTP id l1LDJRh6029975 for ; Wed, 21 Feb 2007 08:19:32 -0500 (EST) Received: from cgpsrv2.cis.mcmaster.ca (univmail.CIS.McMaster.CA [130.113.64.46]) by Dura7.UTS.McMaster.CA (8.13.7/8.13.7) with ESMTP id l1LDJHKg007192 for ; Wed, 21 Feb 2007 08:19:17 -0500 Received: from [74.109.166.109] (account carette@univmail.cis.mcmaster.ca HELO [192.168.1.101]) by cgpsrv2.cis.mcmaster.ca (CommuniGate Pro SMTP 4.1.8) with ESMTP-TLS id 163159281 for caml-list@yquem.inria.fr; Wed, 21 Feb 2007 08:19:17 -0500 Message-ID: <45DC46DA.6030201@mcmaster.ca> Date: Wed, 21 Feb 2007 08:19:22 -0500 From: Jacques Carette Organization: McMaster University User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Combinatorics in a functional way References: <20070221203603.e222647a.mle+ocaml@mega-nerd.com> <20070221110607.GB15796@pulp.rsise.anu.edu.au> In-Reply-To: <20070221110607.GB15796@pulp.rsise.anu.edu.au> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Version-Mac: 4.7.1.128075, Antispam-Engine: 2.4.0.264935, Antispam-Data: 2007.2.21.45934 X-PerlMx-Spam: Gauge=IIIIIII, Probability=7%, Report='__CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0, __USER_AGENT 0' X-Miltered: at concorde with ID 45DC46E7.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; seq:01 seq:01 readable:01 struct:01 flatten:01 wrote:01 rec:01 rewrite:01 caml-list:01 modules:02 lazy:02 append:02 functional:02 mcmaster:02 module:03 And with the Seq module (left at end), combined with the pa_monad extention, one can rewrite find_comb as let find_comb p0 p1 = perform with Seq in i0 <-- range p0; i1 <-- range p0; i2 <-- range p1; guard (test i0 i1 i2); return (i0, i1, i2) I do believe that is more readable :-) Jacques Pietro Abate wrote: > I'm not sure this is exactly what you want... but I think it's a good > starting point to look at for this kind of problems. Making it lazy is > just a matter of changing the definition of the modules Seq. > > module Seq = > struct > let mzero = [] > let return a = [a] > let bind m f = List.flatten (List.map f m) > let mplus = List.append > let guard b = if b then return () else mzero > end > ;; > > let range n = > let rec aux i l = > if i = 0 then l else i::(aux (i-1) l) > in List.rev ( aux n [] ) > ;; > > let test _ _ _ = true ;; > > let find_comb p0 p1 = > Seq.bind (range p0) (fun i0 -> > Seq.bind (range p0) (fun i1 -> > Seq.bind (range p1) (fun i2 -> > Seq.bind (Seq.guard (test i0 i1 i2)) (fun _ -> > Seq.return (i0,i1,i2) > ) > ) > ) > ) > ;; > >