【发布时间】:2013-09-17 16:31:06
【问题描述】:
我今天在scheme中写了一个简单的导函数。我被要求返回一个函数,例如 g(x) = (f (x+h) -f(x))/h 。这足以返回一个函数还是只返回一个值?
(define (der f h)
(lambda (x)
(/ (- (f(+ x h)) (f x)) h)))
【问题讨论】:
标签: scheme derivative
我今天在scheme中写了一个简单的导函数。我被要求返回一个函数,例如 g(x) = (f (x+h) -f(x))/h 。这足以返回一个函数还是只返回一个值?
(define (der f h)
(lambda (x)
(/ (- (f(+ x h)) (f x)) h)))
【问题讨论】:
标签: scheme derivative
是的,问题中的代码正在返回一个函数(这就是 lambda 的用途)。如果它返回一个值,它将丢失带有(lambda (x) 的行和相应的右括号。
还要注意,虽然程序是正确的,但问题中的公式是不对的,应该是:
g(x) = (f(x+h) - f(x))/h ; notice that x is the parameter to the second call to f
附带说明,使用定义的导数函数的正确方法是:
(define der-sqr (der square 1e-10)) ; create the derivative *function*
(der-sqr 10) ; apply the function
=> 20.000072709080996
【讨论】: