如何使用accumulate求解以下方程(方案)
我正在尝试做以下问题(有一个公式,所以我打印屏幕并上传它)
公式http://img248.imageshack.us/img248/6558/problemh.jpg
(http://img248.imageshack.us/img248/6558/problemh.jpg)
使用accumulate:
(define (accumulatecombiner null-value term a next b) (if (> ab) 空值 (combiner (term a) (accumulatecombiner null-value term (next a) next b))))
但我几乎不知道该怎么做。它应该看起来像 (定义(sum-func fk)...?
我不知道应该如何定义 f(x+ j*h) 以便将变化的数字 j (从 0 变化到给定的数字 k)放入其中......换句话说,我很迷失。
感谢您的快速回复。我正在为期中考试学习,我正在尝试做前几年的期中考试,但我只是被困在这里。我知道如何使用累积和其中的所有内容,但我只是不知道如何执行 f(x + jh)。我正在尝试这个: <代码>(定义(求和函数术语 abhx) (定义(下一个a)(+ a 1)) (定义(项a)(项(+ x(* ha)))) (累加+0(项a)a(下一个a)b)) 但这不起作用... 换句话说,我不知道如何在术语中使用“下一个”(更改部分)。 (抱歉,如果我解释得很清楚 - 英语不是我的母语)
I'm trying to do the following problem (there is a formula so I print-screened and uploaded it)
Formula http://img248.imageshack.us/img248/6558/problemh.jpg
(http://img248.imageshack.us/img248/6558/problemh.jpg)
Using accumulate:
(define (accumulate combiner null-value term a next b)
(if (> a b) null-value
(combiner (term a) (accumulate combiner null-value term (next a) next b))))
but I have pretty much no idea how to do it. It should look something like(define (sum-func f k)...?
I'm not sure how I should define f(x+ j*h) so that to put the changing number j (changes from 0 to the given number k) inside of it... In other words, I'm pretty lost.
Thank you for the fast respond. I'm studying for my midterms and I'm trying to do the midterms from the previous years, but I just got stuck here. I know how to use the accumulate and everything in it, but I just don't know how to do the f(x + jh). I'm trying this:(define (sum-func term a b h x)
(define (next a) (+ a 1))
(define (term a) (term (+ x (* h a))))
(accumulate + 0 (term a) a (next a) b))
But it's not working...
In other words I don't know how to use the 'next' (changing part) inside the term..
(sorry if I'm explaining very clearly - English is not my native)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是一个家庭作业问题,但无论如何我都会为您提供一些如何解决它的想法。为了简单起见,我们假设我们的问题是求 j 和 k 之间所有数字的平方和。您可以使用上面的累积函数来解决这个简单的问题。您所要做的就是为这个问题定义组合器、术语和下一个函数,并定义最终的空值。
这里的项函数应该计算每个 j 的平方。因此 term 将以 j 作为参数 &将返回 j 的平方作为结果。
(define (term j) (sqr j))
Next 函数应该获取序列中的下一个 j。
(define (next j) (+ j 1)
组合器函数应将两项组合在一起。
(define (combiner t1 t2) (+ t1 t2))
最后,空值表示停止条件,即当我们累积了从 j 到 k 的所有值时应传递给组合器的最后一个值。在这种情况下,我们所要做的就是将其定义为零。
(定义空值 0)
对于您的问题,除了术语函数之外,大多数函数都是相同的。
在这个问题中,术语“函数”非常简单,它只是求所提供数字的平方。但是,在您的情况下,它并不像求 j 的平方那么简单,因为您还定义了其他几个常量。
希望这能帮助您解决问题。
如果您需要对此有更多了解,您可能需要查看 SICP 第 2 章中的累积问题。
I am not sure if this is a homework problem or not but in any case I will provide you with some ideas on how to solve it. For the sake of simplicity let us assume our problem is to find the sum of squares of all numbers between j and k. You can use your accumulate function above to solve this simple problem. All you will have to do is define your combiner, term and next functions for this problem along with defining the final null-value.
Term function here should calculate the squares for each j. Therefore term will take j as a argument & will return the square of j as the result.
(define (term j) (sqr j))
Next function should get the next j in the sequence.
(define (next j) (+ j 1)
Combiner function should combine two terms together.
(define (combiner t1 t2) (+ t1 t2))
Finally, null-value signifies the stop condition, the last value that should be passed to combiner when we have accumulated all the values from j to k. All we have to do is just define it as a zero in this case.
(define null-value 0)
For your problem, most of these functions are same except for the term function.
In this problem, the term function was a very simple one, it just found the square of the supplied number. However, in your case, it is not as simple as finding the square of j since you have several other constants defined.
Hope this will help you solve the problem.
If you need even more insight into this, you may want to look at the accumulate problems in SICP chapter 2.
我想通了。我想我对算法的思考太多了(认为它比实际复杂得多),这导致我犯了许多愚蠢的错误。
I figured it out. I guess I was overthinking the algorythm (thinking it was much more complex than it was) and this led me to many stupid mistakes.