From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 49B70BB86 for ; Mon, 17 Apr 2006 00:00:19 +0200 (CEST) Received: from mail1.grayskies.net (wrath.grayskies.net [151.202.218.61]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k3GM0IHM010637 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 17 Apr 2006 00:00:19 +0200 Received: from [192.168.0.112] by mail1.grayskies.net with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.60 (FreeBSD)) (envelope-from ) id 1FVFI0-00052O-RJ; Sun, 16 Apr 2006 18:00:16 -0400 Message-ID: <4442BE6B.7000003@grayskies.net> Date: Sun, 16 Apr 2006 18:00:11 -0400 From: David Powers User-Agent: Mail/News 1.5.0.2 (Macintosh/20060310) MIME-Version: 1.0 To: Tato Thetza Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] recursion/iterator question References: <1145221898.16349.259200911@webmail.messagingengine.com> In-Reply-To: <1145221898.16349.259200911@webmail.messagingengine.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4442BE72.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; recursion:01 iterator:01 iter:01 iter:01 lazy:01 libref:01 lazy:01 uncommon:01 beginner's:01 ocaml:01 beginners:01 bug:01 wrote:01 caml-list:01 caml-list:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 The best way would probably be to write a permutation function that returns a list of triplets given a list, and then to use List.iter and a local function to split out each triplet to work on the component parts... something like: let for_each_trip lst = let real_work (a, b, c) = (* do some stuff with a b and c in here *) in List.iter real_work (permute_triples lst) (loose code - forgive my laziness) This could be made more general by improving the permute code to take a number option and to return triplets of that number (permute 3 lst). In addition you could make the internal function (real_work) be a parameter of the for_each... which would reduce all of this to the more general List.iter any_function_I_want (permute 3 lst) Finally, you might want to consider using lazy evaluation in your permute function to avoid calculating every permutation up front. See http://caml.inria.fr/pub/docs/manual-ocaml/libref/Lazy.html for more on that. -David Tato Thetza wrote: > Hi caml-list > Given a list, I would like to iterate over all triplets in the list. For > example, in mathematcs, its not uncommon to have expressions such as > "for all i,j,k in set X, do f(i,j,k)" > > The only way I can think of is to create a list with all triplets of the > list, so: > triplets([1,2,3,4]) = [(1,2,3),(1,2,4),(1,3,4),(2,3,4)] > and take this list and map a function f to it. > > questions: > 1) what would be the best way to write triplets? > 2) is there a cleaner way to iterate over all triplets in a list? > > please excuse my english > Tato T. > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >