[erlang-questions] Idiomatic Erlang, style/performance question

Lev Walkin vlm@REDACTED
Fri Jun 20 10:35:25 CEST 2008


Dominic Williams wrote:
> Hello,
> 
> ROK wrote:
>> I prefer the term "Tail-Call Optimisation" because it is more accurate.
>> The idea (as explained in "Lambda - the Ultimate Goto") is that if the
>> dynamically last thing a function does (in any language) is to return
>> the result of another function call, then there is no point in keeping
>> the current stack frame.  You might as well arrange for the new
>> function to return directly to the original caller.  Think of *all*
>> tail calls as "argument binding + GOTO".
> 
> Is the optimisation possible even when the tail call is not to the same
> function?

Yes.

> E.g.:
> 
>>> power(N, P) when is_integer(P), P >= 0 ->
>>>         ipower(N, P, 1);
>>>     power(N, P) when is_integer(P), P < 0 ->
>>>         1/ipower(N, -P, 1).
>> The first call here is a tail call, the second is not.
> 
> As I wrote in a previous post, I thought that there was no optimisation
> here, because it is not a /recursive/ tail-call. Am I wrong? What is the
> case for Erlang, in particular?

Erlang optimizes tail calls, not necessarily recursive ones.


-- 
Lev Walkin
vlm@REDACTED



More information about the erlang-questions mailing list