[R] dynamics of functions

Tobias Verbeke tobias_verbeke at skynet.be
Thu Jun 5 18:04:23 CEST 2003


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




More information about the R-help mailing list