From: Mark Kimpel <mwkimpel_at_gmail.com>

Date: Sat, 08 Nov 2008 17:09:25 -0500

*>
*

*>
*

*> Y <- function(f) {
*

*>
*

> csum <- function(f) function(n) {

*>
*

*> recurs.time <- matrix(0, ncol = 3, nrow = 100)
*

*> Y.time <- matrix(0, ncol = 3, nrow = 100)
*

*>
*

*> for (i in 1:100) recurs.time[i,] <- unclass(system.time(a <- s(996)))[1:3]
*

*> ave.recurs.time <- colSums(recurs.time)
*

*> ave.recurs.time
*

[1] 0.356 0.004 0.355

*>
*

*> for (i in 1:100) Y.time[i,] <- unclass(system.time(b <- Y
*

(csum)(996)))[1:3]

*> ave.Y.time <- colSums(Y.time)
*

*> ave.Y.time
*

[1] 0.652 0.000 0.640

*>
*

*> u <- s(1000)
*

*> u
*

[1] 500500

*> v <- Y (csum)(1000)
*

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

*> v
*

Error: object "v" not found

No suitable frames for recover()

Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry Indiana University School of Medicine

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 08 Nov 2008 - 22:11:15 GMT

Date: Sat, 08 Nov 2008 17:09:25 -0500

I found the article the "Y of R" in the latest R news to be very
interesting. It is certainly challenging me to learn more about how R works
"under the hood" as the author states. What is less clear to me is whether
this approach is primarily for teaching purposes or has a real world
application. What is meant by "fragility of reliance on the function
name defined as a global variable" as a downside to the classical recursive
formulation of function "s"? How can that impact the average R programmer?

Given that it is slower, is more cumbersome to write, and has a lower nesting limit than the classical approach, I wonder about its utility for the average programmer (or somewhat below average programmer like me).

Okay, here's my code, output, and sessionInfo()

s <- function(n) {

if (n == 1) return(1)

return(s(n-1)+n)

}

Y <- function(f) {

g <- function(h) function(x) f(h(h))(x)
g(g)

}

csum <- function(f) function(n) {

if (n < 2) return(1);

return(n+f(n-1))

}

recurs.time <- matrix(0, ncol = 3, nrow = 100) Y.time <- matrix(0, ncol = 3, nrow = 100)

for (i in 1:100) recurs.time[i,] <- unclass(system.time(a <- s(996)))[1:3]
ave.recurs.time <- colSums(recurs.time)

ave.recurs.time

for (i in 1:100) Y.time[i,] <- unclass(system.time(b <- Y (csum)(996)))[1:3]
ave.Y.time <- colSums(Y.time)

ave.Y.time

u <- s(1000)

u

v <- Y (csum)(1000)

v

sessionInfo()

*> s <- function(n) {
*

+ if (n == 1) return(1) + return(s(n-1)+n) + }

+ g <- function(h) function(x) f(h(h))(x) + g(g) + }

> csum <- function(f) function(n) {

+ if (n < 2) return(1); + return(n+f(n-1)) + }

[1] 0.356 0.004 0.355

(csum)(996)))[1:3]

[1] 0.652 0.000 0.640

[1] 500500

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

Error: object "v" not found

No suitable frames for recover()

Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry Indiana University School of Medicine

15032 Hunter Court, Westfield, IN 46074

(317) 490-5129 Work, & Mobile & VoiceMail
(317) 399-1219 Home

Skype: mkimpel

"The real problem is not whether machines think but whether men do." -- B. F. Skinner

[[alternative HTML version deleted]]

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 08 Nov 2008 - 22:11:15 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Sun 09 Nov 2008 - 22:30:23 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*