为什么我要“申请不是程序”? [英] why am I getting "application not a procedure"?

查看:205
本文介绍了为什么我要“申请不是程序”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个通过迭代过程来计算f的过程。函数f由以下规则定义:如果n $ b f(n)= n。 4和

f(n)= f(n-1)+ 2f(n-2)+ 3f(n-3)+ 4f(n-4),如果n> = 4。

以下是我的程序:

  (定义(fn)
(define(newF temp n)
(letrec((first( - n 1))
(second( - n 2))
(third /第四( - n 3))
(第五( - n 4)))
(define(d)
((if((if(< second 4)(set!temp(+ temp(* second 2)))(newF temp second))
(if(< third /第四4)(set!temp(+ temp(* third / fourth 3)(* third / fourth 4)))(newF temp third / fourth))
(if(< fifth 4) temp(+ temp(* fifth 4)))(newF temp five))))
(d))
temp)
(newF 0 n))

不幸的是,当我运行(f 7)时,我得到了这个错误(它涉及if语句体):

  application:不是程序; 
预计可应用于参数
的程序:#< void>
参数...:
#< void>
#< void>
#< void>

任何人都知道我为什么以及如何修复它的原因?

解决方案

基于您的上一个问题,您要去关于它的完全必要的方式(当然,不正确,否则你不会问这个问题),这不是Scheme如何工作的方式。下面是一个函数的写法(但不是迭代的):

 (define(fn)
(if < n 4)$ * b $ bn
(+(f( - n 1))(* 2(f( - n 2))) (f( - n 4))))))

现在让我们看看如何写这个反复。首先,让我们来看看如何写一个迭代的Fibonacci函数:

$ $ $ $ $ $ $ $ $ $($ (i 0)(a 0)(b 1))
(if(> = in)
a
(loop(+ i 1)b(+ ab)))))

这与以下JavaScript代码完全相同:

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

请注意 i a b 实际上得到更新。我们使用尾递归来更新值,而不是通过重新分配/变异(即,在JS中不使用 = set!为什么tail-recursion在Scheme 中非常重要。



所以,你可以用你的函数做类似的事情:

 (define(fn)
(let loop((i 0)(a 0)(b 1)(c 2)(如果(> = in)
a
(loop(+ i 1)bcd(+ dccbbbaaaa)))))


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?

解决方案

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

这篇关于为什么我要“申请不是程序”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆