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 9CEC3BB86 for ; Mon, 17 Apr 2006 00:30:15 +0200 (CEST) Received: from out3.smtp.messagingengine.com (out3.smtp.messagingengine.com [66.111.4.27]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k3GMUEJI012840 for ; Mon, 17 Apr 2006 00:30:15 +0200 Received: from frontend2.internal (frontend2.internal [10.202.2.151]) by frontend1.messagingengine.com (Postfix) with ESMTP id 8DC43D4B6B7 for ; Sun, 16 Apr 2006 18:30:12 -0400 (EDT) Received: from frontend3.messagingengine.com ([10.202.2.152]) by frontend2.internal (MEProxy); Sun, 16 Apr 2006 18:29:43 -0400 X-Sasl-enc: Ak7bcTFhybGc31ln/Qt5YmiAFRZpUwhPoPD/fHNChD2h 1145226582 Received: from [172.16.13.150] (burnham.ljcrf.edu [192.231.106.2]) by frontend3.messagingengine.com (Postfix) with ESMTP id 9D732804C; Sun, 16 Apr 2006 18:29:41 -0400 (EDT) Date: Sun, 16 Apr 2006 15:27:06 -0700 (PDT) From: Martin Jambon X-X-Sender: martin@droopy To: Tato Thetza Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] recursion/iterator question In-Reply-To: <1145221898.16349.259200911@webmail.messagingengine.com> Message-ID: References: <1145221898.16349.259200911@webmail.messagingengine.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at nez-perce with ID 4442C576.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; recursion:01 iterator:01 uncommon:01 oandrieu:01 nerim:01 ocaml:01 rec:01 iter:01 iter:01 wrote:01 caml-list:01 caml-list:01 tail:01 tail:01 jambon: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 On Sun, 16 Apr 2006, 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? You can use list comprehensions. See http://oandrieu.nerim.net/ocaml/#pa_compr let triplets l = [+ (x, y, z) | x <- l | y <- l | z <- l | when x < y && y < z ];; That's not optimal, but it's pretty clear. > 2) is there a cleaner way to iterate over all triplets in a list? You can do that, it should perform better: let rec iter_full f = function [] -> () | x :: l -> f x l; iter_full f l let iter_tail iter f l = iter_full (fun x l -> iter (f x) l) l let iter_full3 f l = iter_tail (iter_tail iter_full) f l let iter3 f l = iter_full3 (fun x y z _ -> f x y z) l Martin -- Martin Jambon, PhD http://martin.jambon.free.fr Edit http://wikiomics.org, bioinformatics wiki