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 BAFA5BB84 for ; Mon, 17 Apr 2006 05:12:02 +0200 (CEST) Received: from pih-relay04.plus.net (pih-relay04.plus.net [212.159.14.131]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k3H3C26r031743 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Mon, 17 Apr 2006 05:12:02 +0200 Received: from [80.229.56.224] (helo=chetara) by pih-relay04.plus.net with esmtp (Exim) id 1FVK9h-0001Uy-Me for caml-list@yquem.inria.fr; Mon, 17 Apr 2006 04:12:01 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] recursion/iterator question Date: Mon, 17 Apr 2006 01:06:22 +0100 User-Agent: KMail/1.9.1 References: <1145221898.16349.259200911@webmail.messagingengine.com> In-Reply-To: <1145221898.16349.259200911@webmail.messagingengine.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200604170106.23660.jon@ffconsultancy.com> X-Miltered: at nez-perce with ID 44430782.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; recursion:01 iterator:01 uncommon:01 pairs:01 rec:01 val:01 rec:01 val:01 ocaml:01 2006:98 frog:98 wrote:01 caml-list:01 caml-list:01 int: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 Sunday 16 April 2006 22:11, 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? As 3-tuples, as you have done. > 2) is there a cleaner way to iterate over all triplets in a list? I think you mean _tabulate_ all triplets _from_ a list. You can write a function to tabulate all pairs like this: # let rec f2 = function [] -> [] | a::t -> List.map (fun b -> a, b) t @ f2 t;; val f2 : 'a list -> ('a * 'a) list = and then another to tabulate all triplets like this: # let rec f3 = function [] -> [] | a::t -> List.map (fun (b, c) -> a, b, c) (f2 t) @ f3 t;; val f3 : 'a list -> ('a * 'a * 'a) list = On the example you gave: # f3 [1;2;3;4];; - : (int * int * int) list = [(1, 2, 3); (1, 2, 4); (1, 3, 4); (2, 3, 4)] -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists