[R] dynamics of functions

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jun 5 18:15:52 CEST 2003


On Thu, 5 Jun 2003, Tobias Verbeke wrote:

...

> > 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)

That's an expression, not a function, and you would need some convention 
that it is x that should be varied.  You can do that: see curve() for 
example.  Or just use

iterate(function(x) -x^3, 3, 256)

Someone seems very adverse to using the spacebar!

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list