Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

recursion - I got "scheme application not a procedure" in the last recursive calling of a function

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 (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 calculating stopped. . . application: not a procedure; expected a procedure that can be applied to arguments
given: #<void>
arguments...:
#<void>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...