Skip to content

return from nested function?

4 messages · Jan T. Kim, Duncan Murdoch, Seth Falcon

#
On Feb 25, 2005, at 12:34 PM, jhallman at frb.gov wrote:

            
This snippet may be of interest:


 > f = function(x) {
+ print("f")
+ g(return())
+ print("end of f")
+ }

 > g=function(x) {print("g")
+ x
+ print("end of g")
+ }

 > f(1)
[1] "f"
[1] "g"
NULL
#
On Tue, Mar 01, 2005 at 11:21:44PM -0800, Seth Falcon wrote:
I may be dumb today, but doesn't that beg the question of how does g
cause f not to return?

Best regards, Jan
#
On Wed, 2 Mar 2005 12:13:17 +0000, "Jan T. Kim" <jtk at cmp.uea.ac.uk>
wrote :
I'm not suggesting that I would ever use code like this, but if x is
never evaluated then the double return would not happen:
function(x) {
print("f")
g(x, return())
print("end of f")
}
function(x, blastoff) {
   print("g")
   if (x) blastoff
   print("end of g")
}
[1] "f"
[1] "g"
NULL
[1] "f"
[1] "g"
[1] "end of g"
[1] "end of f"

However, this is not a style of programming that I would expect to
last for more than 5 minutes after I published a program using it.
Follow Thomas's advice, and use tryCatch instead.

Duncan Murdoch
#
On Mar 2, 2005, at 4:13 AM, Jan T. Kim wrote:
No, I think my post doesn't really help you.  In the context of a 
recursive function, the code I posted provides a way to jump out of the 
recursion which is a cousin of what you are looking for.

I wonder if try or tryCatch might be what you want?

cheers,

+ seth