I got “scheme application not a procedure” in the last recursive calling of a function

前端 未结 2 1027
忘了有多久
忘了有多久 2020-11-30 14:07

so here is the code:

(define (time-prime-test n)
  (newline)
  (display n)
  (start-prime-test n (runtime)))

(define (start-prime-test n start-time)
  (if (         


        
相关标签:
2条回答
  • 2020-11-30 14:20
      ((time-prime-test n)
       (search-for-primes (+ n 1) m))
    

    This will try to apply the result of time-prime-test as a procedure. time-prime-test doesn't return a procedure. Use begin:

      (begin
       (time-prime-test n)
       (search-for-primes (+ n 1) m))
    
    0 讨论(0)
  • 2020-11-30 14:37

    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.

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

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

    Or a begin:

    (define (search-for-primes n m)
      (if (< n m)
          (begin
            (time-prime-test n)
            (search-for-primes (+ n 1) m))
          (display " calculating stopped. ")))
    
    0 讨论(0)
提交回复
热议问题