rrecursionfunctional-programmingtail-recursion

Tail recursion on R Statistical Environment


Does R support proper tail recursion and where can I find documentation about this?


Solution

  • It's quite easy to find out that R does not support tail recursion optimization:

    f <- function(n) {
    if (n != 0) f(n-1)
    }
    f(100000)
    # Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
    

    Had tail calls been optimized to jumps, then this function would have terminated without problems.