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

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Oct 10 22:21:08 CEST 2006


On 10/10/06, Alberto Monteiro <albmont at 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>

Presumably, 'f' takes the value 'g' in this environment, and indeed

> environment(h)$f
function(y) y^2

> I don't get it. h should show function(x) g(x+1)-g(x)
> or something like that.

Not that I know much about functions and environments, but printing is
ultimately a matter of aesthetics. What do you think "should" happen
when

h <- f1(function(y) y^2)

and you try to print h ?

-Deepayan



More information about the R-help mailing list