Recall for parent

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Recall for parent

Paul Roebuck
What's the best way to simulate Recall for parent function?
Consider this one-time recursive code:

alwaysEven <- function(x) {
    handleOdd <- function(x) {
        alwaysEven(x-1)    # use Recall-like here
    }

    return(if (x %% 2) handleOdd(x) else x)
}
any2even <- alwaysEven
rm(alwaysEven)
any2even(3)

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Recall for parent

Gabor Grothendieck
Here are two ways:


# method 1. nest an extra alwaysEven
alwaysEven <- function(x) {
 alwaysEven <- function(x) {
   handleOdd <- function(x) {
       alwaysEven(x-1)    # use Recall-like here
   }

   return(if (x %% 2) handleOdd(x) else x)
 }
 alwaysEven(x)
}
any2even <- alwaysEven
rm(alwaysEven)
any2even(3)


# method 2.  sys.function

alwaysEven <- function(x) {
   handleOdd <- function(x) {
       sys.function(1)(x-1)    # use Recall-like here
   }

   return(if (x %% 2) handleOdd(x) else x)
}
any2even <- alwaysEven
rm(alwaysEven)
any2even(3)



On 3/29/06, Paul Roebuck <[hidden email]> wrote:

> What's the best way to simulate Recall for parent function?
> Consider this one-time recursive code:
>
> alwaysEven <- function(x) {
>    handleOdd <- function(x) {
>        alwaysEven(x-1)    # use Recall-like here
>    }
>
>    return(if (x %% 2) handleOdd(x) else x)
> }
> any2even <- alwaysEven
> rm(alwaysEven)
> any2even(3)
>
> ----------------------------------------------------------
> SIGSIG -- signature too long (core dumped)
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Recall for parent

Thomas Lumley
In reply to this post by Paul Roebuck
On Wed, 29 Mar 2006, Paul Roebuck wrote:

> What's the best way to simulate Recall for parent function?
> Consider this one-time recursive code:
>
> alwaysEven <- function(x) {
>    handleOdd <- function(x) {
>        alwaysEven(x-1)    # use Recall-like here
>    }
>
>    return(if (x %% 2) handleOdd(x) else x)
> }
> any2even <- alwaysEven
> rm(alwaysEven)
> any2even(3)

You can make anonymous recursive functions with local()

whatever <- local({
               alwaysEven <- function(x) {
                  handleOdd <- function(x) {
                     alwaysEven(x-1)
               }
               return(if (x %% 2) handleOdd(x) else x)
               }
           alwaysEven
           })


  -thomas

Thomas Lumley Assoc. Professor, Biostatistics
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Recall for parent

Gabor Grothendieck
On 3/30/06, Thomas Lumley <[hidden email]> wrote:

> On Wed, 29 Mar 2006, Paul Roebuck wrote:
>
> > What's the best way to simulate Recall for parent function?
> > Consider this one-time recursive code:
> >
> > alwaysEven <- function(x) {
> >    handleOdd <- function(x) {
> >        alwaysEven(x-1)    # use Recall-like here
> >    }
> >
> >    return(if (x %% 2) handleOdd(x) else x)
> > }
> > any2even <- alwaysEven
> > rm(alwaysEven)
> > any2even(3)
>
> You can make anonymous recursive functions with local()
>
> whatever <- local({
>               alwaysEven <- function(x) {
>                  handleOdd <- function(x) {
>                     alwaysEven(x-1)
>               }
>               return(if (x %% 2) handleOdd(x) else x)
>               }
>           alwaysEven
>           })
>

Or perhaps this variation which separates the definition and the call:

alwaysEven <- local(alwaysEven <- function(x) {
                  handleOdd <- function(x) {
                     alwaysEven(x-1)
               }
               return(if (x %% 2) handleOdd(x) else x)
               }
)
alwaysEven2 <- alwaysEven
rm(alwaysEven)
alwaysEven2(3)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html