12

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>

Óscar López
  • 232,561
  • 37
  • 312
  • 386
tuo
  • 586
  • 1
  • 4
  • 20
  • 3
    Note: if you run this in DrRacket, the IDE's error highlight should be surrounding the function application that's causing the issue. Do you see this? – dyoo Aug 29 '12 at 18:19

2 Answers2

19

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. ")))
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    Thanks for the very detailed answer! Can't verify it now since i'm at work, I think you're absolutely right about this. – tuo Aug 30 '12 at 03:34
3
  ((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))
hzap
  • 1,103
  • 7
  • 9