# R-beta: a recursive anonymous function

Douglas Bates (bates@stat.wisc.edu)
Mon, 28 Apr 1997 08:28:35 -0500 (CDT)

```Message-Id: <m0wLqUB-000hhBC@franz.stat.wisc.edu>
Date: Mon, 28 Apr 1997 08:28:35 -0500 (CDT)
From: Douglas Bates <bates@stat.wisc.edu>
To: r-help@stat.math.ethz.ch
Subject: R-beta: a recursive anonymous function

This was either an attempt to get an early lead in some future
obfuscating R contest or a way of getting around the different scoping
rules of R and S.

I attempted to create a recursive anonymous function to be called
within another function.  You may want to stop reading for a bit and
consider how that would be done.  That is, how do you recursively call
a function that has never been assigned a name?

OK, you're back.  You probably came up with a better solution than I
did but I used (sys.function())(arg) to do the recursion.  The piece
of code looks like

flist <- (function(x) {
if (mode(x) == "call") {
if (x[[1]] == as.name("/"))
return(c(sys.function()(x[[2]]), sys.function()(x[[3]])))
if (x[[1]] == as.name("(")) 	# for R
return(sys.function()(x[[2]]))
}
if (mode(x) == "(") return(sys.function()(x[[2]])) # for S
list(x)
})(getGroupsFormula(data, form, ...)[[2]])
## I know it's horribly obscure.  Blame Bill Venables for teaching me this.

Regretably, it doesn't work in R.  Using the debugger one finds that
sys.function() returns the function being called the first time
through but the second time through it returns NULL.  Is this a bug or
a feature?
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
```