Skip to content
Prev 295298 / 398502 Next

Creating functions with a loop.

Interpreting your question slightly differently, where ff.k(x) is a
function call.

Suppose you want to get a list of functions like

x^2
x^2 -1
x^2 - 3
x^2 - 6

It's perfectly possible with something like this:

NextFunc <- function(f, i) {
   # Takes in a function f and returns
   # a different function that gives you
   # f(x) - i

   force(i) # probably unnecessary, but good practice as this just
might bite you in a loop


  function(x) f(x) - i
}

Then

f <- function(x) x^2

f2 <- NextFunc(f, 1)

f3 <- NextFunc(f2, 2)

f3(3) # 6 = 3^2 - 1 - 2

Which should be wrappable in a loop.

Michael

On Tue, May 22, 2012 at 11:08 AM, David Winsemius
<dwinsemius at comcast.net> wrote: