Displaying 4 results from an estimated 4 matches for "factorial_tr_manu".
Did you mean:
factorial_tr_manual
2018 Feb 27
2
Parallel assignments and goto
...nvironment ? either the local one, for <-, or an enclosing one, for <<- ? so I guess I am asking if there is a more low-level assignment operation I can get my hands on without diving into C?
factorial <- function(n, acc = 1) {
? ? if (n == 1) acc
? ? else factorial(n - 1, n * acc)
}
factorial_tr_manual <- function (n, acc = 1)
{
? ? repeat {
? ? ? ? if (n <= 1)
? ? ? ? ? ? return(acc)
? ? ? ? else {
? ? ? ? ? ? .tailr_n <- n - 1
? ? ? ? ? ? .tailr_acc <- acc * n
? ? ? ? ? ? n <- .tailr_n
? ? ? ? ? ? acc <- .tailr_acc
? ? ? ? ? ? next
? ? ? ? }
? ? }
}
factorial_tr_automatic_1...
2018 Feb 27
0
Parallel assignments and goto
...r an enclosing one,
> for <<- ? so I guess I am asking if there is a more low-level assignment
> operation I can get my hands on without diving into C?
>
>
> factorial <- function(n, acc = 1) {
> if (n == 1) acc
> else factorial(n - 1, n * acc)
> }
>
> factorial_tr_manual <- function (n, acc = 1)
> {
> repeat {
> if (n <= 1)
> return(acc)
> else {
> .tailr_n <- n - 1
> .tailr_acc <- acc * n
> n <- .tailr_n
> acc <- .tailr_acc
>...
2018 Feb 26
0
Parallel assignments and goto
...involving those local variables ? and assigning to local variables in general.
Consider again the factorial function and three different ways of implementing it using the tail recursion optimisation:
factorial <- function(n, acc = 1) {
? ? if (n == 1) acc
? ? else factorial(n - 1, n * acc)
}
factorial_tr_manual <- function (n, acc = 1)
{
? ? repeat {
? ? ? ? if (n <= 1)
? ? ? ? ? ? return(acc)
? ? ? ? else {
? ? ? ? ? ? .tailr_n <- n - 1
? ? ? ? ? ? .tailr_acc <- acc * n
? ? ? ? ? ? n <- .tailr_n
? ? ? ? ? ? acc <- .tailr_acc
? ? ? ? ? ? next
? ? ? ? }
? ? }
}
factorial_tr_automatic_1...
2018 Feb 11
4
Parallel assignments and goto
Hi guys,
I am working on some code for automatically translating recursive functions into looping functions to implemented tail-recursion optimisations. See https://github.com/mailund/tailr
As a toy-example, consider the factorial function
factorial <- function(n, acc = 1) {
if (n <= 1) acc
else factorial(n - 1, acc * n)
}
I can automatically translate this into the loop-version