【发布时间】:2014-11-22 16:34:05
【问题描述】:
我正在尝试编写一个通过迭代过程计算 f 的过程。函数 f 由以下规则定义:
f(n) = n,如果 n
f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) + 4f(n - 4),如果 n >= 4。
这是我的程序:
(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))
不幸的是,当我运行 (f 7) 时,我得到了这个错误(它引用了 if 语句体):
application: not a procedure;
expected a procedure that can be applied to arguments
given: #<void>
arguments...:
#<void>
#<void>
#<void>
有人知道为什么以及如何解决它吗?
【问题讨论】:
-
你看到if前面的两个
((了吗?set!返回 void 并尝试将其作为过程应用 -
在问这个问题之前你搜索过那个错误信息吗? google search for
"application: not a procedure" site:stackoverflow.com会出现一大堆结果,您很快就会发现它在((if ....) ...)中。第一个(if ...)返回一些东西(那不是函数),然后你尝试像函数一样调用它。
标签: recursion functional-programming scheme racket tail-recursion