所以这里是代码:
(define (time-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
(define (search-for-primes n m)
(if (< n m)
((time-prime-test n)
(search-for-primes (+ n 1) m))
(display " calculating stopped. ")))
(search-for-primes 100000 100020)
在“计算停止”后我收到了这个错误。已显示。像下面这样:
100017 100018 100019 * 54 calculating stopped. . . application: not a procedure; expected a procedure that can be applied to arguments
given: #<void>
arguments...:
#<void>
最佳答案
您打算在 if
的后续部分中执行两个表达式,但是 if
只允许一个表达式在结果中,一个在替代中。
将两个表达式括在括号之间(如您所做的那样)将不起作用:结果表达式将作为第一个表达式的函数应用程序进行评估,第二个表达式作为其参数,产生错误 "application: not a procedure; expected a procedure that can be applied to arguments ..."
, 因为 (time-prime-test n)
不计算为过程,它计算为 #<void>
.
您可以使用 cond
来解决问题。 :
(define (search-for-primes n m)
(cond ((< n m)
(time-prime-test n)
(search-for-primes (+ n 1) m))
(else
(display " calculating stopped. "))))
或
begin
:(define (search-for-primes n m)
(if (< n m)
(begin
(time-prime-test n)
(search-for-primes (+ n 1) m))
(display " calculating stopped. ")))
关于recursion - 我在最后一次递归调用函数时得到了 "scheme application not a procedure",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12183096/