Version française
Home     About     Download     Resources     Contact us    

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

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 (02:56)
From: skaller <skaller@u...>
Subject: tail rec
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).

[Maybe this can be done with camlp4, though I think it might
be hard?]

I think this could be useful for newbies learning functional
programming, and perhaps even experts looking for performance

If a let closure of f is made, a warning is issued, eg:

let tailrec f x = 
  let g x = f x in (* WARNING *)
  1 + g (x - 1) (* not tail, but not explicit call of f *)

The annotation does NOT prove a function is tail rec,
but if the compiler bombs, it proves the function is
not tail rec.


John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: