From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8TFGUlB006346 for ; Thu, 29 Sep 2011 17:16:30 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiMBANyKhE5RZ90wkWdsb2JhbABBqA8UAQEBAQkLCwcUAyKBUwEBAQEDJxNPAgEIGAoUEDIlAgQbh3S4UoYyYQSTVpE6 X-IronPort-AV: E=Sophos;i="4.68,461,1312149600"; d="scan'208";a="122166735" Received: from mtaout02-winn.ispmail.ntl.com ([81.103.221.48]) by mail1-smtp-roc.national.inria.fr with ESMTP; 29 Sep 2011 17:16:25 +0200 Received: from aamtaout02-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout02-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20110929151623.LRJC17426.mtaout02-winn.ispmail.ntl.com@aamtaout02-winn.ispmail.ntl.com> for ; Thu, 29 Sep 2011 16:16:23 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout02-winn.ispmail.ntl.com (InterMail vG.3.00.04.00 201-2196-133-20080908) with ESMTP id <20110929151623.YKLO5924.aamtaout02-winn.ispmail.ntl.com@romulus.metastack.com> for ; Thu, 29 Sep 2011 16:16:23 +0100 Received: from remus.metastack.local ([172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id p8TFGKS1008444 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL) for ; Thu, 29 Sep 2011 16:16:20 +0100 Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%10]) with mapi id 14.01.0323.003; Thu, 29 Sep 2011 16:16:20 +0100 From: David Allsopp To: "OCaml List (caml-list@inria.fr)" Thread-Topic: [Caml-list] still puzzled on generic types Thread-Index: AQHMfrmfVHFjW7+SMESrq+opltUASZVkdpnw Date: Thu, 29 Sep 2011 15:16:19 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [195.12.30.30] Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=R50lirqlHffDPPkwUlkuVa99MrvKdVWo//yz83qex8g= c=1 sm=0 a=TSKAR9cOtZYA:10 a=IIGt3dPmmM4A:10 a=cTs9vV391PwA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=gztJFahblne56aWvDsgA:9 a=CjuIK1q_8ugA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p8TFGUlB006346 Subject: RE: [Caml-list] still puzzled on generic types Walter Cazzola wrote: > Dear all, > waiting for some hints on my other issue for functors and generic I was > playing with an enumerate function (with tail recursion): > > let rec enumerate ?(l':((int * 'a) list)=[]) ?(n=0) l = > match l with > h::l1 -> enumerate (l'@[(n,h)]) (n+1) l1 > | [] -> l' > > this should just take a list and return a list of couple whose first entry > is the position in the list of the element; to be clearer: > > enumerate ['a'; 'b'; 'c'] -> [(0,'a'); (1,'b'); (2,'c')]; > > but this doesn't work as by type mismatch: > > Error: This expression has type (int * 'a) list > but an expression was expected of type 'a list I think it's probably something to do with optional arguments not behaving as you expect. But this is a bad use for optional arguments - you should instead use a nested function to pass the accumulator values. This works fine: let enumerate l = let rec enumerate acc n = function h::ls -> enumerate ((n, h)::acc) (n + 1) ls | [] -> List.rev acc in enumerate [] 0 l Concatenating lists is also expensive in terms of the left list so your function is about as slow as possible! Much better when aiming for tail recursion, accumulate a reversed list and then reverse it in the basis case. David