Scheme SICP练习1.11

Scheme SICP练习1.11,scheme,lisp,sicp,Scheme,Lisp,Sicp,我无法得到处决的结果 我认为它会无限期地重复 我通过向计数中添加1并重复它来创建一个结构 我做错了什么 #lang sicp (define (fi n) (f-iter 0 3 n)) (define (f-iter sum count n) (cond ((= count (+ n 1)) sum) ((< count 3) count) (+ (f-iter sum (- count 1) n)

我无法得到处决的结果

我认为它会无限期地重复

我通过向计数中添加1并重复它来创建一个结构

我做错了什么

   #lang sicp
   (define (fi n)
    (f-iter 0 3 n))
 
  (define (f-iter sum count n)
    (cond ((= count (+ n 1)) sum)
          ((< count 3) count)
          (+ (f-iter sum (- count 1) n)
             (* 2 (f-iter sum (- count 2) n))
             (* 3 (f-iter sum (- count 3) n))
             sum))
          (+ count 1)
          (f-iter sum count n))

最后的代码总结了我在问题下面的评论


对于任何大于等于3的n值,您需要函数的值在另外三个n值处。对于n=3,需要f2、f1和f0。对于f4,您需要f3、f2、f1。要进行新的计算,需要使用上一次计算中的三个旧值。因此,在迭代函数中,需要保留三个存储上述三个值的参数,一个存储n值的参数,以及一个跟踪进度的反参数


您需要做的第一件事是正确缩进代码:

(define (f-iter sum count n)
  (cond ((= count (+ n 1)) sum)
        ((< count 3) count)
        (+ (f-iter sum (- count 1) n)
           (* 2 (f-iter sum (- count 2) n))
           (* 3 (f-iter sum (- count 3) n))
           sum))
  (+ count 1)
  (f-iter sum count n))
让我们用注释注释代码:

(define (f-iter sum count n)
  (cond ((= count (+ n 1)) sum)
        ((< count 3) count)
        (+ (f-iter sum (- count 1) n)       ; the syntax of COND expects
                                            ; a list with a condition as
                                            ; the first element.
                                            ; here the first element is the
                                            ; function +, which is always true.
                                            ; as such it makes no sense.

           (* 2 (f-iter sum (- count 2) n))
           (* 3 (f-iter sum (- count 3) n))
           sum))                            ; here the result of COND is not
                                            ; used anymore. It is not returned from
                                            ; f-iter.

  (+ count 1)                               ; this result is never used.

  (f-iter sum count n))                     ; this calls the same function with the
                                            ; same arguments. Thus
                                            ; f-iter calls itself
                                            ; indefinitely at this point.

您有几个语法错误:cond之外有两个表达式,它们应该做什么?con被评估,其结果被忽略。cond的最后一个条件必须有一个else,否则它将不起作用。对于任何大于等于3的n值,您需要函数的值在另外三个n值处。对于n=3,需要f2、f1和f0。对于f4,您需要f3、f2、f1。要进行新的计算,需要使用上一次计算中的三个旧值。因此,在迭代函数中,需要保留三个存储上述三个值的参数,一个存储n值的参数,以及一个跟踪进度的反参数。该条款的价值是总和。然后在cond后面还有两个表达式,最后一个是结果,因此您的定义相当于定义f-iter和计数n f-iter和计数n。给自己找一个可以缩进代码和显示平衡括号的编辑器。
(define (f-iter sum count n)
  (cond ((= count (+ n 1)) sum)
        ((< count 3) count)
        (+ (f-iter sum (- count 1) n)       ; the syntax of COND expects
                                            ; a list with a condition as
                                            ; the first element.
                                            ; here the first element is the
                                            ; function +, which is always true.
                                            ; as such it makes no sense.

           (* 2 (f-iter sum (- count 2) n))
           (* 3 (f-iter sum (- count 3) n))
           sum))                            ; here the result of COND is not
                                            ; used anymore. It is not returned from
                                            ; f-iter.

  (+ count 1)                               ; this result is never used.

  (f-iter sum count n))                     ; this calls the same function with the
                                            ; same arguments. Thus
                                            ; f-iter calls itself
                                            ; indefinitely at this point.