English version
Accueil     Ŕ propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis ŕ jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml ŕ l'adresse ocaml.org.

Browse thread
tail rec
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-05-19 (05:28)
From: Basile STARYNKEVITCH <basile@s...>
Subject: Re: [Caml-list] tail rec
skaller wrote:
> I have a silly idea. Introduce a new construction:
> let tailrec f .. 
> 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 
is wrong. What would be needed is a tailrec operator keyword that makes 
the compiler check that a given call is in tail position. It is not a 
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 = if n <= 0 then 1 else n * tailrec fact (n - 1)

should give a warning, while the equivalent

   let fact n =
      let rec factloop n p =
         if n <= 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 
ocamlbrowser) flag (e.g. show in a different color) each tail-recursive 

And yes, tail-recursion is syntactic, so all this could be a big camlp4 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faďencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***