-1

I can't get the results of the execution.

I think it's repeated indefinitely.

I made a structure by adding 1 to the count and repeating it.

What did I do wrong?

   #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))
acho
  • 53
  • 7
  • 1
    You have several syntax errors :( there are two expressions outside of the `cond`, what are those supposed to do? the `con` is evaluated and its results ignored. And the last condition of the `cond` must have an `else`, otherwise it won't work. – Óscar López Apr 06 '21 at 13:00
  • For any value of n greater and equal to 3, you need the values of the function at three more values of n. For n = 3, you need f(2), f(1) & f(0). For f(4), you need f(3), f(2), f(1). You need to have three older values from the previous computation for a fresh calculation. So in your iterative function, you need to keep three arguments which store the above 3 values, one argument for storing the value of n, and a counter argument which keeps track of the progress. – tf3 Apr 06 '21 at 14:24
  • Your last condition is `+`, which is truth-y. The value of that clause is `sum`. Then you have two more expressions after the `cond` and the last one is the result, so your definition is equivalent to `(define (f-iter sum count n) (f-iter sum count n))`. (Get yourself an editor that can indent code and show balanced parentheses.) – molbdnilo Apr 06 '21 at 15:27

2 Answers2

1

The first thing you need to do is to indent your code correctly:

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

Let's annotate the code with comments:

(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.
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
0

The code at the end sums up my comment below the question.

For any value of n greater and equal to 3, you need the values of the function at three more values of n. For n = 3, you need f(2), f(1) & f(0). For f(4), you need f(3), f(2), f(1). You need to have three older values from the previous computation for a fresh calculation. So in your iterative function, you need to keep three arguments which store the above 3 values, one argument for storing the value of n, and a counter argument which keeps track of the progress.

(define (funcn n) (func-iter 2 1 0 n 2))

(define (func-iter p1 p2 p3 n count) ; p1 and p2 exchange places with
                                     ; p2 and p3 respectively on every iteration
  (cond ((= n 0) 0)
        ((= n 1) 1)
        ((= n count) p1) ; p1 stores the cumulative sum to be returned
                         ; when 'count' is equal to n
        (else (func-iter (+ p1 (* p2 2) (* p3 3)) p1 p2 n (+ count 1)))
  )
)
tf3
  • 447
  • 1
  • 4
  • 16