# Re: [R] Function that operates on functions: it's ok, but the display isn't

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue 10 Oct 2006 - 20:16:41 GMT

You need to substitute it into yourself. Also note that displaying a function will display its source attribute which may get unsynchronized with the actual function if the function was constructed yourself so NULL it out to be sure what you are seeing is what the unction actually is:

# g is the function that returns the square of a number g <- function(y) y^2

> # f1 is a function that takes one function
> # as argument, and returns another function
> f1 <- function(f) function(x) f(x+1) - f(x)
>
> # h(x) is g(x+1) - g(x) or 2x + 1
> h <- f1(g)
> h
function(x) f(x+1) - f(x)
<environment: 0x01e79470>
>
> f2 <- function(f) eval(substitute(function(x) f(x+1) - f(x)))
> h2 <- f2(g)
> h2 # this is displaying the source attribute, not the actual function
function(x) f(x+1) - f(x)
<environment: 0x01e75494>
> attr(h2, "source") <- NULL
> h2 # now we are seeing the actual function
function (x)
g(x + 1) - g(x)
<environment: 0x01e75494>

On 10/10/06, Alberto Monteiro <albmont@centroin.com.br> wrote:
> The following code works fine:
>
> # g is the function that returns the square of a number
> g <- function(y) y^2
>
> # f1 is a function that takes one function
> # as argument, and returns another function
> f1 <- function(f) function(x) f(x+1) - f(x)
>
> # h(x) is g(x+1) - g(x) or 2x + 1
> h <- f1(g)
>
> # h(1) = 3
> # h(2) = 5
> # h(3) = 7
>
> So far, so good. But why:
>
> h
>
> shows:
>
> function(x) f(x+1)-f(x)
> <environment: 0264BE84>
>
> I don't get it. h should show function(x) g(x+1)-g(x)
> or something like that.
>
> Alberto Monteiro
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>

R-help@stat.math.ethz.ch 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 Wed Oct 11 06:21:12 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 10 Oct 2006 - 20:30:36 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.