我得到了“方案申请不是程序"在函数的最后一次递归调用中 [英] I got "scheme application not a procedure" in the last recursive calling of a function
问题描述
代码如下:
(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)
在计算停止"后我收到了这个错误.已显示.如下图:
and i got this error after "calculating stopped." has been displayed. like below:
100017100018100019 * 54 计算停止...应用程序:不是程序;期望一个可以应用于参数的过程
给定:#
参数...:
#
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
只允许一个在结果中表达,在替代中表达.
You intend to execute two expressions inside the consequent part of the if
, but if
only allows one expression in the consequent and one in the alternative.
将两个表达式括在括号之间(如您所做的那样)将不起作用:结果表达式将被评估为第一个表达式的函数应用程序,第二个表达式作为其参数,产生错误 "application: not一个过程;期望一个可以应用于参数的过程......"
,因为 (time-prime-test n)
不计算为过程,它计算为 #
.
Surrounding both expressions between parenthesis (as you did) won't work: the resulting expression will be evaluated as a function application of the first expression with the second expression as its argument, producing the error "application: not a procedure; expected a procedure that can be applied to arguments ..."
, because (time-prime-test n)
does not evaluate to a procedure, it evaluates to #<void>
.
您可以使用 cond
来解决问题:
You can fix the problem by either using a 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. ")))
这篇关于我得到了“方案申请不是程序"在函数的最后一次递归调用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!