方案导数函数 [英] Scheme Derivative Function

查看:47
本文介绍了方案导数函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天在scheme中写了一个简单的导数函数.我被要求返回一个函数,使得 g(x) = (f (x+h) -f(x))/h .这足以返回一个函数还是只返回一个值?

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)))    

推荐答案

是的,问题中的代码正在返回一个函数(这就是 lambda 的用途).如果它返回一个,它将缺少带有(lambda (x) 和相应的右括号的行.

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

这篇关于方案导数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆