This Is It
This Is It

Reputation: 3

Bad Let in Form Scheme

(define (prime max)
  (let ((a 2)))
  (if not(= modulo max 2) 0)
  ((+ a 1)
    prime(max))
  )

It tells me bad let in form (let ((a 2))) but as far as I'm aware, the syntax and code is right

Upvotes: 0

Views: 760

Answers (2)

6502
6502

Reputation: 114481

let format is

(let ((<var1> <value1>)
      (<var2> <value2>)
      ...
      (<varN> <valueN>))
  <expr1>
  <expr2>
  ...
  <exprN>)

Also the general form for calling a function is

(<function> <arg1> <arg2> ... <argN>)

so your not call is wrong, should be (not ...) and the call to prime should have the form (prime max).

You got the addition "operator" (+ a 1) correct but indeed one big difference between Lisp dialects and other languages is that you don't have special operators, just functions. (+ a 1) is just like (add a 1): you are just calling a function that is named +; no special unary prefix/postfix case or precedence and associativity rules... just functions: not is a function + is a function.

Lisp "syntax" may feel weird at first (if and because you've been exposed to other programming languages before), but the problem doesn't last long and after a little all the parenthesis just disappear and you begin to "see" the simple tree structure of the code.

On the other spectrum of syntax complexity you've for example C++ that is so complex that even expert programmers and compiler authors can debate long just about how to interpret and what is the semantic meaning of a given syntax construct. Not kidding there are C++ rules that goes more of less "if a syntax is ambiguous and could be considered both as a declaration and as an expression, then it's a declaration" (https://en.wikipedia.org/wiki/Most_vexing_parse). Go figure.

Upvotes: 0

Amadan
Amadan

Reputation: 198324

No, it is not right. let form has this syntax: (let binds body) Your bindings are ((a 2)). Where's your body? You put it outside the let form. This raises two problems: let is malformed by only having one argument instead of two, and a is undeclared at the location it appears in. (Without going into the logic of the code, which is also incorrect, assuming you are trying for a primality test function.)

Upvotes: 2

Related Questions