Skip to content
Back to formatted view

Raw Message

Message-ID: <5121891D.5060601@bitwrit.com.au>
Date: 2013-02-18T01:51:25Z
From: Jim Lemon
Subject: detecting entry into a recursive function
In-Reply-To: <5120F13D.4050002@gmail.com>

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