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 p8TJ4ZTg013384 for ; Thu, 29 Sep 2011 21:04:38 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ah8BAMTAhE5RZ90xkWdsb2JhbABBqBQUAQEBAQkLCwcUAyKBUwEBAQMBJxMzHAIBCBgKFBAyJQIEG4dyAroOhjJhBJNWkTo X-IronPort-AV: E=Sophos;i="4.68,462,1312149600"; d="scan'208";a="122190357" Received: from mtaout03-winn.ispmail.ntl.com ([81.103.221.49]) by mail1-smtp-roc.national.inria.fr with ESMTP; 29 Sep 2011 21:04:38 +0200 Received: from aamtaout02-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout03-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20110929190437.XLGV8898.mtaout03-winn.ispmail.ntl.com@aamtaout02-winn.ispmail.ntl.com> for ; Thu, 29 Sep 2011 20:04:37 +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 <20110929190437.OFJ5924.aamtaout02-winn.ispmail.ntl.com@romulus.metastack.com> for ; Thu, 29 Sep 2011 20:04:37 +0100 Received: from remus.metastack.local ([172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id p8TJ4Xwx009609 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL) for ; Thu, 29 Sep 2011 20:04:34 +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 20:04:33 +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///0NQCAAEqKQA== Date: Thu, 29 Sep 2011 19:04:31 +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: [212.183.140.35] 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=JvdXmxIgLJv2/GthKqHpGJEEHukvLcvELVXUanXFreg= c=1 sm=0 a=h3xG40aIwaUA:10 a=IIGt3dPmmM4A:10 a=cTs9vV391PwA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=1grv6VYyXK-z2JsdpwAA: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 p8TJ4ZTg013384 Subject: RE: [Caml-list] still puzzled on generic types Walter Cazzola wrote: > On Thu, 29 Sep 2011, David Allsopp wrote: > > > I think it's probably something to do with optional arguments not > > behaving as you expect. > > uhm, would be interesting to see what is wrong on my use of the optional > arguments. Separately answered... > > But this is a bad use for optional arguments - you should instead use > > a nested function to pass the accumulator values. This works fine: > > yes my first attempt was implemented with nested functions but ,,, > > > 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 > > ... my nested function had a different name than the nextee function and I > was passing l to it too. Could you explain me why your code works? in > particular where does it take the list to enumerate? Silly question I know > but it seems magic to me written in such a way OCaml allows a name to hide a previous use of a name in the scope chain. The closest name moving up the "let .. in .." chains will always be the one used. Very occasionally that can cause some weird bugs but as the types will often differ, it's a clearer way of writing (rather than inventing arbitrary new names or using lots of prime symbols). I defined the innermost [enumerate] using the [function] keyword. You could equivalently write: let rec enumerate acc n l = match l with h::ls -> ... but the function keyword gives you that for free (it's the keyword for introducing an anonymous function with match and simply gives you a match over a single anonymous parameter). > > 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. > > Ok, thanks for the note, but efficiency issues are still far in my > learning curve ;-) Learning not to concatenate lists is part of learning tail recursion - it should be higher up your list ;o) HTH, David