我得到了“方案申请而不是程序”。在函数的最后递归调用中 [英] I got "scheme application not a procedure" in the last recursive calling of a function

查看:130
本文介绍了我得到了“方案申请而不是程序”。在函数的最后递归调用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是代码:

(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:


100017
100018
100019 * 54计算停止。 。 。申请:不是程序;期望一个可以应用于参数的过程

给出:#< void>

arguments ...:

#< void>

100017 100018 100019 * 54 calculating stopped. . . application: not a procedure; expected a procedure that can be applied to arguments
given: #<void>
arguments...:
#<void>


推荐答案

您打算在的结果部分内执行两个表达式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)不评估为一个过程,它评估为#< void>

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

您可以通过使用<来解决问题code> cond :

(define (search-for-primes n m)
  (cond ((< n m)
         (time-prime-test n)
         (search-for-primes (+ n 1) m))
        (else
         (display " calculating stopped. "))))

或者开始

(define (search-for-primes n m)
  (if (< n m)
      (begin
        (time-prime-test n)
        (search-for-primes (+ n 1) m))
      (display " calculating stopped. ")))

这篇关于我得到了“方案申请而不是程序”。在函数的最后递归调用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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