简体   繁体   English

从a到b的方案总和

[英]Scheme Sum from a to b Iteratively

I am trying to write a procedure that adds all the numbers between a and b. 我正在尝试编写一个过程,将a和b之间的所有数字相加。 For example if a=1 and b=5, the procedure would add 1+2+3+4+5. 例如,如果a = 1且b = 5,则该过程将添加1 + 2 + 3 + 4 + 5。 Here's what I have so far. 到目前为止,这就是我所拥有的。 I want to write this iteratively. 我想反复写这个。 Thanks! 谢谢!

(define (sum term a next b)
  (define (iter a result)
    (if (> a b)
        sum
        (iter ?? ??)))
  (iter ?? ??))

For starters, notice that the sum procedure receives four parameters, clearly you'll have to use all of them as part of the solution, in particular, the two procedures will bee needed at some point. 对于初学者来说,注意sum程序接收四个参数,显然你将不得不使用它们作为解决方案的一部分,特别是将蜜蜂需要在某一时刻的两个过程。 Here's what each one represents: 这是每个人代表的意思:

  • term : function to apply to each element of the sequence term :应用于序列中每个元素的函数
  • a : starting number of the range (inclusive) a :范围的起始编号(含)
  • next : function to determine the next element of the sequence next :确定序列的下一个元素的功能
  • b : ending number of the range (inclusive) b :范围的结束编号(含)

For instance, this is how you'd test the procedure with the example in the question: 例如,这是您使用问题中的示例测试过程的方式:

(sum identity 1 add1 5)
=> 15

(= (sum identity 1 add1 5) (+ 1 2 3 4 5))
=> #t

But wait, how do we implement it? 但是,等等,我们如何实施呢? that's for you to discover - it won't do you any good if we give the answer right away, but I can give you some hints: 这是您发现的-如果我们立即给出答案不会对您有任何好处,但是我可以给您一些提示:

(define (sum term a next b)
  ; internal procedure for implementing the iteration
  ; a      : current number in the iteration
  ; result : accumulated sum so far
  (define (iter a result) 
    (if (> a b)  ; if we traversed the whole range
        result   ; then return the accumulated value
        (iter    ; else advance the iteration
         ??      ; what's the next value in the range?
         (+      ; accumulate the result by adding
          ??     ; the current term in the range and
          ??)))) ; the accumulated value
  ; time to call the iteration
  (iter ??       ; start iteration. what's the initial value in the range?
        ??))     ; what's the initial value of the sum?

Okay, so you have two iter calls, and you have to decide which values to put into them: 好的,因此您有两个iter调用,并且必须确定要在其中放入哪些值:

  • For the outer iter call, you have to decide what the initial value for a and result is. 对于外部iter调用,您必须确定aresult的初始值是什么。
    • For the initial value of result , think about what your function should return if a were greater than b to begin with. 对于result的初始值,请考虑一下,如果a大于b ,函数应该返回什么。
  • For the inner iter call, you have to decide what the next value for a and result will be. 对于内部iter调用,您必须确定aresult的下一个值是什么。
    • For the next value of result , you should add in the current number to the previous result. 对于result的下一个值,您应该将当前数字添加到上一个结果中。

I hope this helps. 我希望这有帮助。

A lot of algorithms call for iteration, like simpson's rule for approximating integrals. 许多算法都要求迭代,例如用于近似积分的辛普森规则。 We can 'fake' the iteration by calling an iterative helper . 我们可以通过调用迭代助手来“伪造”迭代。

(define (sum a b)
 (sum-iter a b 0))

(define (sum-iter index n sum)
   (if (= n index)
    (+ sum index) 
    (+  (sum-iter  n (+ index 1) (+ sum index)))
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM