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: Re: [Caml-list] tail rec
On Sat, 2007-05-19 at 16:19 -0400, Jonathan Bryant 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).
> 
> A question I have about tail recursion in general:
> 
> let f x = (* Do something *)
> let g x = (* Do something *); f x
> let h x = (* Do something *); g x
> 
> Does the compiler optimize the calls (f x) and (g x) into tail  
> calls?  If so, how would the
> 
> let railrec f x = ...
> let tailrec g x = ...
> let tailrec h x = ...
> 
> syntax handle/help this? 

let tailrec would be a bad idea here because these
functions aren't recursive. However this form:

let g x = (* Do something *); tail f x

would assure you the call to f is tailed.

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