Hi Peter,
Here is my homework :)
DD <- function(f,order=1){
f.str <- "body(f)"
while(order>=1) {
f.str <- paste('D(',f.str,',"x")',sep="")
order <- order-1}
ddf <- f
body(ddf) <- eval(parse(text=f.str))
ddf}
f <- function(x,alpha) x^alpha
DD(f)(2,3)
g <- function(x,alpha) x^alpha+2*x^(alpha-1)
DD(g,2)(2,3)
It would be better to handle expressions directly, but I can only paste
strings. The reason why I didn't prefer using body() is that the derivative
only works on a mathematical function not a R function. The body(f) would
cause error when function is constructed by "{}".
-----
A R learner.