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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 2746EBB84 for ; Mon, 17 Apr 2006 08:50:05 +0200 (CEST) Received: from rouge.crans.org (rouge.crans.org [138.231.136.3]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k3H6o422002375 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Mon, 17 Apr 2006 08:50:04 +0200 Received: from localhost (rouge.adm.crans.org [138.231.144.3]) by rouge.crans.org (Postfix) with ESMTP id 3D9B3BC02; Mon, 17 Apr 2006 08:50:04 +0200 (CEST) Received: from rouge.crans.org ([138.231.144.3]) by localhost (rouge [138.231.144.3]) (amavisd-new, port 10024) with LMTP id 26090-01-2; Mon, 17 Apr 2006 08:50:04 +0200 (CEST) Received: from [138.231.150.106] (sayan-ftp.wifi.crans.org [138.231.150.106]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by rouge.crans.org (Postfix) with ESMTP id 1B5BEBC00; Mon, 17 Apr 2006 08:50:03 +0200 (CEST) Message-ID: <44433AA3.9080908@crans.org> Date: Mon, 17 Apr 2006 08:50:11 +0200 From: =?ISO-8859-1?Q?Li-Thiao-T=E9_S=E9bastien?= User-Agent: Mail/News 1.5 (X11/20060228) 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: 8bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at crans.org X-Miltered: at concorde with ID 44433A9C.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; recursion:01 iterator:01 uncommon:01 wrote:01 caml-list:01 explicitly:01 functional:02 let:03 let:03 expressions:04 sebastien:05 sebastien:05 iterate:06 iterate:06 implementing:06 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 Tato Thetza wrote: > 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? > Hi, I suggest turning the list into an array, then using for-loops instead of trying to explicitly generate the list of all possible triplets. If you need to accumulate the results, you can use a list ref. This amounts to implementing a set using an array instead of a list; unfortunately it is not functional :( let iterate f l = let t = Array.of_list l in let n = Array.length t in let results = ref [] in for i = 0 to n-1 do for j = 0 to n-1 do for k = 0 to n-1 do results := f (t.(i),t.(j),t.(k)) :: results done done done ;; -- Li-Thiao-Té Sébastien