[R] I don't understand this

Thomas Lumley tlumley at u.washington.edu
Wed Sep 3 15:56:26 CEST 2003


On Wed, 3 Sep 2003, Simon Fear wrote:

> Thanks to everyone for enlightening me about (f)(g). So I
> suppose (f)(g)(h)... is OK as long as (f)(g) returns a function with
> an argument (and so on)?

Indeed.  As an example of what can be done

recurse <- function(f) {
              g<- function(h)
                  function(x)  f(h(h))(x)
	      g(g)
	}

is not only syntactically valid, but arguably useful (it gives a way of
writing anonymous recursive functions), so that
  recurse( function(self) function(n) if (n<2) 1 else n*self(n-1))
is an anonymous factorial function.

	-thomas




More information about the R-help mailing list