1
votes

I have written the following code snippet in scheme,

(define (test-for-prime number divisor)
  (cond (prime? number) (number)
        (else (let ((next-divisor) (find-next-divisor number (+ 1 divisor)))
                 (test-for-prime (/ number next-divisor) (next-divisor))))))

However, I get the following error

let: bad syntax (not an identifier and expression for a binding) in: (next-divisor)

How do I rectify it?

2

2 Answers

2
votes

Try this version, it fixes all the syntax errors:

(define (test-for-prime number divisor)
  (cond ((prime? number)
         number)        
        (else
         (let ([next-divisor (find-next-divisor number (+ 1 divisor))])
           (test-for-prime (/ number next-divisor) next-divisor)))))

You have lots of misplaced parenthesis. Some are missing, some are mistaken ... I'd suggest you take a good read at a Scheme tutorial and play around writing some basic procedures to get the hang of it, in particular here is the documentation and correct structure for a let special form:

(let ([id val-expr] ...) body ...+)

The first form evaluates the val-exprs left-to-right, creates a new location for each id, and places the values into the locations. It then evaluates the bodys, in which the ids are bound. The last body expression is in tail position with respect to the let form. The ids must be distinct according to bound-identifier=?

Also it's a good idea to use an IDE/editor which highlights this kind of problems and helps you indent the code properly. In particular, be aware that a variable must not be surrounded in parenthesis, when you write this: (x) Scheme assumes that x is a procedure and that you're calling it.

1
votes

Let has the following syntax (example with two bindings):

(let ((<var> <expr>)
      (<var1> <expr1>))
  <body>)

If we fill in your let form, you have two bindings: next-divisor gets bound to nothing, and find-next-divisor gets bound to number, but there is a third form (+ 1 divisor) that does not fit into let's syntax.

This might be what you want:

(let ((next-divisor (find-next-divisor number (+ 1 divisor))))
  ;; ...
  )