Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

recursion - why am I getting "application not a procedure"?

I am trying to write a procedure that computes f by means of an iteratve process. The function f is defined by the rule that

f(n) = n, if n < 4 and

f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) + 4f(n - 4), if n >= 4.

Here is my procedure:

(define (f n)
  (define (newF temp n)
    (letrec ((first (- n 1))
             (second (- n 2))
             (third/fourth (- n 3))
             (fifth (- n 4)))
      (define (d)
        ((if (< first 4) (set! temp (+ temp first)) (newF temp first))
         (if (< second 4) (set! temp (+ temp (* second 2))) (newF temp second))
         (if (< third/fourth 4) (set! temp (+ temp (* third/fourth 3) (* third/fourth 4))) (newF temp third/fourth))
         (if (< fifth 4) (set! temp (+ temp (* fifth 4)))(newF temp fifth))))
      (d))
    temp)
  (newF 0 n))

Unfortunately, when I ran (f 7), I got this error(which referred to the if statement body):

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: #<void>
  arguments...:
   #<void>
   #<void>
   #<void>

Anyone know the reason why and how I can fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on your previous question, you're going about it in a totally imperative way (and incorrect, of course, otherwise you wouldn't be asking this question), which is not how Scheme likes to work. Here's a functional (but not iterative) way to write the function:

(define (f n)
  (if (< n 4)
      n
      (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))) (* 4 (f (- n 4))))))

Now, let's see how one might write this iteratively. First, let's see how an iterative Fibonacci function is written:

(define (fib n)
  (let loop ((i 0) (a 0) (b 1))
    (if (>= i n)
        a
        (loop (+ i 1) b (+ a b)))))

This does the same thing as the following JavaScript:

fib = function (n) {
  return (function loop(i, a, b) {
            return i >= n ? a : loop(i + 1, b, a + b);
          })(0, 0, 1);
};

Notice how i, a, and b actually get updated. We use tail-recursion to update the values, not by reassignment/mutation (i.e., not using = in JS or set! in Scheme). I recently wrote an answer about why tail-recursion is so important in Scheme.

So, you would do something similar with your function:

(define (f n)
  (let loop ((i 0) (a 0) (b 1) (c 2) (d 3))
    (if (>= i n)
        a
        (loop (+ i 1) b c d (+ d c c b b b a a a a)))))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...