[erlang-questions] tail-recursive functions

Richard O'Keefe <>
Thu Jan 29 23:53:06 CET 2009


On 30 Jan 2009, at 12:35 am, Vlad Dumitrescu wrote:

> Hi,
>
> Reading the new EEP 26 "Make andalso and orelse tail-recursive" (which
> I'm in favor for, btw) made me think of a side-effect: it will become
> less easy to see if a function is tail-recursive or not.
> I mean, one has to actively look at the operator and decide "it's
> andalso, it's tail-recursive" or "it's and, it's not tail-recursive".
> In addition, there were discussions about making other constructs
> tail-recursive too.

Well, there's an improvement to Erlang that would fix this:
eliminate the 'and' and 'or' operators entirely.  This would
also eliminate the possibility of someone accidentally using
the "unsafe" version.  Quoting the text of the SML Basis Library:
  "the language defines the special operators andalso and orelse,
   which provide short-circuit evaluation of the AND and OR of two
   boolean expressions.  The semantics of strict AND and OR operators,
   which would evaluate both expressions before applying the operator,
 >>are rarely needed
   and can easily be obtained using the andalso and orelse operators."

In a strict language, 'andalso' and 'orelse' aren't operators;
they are control structures, every bit as much so as 'if' or 'case'.
(In Haskell, I grant you, '&&' and '||' are definable as ordinary
  functions.  But then except for syntax, so is 'if'.)

Many functions aren't intended to be tail recursive; perhaps any
check should be confined to functions that are explicitly declared so.





More information about the erlang-questions mailing list