Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
issues.

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.

Comments?

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net