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

Dominic Williams erlang@REDACTED
Fri Jun 20 10:16:08 CEST 2008


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?

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?

Thanks,

Dominic Williams
http://dominicwilliams.net




More information about the erlang-questions mailing list