From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id A36CCBC0A for ; Sat, 19 May 2007 07:28:43 +0200 (CEST) Received: from kraid.nerim.net (smtp-106-saturday.nerim.net [62.4.16.106]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l4J5Shk8008640 for ; Sat, 19 May 2007 07:28:43 +0200 Received: from hector.lesours (ours.starynkevitch.net [213.41.244.95]) by kraid.nerim.net (Postfix) with ESMTP id CB2E640F45; Sat, 19 May 2007 07:28:40 +0200 (CEST) Received: from glinka.lesours ([192.168.0.1] ident=basile) by hector.lesours with esmtp (Exim 4.63) (envelope-from ) id 1HpHUv-0004Q2-RR; Sat, 19 May 2007 07:28:57 +0200 Message-ID: <464E8B1B.7080800@starynkevitch.net> Date: Sat, 19 May 2007 07:28:59 +0200 From: Basile STARYNKEVITCH User-Agent: Icedove 1.5.0.10 (X11/20070328) MIME-Version: 1.0 To: skaller Cc: caml-list@inria.fr Subject: Re: [Caml-list] tail rec References: <1179543365.26755.33.camel@rosella.wigram> In-Reply-To: <1179543365.26755.33.camel@rosella.wigram> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Miltered: at concorde with ID 464E8B0B.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; basile:01 basile:01 compiler:01 compiler:01 recursive:01 factorial:01 ocaml:01 emacs:01 camlp:01 faiencerie:01 92340:01 reine:01 wrote:01 rec:01 rec:01 skaller wrote: > I have a silly idea. Introduce a new construction: >=20 > let tailrec f ..=20 >=20 > This is the same as let rec except it checks every direct call to f > is in tail position (and bombs out the compiler if not). While I also want a similar construct or help, I believe your proposal=20 is wrong. What would be needed is a tailrec operator keyword that makes=20 the compiler check that a given call is in tail position. It is not a=20 function which is tail recursive, it is a call which is. In that way, the classical factorial does not have any tail call, so let rec fact n =3D if n <=3D 0 then 1 else n * tailrec fact (n - 1) ^^^^^^^ should give a warning, while the equivalent let fact n =3D let rec factloop n p =3D if n <=3D 0 then p else tailrec factloop (n - 1) (n * p) in factloop n 1 However, I have a more elegant "solution" (actually not a solution but a = proposal) : that tools like ocaml -dtypes and the emacs mode (or the=20 ocamlbrowser) flag (e.g. show in a different color) each tail-recursive=20 call. And yes, tail-recursion is syntactic, so all this could be a big camlp4=20 stuff. Regards. --=20 Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basilestarynkevitchnet mobile: +33 6 8501 2359 8, rue de la Fa=EFencerie, 92340 Bourg La Reine, France *** opinions {are only mines, sont seulement les miennes} ***