Re: [Rd] Tail Call Elimination?

From: <>
Date: Mon, 18 Apr 2011 09:41:58 -0500

The premise of your post is false: contrary to popular belief, R's looping constructs are not particularly inefficient. Slowness of loops relative to vectorized code comes from the cost of interpreting the body of the loop. That exact same interpreter would be used to interpret the bodies of functions used to express loops using recursion, so it is not reasonable to expect this to improve performance. In fact, due to the inefficiency of the current function call mechanism in R, quite the opposite is true.

As to the question: tail call optimization cannot be applied in R, at least not in a simple way, because the semantics of R provide access to the call stack via the functions and parent.frame and such. It might be possible to make some semantic changes, such as only guaranteeing access to the immediate caller, but there isn't much point unless/until the performance of the function calling mechanism is improved.



On Sun, 17 Apr 2011, Mohit Dayal wrote:

> Dear R-programmers,
> I am trying to program a Newton-Raphson in R (yes, i will try GSL, not right
> now), and it would be a real boon if R had tail call elimination, so that a
> recursive program has a guarantee not to fail due to stack overflows, given
> how slow loops in R are. I did look at the documentation, but could not find
> a reason for it.
> Regards,
> Mohit Dayal
> Researcher
> Applied Statistics & Computing Lab
> [[alternative HTML version deleted]]
> ______________________________________________
> mailing list

Luke Tierney
Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:
Iowa City, IA 52242                 WWW:

______________________________________________ mailing list
Received on Mon 18 Apr 2011 - 14:46:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Apr 2011 - 16:00:47 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive