Description Usage Arguments Value Examples
If you can specify your recursive function such that the
recursive call is in 'tail position' (that is, the very
last operation in your function), you can take advantage
of tail call optimization. Just wrap your recursive call
in trm_tailcall()
1 | trm_tailcall(x)
|
x |
A recursive call within generator fed to |
x
with added class attribute 'trampoline_tailcall'
1 2 3 4 5 6 7 8 9 | trampoline(factorial(13),
factorial = function(n, x = 1) {
force(x) ## necessary thanks to R's lazy evaluation
if(n <= 1) {
return(trm_return(x))
}
val <- trm_tailcall(factorial(n - 1, x * n))
return(val)
})
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.