This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

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 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
call.

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

Regards.
--
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} ***

```