Re: [R] Recall() and sapply()

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Thu 31 Mar 2005 - 02:10:17 EST

On Wed, 30 Mar 2005, Robin Hankin wrote:

> Hi.
>
> I'm having difficulty following the advice given in help(Recall). Consider
> the two
> following toy functions:
>
>
> f1 <- function(n){
> if(length(n)>1){return(sapply(n,f1))}
> matrix(n,n,n)
> }
>
> f2 <- function(n){
> if(length(n)>1){return(sapply(n,Recall))}
> matrix(n,n,n)
> }
>
>
> f1() works as desired (that is, f(1:3), say, gives me a three element list
> whose i-th element
> is an i-by-i matrix whose elements are all i).
>
> But f2() doesn't.
>
> How do I modify either function to use Recall()? What exactly is Recall()
> calling here?
>

You can't use Recall here. I thought this was explicitly documented, but it turns out that it isn't, an omission I will fix.

You don't need Recall, because R can easily have recursive functions without it (unlike S)
- as you show in f1, the function can call itself - the problem with f1 is that it stops working if you change the name, but

   ?local shows how to get around this. This is probably the best way to    implement recursion.
- You can even implement Y, the "appplicative-order fixed point operator"

   to create anonymous recursive functions a la lambda calculus.

         -thomas



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Mar 31 02:23:44 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:57 EST