[R] question about the "Y of R" article in the latest R news

From: Mark Kimpel <mwkimpel_at_gmail.com>
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?

Beyond that, empiricist that I am, I decided to put the examples to the test. My source code and output is below, but the bottom line consists of 2 observations:

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)
+ }

>
>
> 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

[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

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.

list of date sections of archive