Floyd Everest
Floyd Everest

Reputation: 288

Self-referencing nested functions in R?

So, I'm trying to write a function that builds a large complicated formula recursively. Basically, what I would love to work simply, is the following:

f <- function(x) {
  g <- function(y) y
  for( i in 1:4 ) {
    h <- g
    g <- function(y) h(y)^2
  }
  g(x)
}

Please refrain from laughing at this insane motivation. Now what I would like to get, is a function that returns ((((x^2)^2)^2)^2), but what actually happens is that my runtime just crashes immediately, probably because there's some sort of call to an unreferenced function or something, since I'm overwriting the expression for g every time (obviously I don't really know how r works in this scenario).

How can I achieve this idea of retaining the information from the older g references?

Upvotes: 4

Views: 169

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270348

1) Recursion We can use recursion like this:

h1 <- function(f, n) if (n == 1) f else function(x) f(h1(f, n-1)(x))

# test using g from questioun
h1(g, 4)(3)
## [1] 43046721

(((3^2)^2)^2)^2
## [1] 43046721

2) Reduce This uses Reduce to compose a function f with itself iteratively n times.

h2 <- function(f, n) function(y) Reduce(function(x, f) f(x), rep(list(f), n), y)

h2(g, 4)(3)
## [1] 43046721

3) for

h3 <- function(f, n) {
  function(x) {
   for(i in 1:n) x <- f(x)
   x
  }
}

h3(g, 4)(3)
## [1] 43046721

4) Fixed If there are a small fixed number we could just write it out explicitly:

h4 <- function(x) g(g(g(g(x))))

h4(3)
## [1] 43046721

5) Compose We could slightly simplify any of the above using Compose from the functional package. (The purrr package also has a compose function. Use that if you are already using purrr; otherwise, functional has a smaller footprint.)

library(functional)

h1a <- function(f, n) if (n == 1) f else Compose(f, h(f, n-1))

h2a <- function(f, n) Reduce(Compose, rep(list(f), n))

h2b <- function(f, n) do.call(Compose, rep(list(f), n))

h3a <- function(f, n) {
  for(i in 1:n) ff <- if (i == 1) f else Compose(ff, f)
  ff
}

h4a <- Compose(g, g, g, g)

Upvotes: 5

Related Questions