From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA31460; Tue, 17 Aug 2004 17:17:05 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA30864 for ; Tue, 17 Aug 2004 17:17:04 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from m14s26.vip-server.net (cray.gaillourdet.net [80.190.204.60]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i7HFH3mL012940 for ; Tue, 17 Aug 2004 17:17:03 +0200 Received: from dijkstra.gaillourdet.net (m14s26.vip-server.net [80.190.204.60]) by m14s26.vip-server.net (Postfix) with ESMTP id BFCCA94D6C for ; Tue, 17 Aug 2004 17:15:16 +0200 (CEST) Received: by dijkstra.gaillourdet.net (Postfix, from userid 1000) id 79F38544E7; Tue, 17 Aug 2004 17:17:02 +0200 (CEST) From: Jean-Marie Gaillourdert To: caml-list@inria.fr Subject: Re: [Caml-list] Are map and iter guaranteed to be called in forwards order? Date: Tue, 17 Aug 2004 17:17:01 +0200 User-Agent: KMail/1.6.2 References: <20040817120053.GA9749@annexia.org> <20040817145653.GA12345@annexia.org> In-Reply-To: <20040817145653.GA12345@annexia.org> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200408171717.01865.jmg@gaillourdet.net> X-Miltered: at nez-perce with ID 4122216F.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; jean-marie:99 caml-list:01 2004:99 'g':01 val:01 char:01 'g':01 val:01 incr:01 char:01 jean-marie:99 int:01 int:01 schrieb:01 it'd:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi, Am Dienstag, 17. August 2004 16:56 schrieb Richard Jones: > This came up because I wanted a sensible way to number a list of > items. The obvious imperative approach is: > > # let items = ['a';'c';'e';'g';'i'];; > val items : char list = ['a'; 'c'; 'e'; 'g'; 'i'] > # let i = ref 0;; > val i : int ref = {contents = 0} > # let items = List.map (fun item -> let n = !i in incr i; n, item) > items;; val items : (int * char) list = > [(0, 'a'); (1, 'c'); (2, 'e'); (3, 'g'); (4, 'i')] > > The functional approach is comparatively long-winded: you have to > effectively write your own loop explicitly, and the obvious way to > write it isn't tail recursive, so you have to do it with accumulators. > > It'd be nicer to have a library HOF to do this. There is a higher order function to do this: fold List.rev (List.fold_left (fun xs x -> match xs with [] -> [(0,x)] | (i,_)::_ -> (i+1,x)::xs) [] items);; Regards, Jean-Marie ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners