jacob navia wrote:
Richard Tobin wrote:
>In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>>Whenever the last action of the routine is to call itself with
revised parameters, this can obviously be replaced by a loop.
Not necessarily. For example, consider the case where the address
of a local variable is passed.
And of course self-call is just the simplest example of tail call.
-- Richard
Why would it matter?
suppose that as the last action you make
return sameproc(2,3,&local);
Since the value of "local" can't ever be used after
the call returns, it doesn't matter at all.
I'm afraid you are mistaken, since the address of `local` can be used
/inside the call of `sameproc`/. Hence it's important that `local`
continue to exist, hence (in the usual implementation) that the
stack frame it's in continues to exist, hence you can't do the
straightforward stack-frame elimination part of tail-call optimisation.
Consider this horrible sketch of an example:
Answer example( Args x, struct Ex *uplink )
{
if (terminationCondition( x ))
{
return dependsOnUplinkChainAndX( x, uplink );
}
else
{
struct Ex another;
another.uplink = uplink;
another.stuff = hackeryOn( x );
return example( shrink( x ), &another );
}
}
The recursive call can't be TCOd; the number of `another` elements
needed depends on how much `x` needs to be shrunk before meeting
the termination condition, so you can't junk the stack frames.
--
Chris "tails can't speak, never mind /call/." Dollin
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England