【问题标题】:I got "scheme application not a procedure" in the last recursive calling of a function我在函数的最后一次递归调用中得到“方案应用程序不是过程”
【发布时间】:2012-08-24 08:49:57
【问题描述】:

代码如下:

(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 计算停止。 . .申请:不是程序;期望可以应用于参数的过程
给定:#
论据...:
#

【问题讨论】:

  • 注意:如果您在 DrRacket 中运行此程序,IDE 的错误突出显示应该围绕导致问题的功能应用程序。你看到了吗?

标签: recursion if-statement scheme racket


【解决方案1】:

您打算在 if 的后件部分内执行两个表达式,但 if 只允许在后件中使用一个表达式,在备选中使用一个。

用括号括住两个表达式(如您所做的那样)将不起作用:结果表达式将作为第一个表达式的函数应用程序进行评估,第二个表达式作为其参数,产生错误"application: not a procedure; expected a procedure that can be applied to arguments ...",因为@987654324 @ 不计算为过程,它计算为#&lt;void&gt;

您可以使用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. ")))

【讨论】:

  • 感谢非常详细的回答!由于我在工作,现在无法验证,我认为您对此绝对正确。
【解决方案2】:
  ((time-prime-test n)
   (search-for-primes (+ n 1) m))

这将尝试将time-prime-test 的结果作为一个过程应用。 time-prime-test 不返回过程。使用begin

  (begin
   (time-prime-test n)
   (search-for-primes (+ n 1) m))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-15
    • 2016-03-05
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多