s-news
[Top] [All Lists]

[S] On defining functions within functions: a function might be invisibl

To: John Thaden <jjthaden@life.uams.edu>
Subject: [S] On defining functions within functions: a function might be invisible to itself
From: Jens Oehlschlaegel <oehl@Psyres-Stuttgart.DE>
Date: Thu, 25 Jun 1998 20:45:06 +0200 (MET DST)
Cc: S+ list <s-news@wubios.wustl.edu>
Reply-to: Jens Oehlschlaegel <oehl@Psyres-Stuttgart.DE>
Sender: owner-s-news@wubios.wustl.edu

# look at a function which does some (here nonsense) recalling
test <- function(x=1){
        if (x==1) test(2)
        else "OldTest"
}

> test()
[1] "OldTest"
# looks like a function which works, as long as you not rename it


# now define such a function within another function
test2 <- function(){
        test <- function(x=1){
                if (x==1) test(2)
                else "NewTest"
        }
        test()
}

> test2()
[1] "OldTest"
# silently returns "OldTest" instead of "NewTest"


# a good reason to use Recall()
test2 <- function(){
        test <- function(x=1){
                if (x==1) Recall(2)
                else "NewTest"
        }
        test()
}

> test2()
[1] "NewTest"

# Hats off to the programmer !

Best regards


--
Jens Oehlschlaegel-Akiyoshi
Psychologist/Statistician

CURRENT ADDRESS (until 12/98) | PERMANENT VIRTUAL ADDRESS
------------------------------------------------------------------------
                              | http://www.geocities.com/Tokyo/Bay/5663/
oehl@psyres-stuttgart.de      | oehl@geocities.com 
+49 711 6781-408 (phone)      |
+49 711 6876902    (fax)      |

Center for Psychotherapy Research |
Christian-Belser-Strasse 79a      |
D-70597 Stuttgart Germany         |

(general disclaimer)


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>
  • [S] On defining functions within functions: a function might be invisible to itself, Jens Oehlschlaegel <=