Question

I've been attempting to learn programming with the book "Structures and Interpretation of Computer Programs. To do the exercises I've been using DrRacket (I couldn't find a scheme interpreter for Windows 7, and DrRacket seems pretty good), and haven't had any problems so far. But while doing exercise 1.22 I've ran into an issue. I've wrote a procedure that gives a given number (n) of prime numbers larger than a:

(define (search-for-primes a n)
  (define (sfp-iter a n counter)
    (cond ((and (prime? a) (= counter n))
           ((newline) (display "end")))
          ((prime? a)
           ((newline)
            (display a)
            (sfp-iter (+ a 1) n (+ counter 1))))
          (else (sfp-iter (+ a 1) n counter))))
  (sfp-iter a n 0))

The procedure works as intended, displaying all that it should, but after displaying end it shows the following error message:

application: not a procedure; expected a procedure that can be applied to arguments given: # arguments...: #

And highlights the following line of code:

((newline) (display "end"))

What is the problem? (I apologize for any mistakes in spelling and so, English isn't my native language, I also apologize for any error in formatting or tagging, I'm new here)

Was it helpful?

Solution

You have a couple of parenthesis problems, this fixes it:

(define (search-for-primes a n)
  (define (sfp-iter a n counter)
    (cond ((and (prime? a) (= counter n))
           (newline) (display "end"))
          ((prime? a)
           (newline)
           (display a)
           (sfp-iter (+ a 1) n (+ counter 1)))
          (else (sfp-iter (+ a 1) n counter))))
  (sfp-iter a n 0))

In the first and second conditions of the cond, you were incorrectly surrounding the code with (). That's unnecessary, in a cond clause all the expressions that come after the condition are implicitly surrounded by a (begin ...) form, so there's no need to group them together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top