Scheme Derivative Function

2019-03-01 07:22发布

I wrote a simple derivative function in scheme today. I was asked to return a function such that g(x) = (f (x+h) -f(x))/h . Does this suffice to return a function or does this only return a value?

(define (der f h)
 (lambda (x)
 (/ (- (f(+ x h)) (f x)) h)))    

1条回答
走好不送
2楼-- · 2019-03-01 07:43

Yes, the code in the question is returning a function (that's what the lambda is for). If it were returning a value, it'd be missing the line with (lambda (x) and the corresponding closing parentheses.

Also notice that although the procedure is correct, the formula stated in the question isn't right, it should be:

g(x) = (f(x+h) - f(x))/h ; notice that x is the parameter to the second call to f

As a side note, the correct way to use the derivative function as defined would be:

(define der-sqr (der square 1e-10)) ; create the derivative *function*
(der-sqr 10)                        ; apply the function
=> 20.000072709080996
查看更多
登录 后发表回答