[R] detecting entry into a recursive function
    Jim Lemon 
    jim at bitwrit.com.au
       
    Mon Feb 18 02:51:25 CET 2013
    
    
  
On 02/18/2013 02:03 AM, Benjamin Tyner wrote:
> Thanks Jim -- I had considered this approach; is there any way to "hide"
> such arguments from users?
>
Hi Ben,
I played around with your solution for a while and if the first argument 
to the function changes with each recursive call, you may be able to do 
it like this:
factorial<-function(x) {
  cat(x,"\n")
  if(as.character(x) == as.character(sys.call())[2])
   cat("I\'m the first!\n")
  if(x>1) x<-x*factorial(x-1)
  return(x)
}
factorial(3)
factorial(4)
...
Jim
    
    
More information about the R-help
mailing list