[R] dynamics of functions

Ravi Varadhan rvaradha at jhsph.edu
Thu Jun 5 18:32:46 CEST 2003


Here is a recursive function.

iterate <- function(f, n, x){
 if(n==0) return(x)
 iterate(f, n-1, f(x))
}

iterate(function(x) 1/(1+x), 10, 1)

Best,
Ravi.

----- Original Message -----
From: Tobias Verbeke <tobias_verbeke at skynet.be>
Date: Thursday, June 5, 2003 12:04 pm
Subject: Re: [R] dynamics of functions

> Dear Thomas,  
> 
> > What about the following function?
> > 
> > iterate<-function(f,n,x){
> >   if(n==0) return(x)
> >   y<-x
> >   for(i in 1:n)y<-f(y)
> >   y
> > }
> > iterate(sqrt,3,256)
> 
> Thank you very much, this certainly helps.
> I'm still curious, though, to know how to
> write the expression of my function 
> immediately as the argument f.
> I can define it outside of the function
> > aap <- function(x) -x^3
> and use it as argument
> > iterate(aap,3,256),
> but I seem not to be clever enough
> to write a function that receives
> the following as input
> 
> > iterate(-x^3,3,256)
> 
> Thanks again,
> 
> Tobias
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>




More information about the R-help mailing list