为什么我要“申请不是程序”? [英] why am I getting "application not a procedure"?
问题描述
我正在尝试编写一个通过迭代过程来计算f的过程。函数f由以下规则定义:如果n 以下是我的程序: 不幸的是,当我运行(f 7)时,我得到了这个错误(它涉及if语句体): 任何人都知道我为什么以及如何修复它的原因? 基于您的上一个问题,您要去关于它的完全必要的方式(当然,不正确,否则你不会问这个问题),这不是Scheme如何工作的方式。下面是一个函数的写法(但不是迭代的): 现在让我们看看如何写这个反复。首先,让我们来看看如何写一个迭代的Fibonacci函数: 这与以下JavaScript代码完全相同: 请注意 所以,你可以用你的函数做类似的事情: 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: Unfortunately, when I ran (f 7), I got this error(which referred to the if statement body): 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: Now, let's see how one might write this iteratively. First, let's see how an iterative Fibonacci function is written: This does the same thing as the following JavaScript: Notice how So, you would do something similar with your function:
这篇关于为什么我要“申请不是程序”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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(< 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))
application:不是程序;
预计可应用于参数
的程序:#< void>
参数...:
#< void>
#< void>
#< void>
(define(fn)
(if < n 4)$ * b $ bn
(+(f( - n 1))(* 2(f( - n 2))) (f( - n 4))))))
$ $ $ $ $ $ $ $ $ $($ (i 0)(a 0)(b 1))
(if(> = in)
a
(loop(+ i 1)b(+ ab)))))
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!$ c $在Scheme中)。我最近写了一个关于为什么tail-recursion在Scheme 中非常重要。
(define(fn)
(let loop((i 0)(a 0)(b 1)(c 2)(如果(> = in)
a
(loop(+ i 1)bcd(+ dccbbbaaaa)))))
(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))
application: not a procedure;
expected a procedure that can be applied to arguments
given: #<void>
arguments...:
#<void>
#<void>
#<void>
(define (f n)
(if (< n 4)
n
(+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))) (* 4 (f (- n 4))))))
(define (fib n)
(let loop ((i 0) (a 0) (b 1))
(if (>= i n)
a
(loop (+ i 1) b (+ a b)))))
fib = function (n) {
return (function loop(i, a, b) {
return i >= n ? a : loop(i + 1, b, a + b);
})(0, 0, 1);
};
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.(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)))))