[R] Function that operates on functions: it's ok, but the display isn't
Alberto Vieira Ferreira Monteiro
albmont at centroin.com.br
Wed Oct 11 02:31:24 CEST 2006
Deepayan Sarkar wrote:
>
>> # 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: 0264BE84>
>
> Presumably, 'f' takes the value 'g' in this environment, and indeed
>
>> environment(h)$f
>
> function(y) y^2
>
Ok, so it's possible to recover the meaning of h. It might then
be possible to write something like "simplify", that would
take a function and try to "rewrite" it using simpler blocks.
In this case, "simplify" h would return the function h1
equal to function(x) (x+1)^2 - x^2.
> 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 ?
>
Yesterday, I would expect function(y) (y+1)^2 - y^2, but today I expect
the same as above.
Alberto Monteiro
More information about the R-help
mailing list