-4

I can't for the life of me find out why this code produces the error. Here's the error:

let*: bad syntax (missing body) in: (let* ((tempp2 (p2) (letrec ((mloop (p1 p2) (if (= (length p1) 0)) (else if ((= (length p2) 0) ((set! p2 (pn)) (multiloop (cdr p1) (p2)))) (else (let* ((prod (append (prod) (cons (* (coeff (car p1)) (coeff (car p2))) (+ (expon (car p1)) (expon (car p2)))))))) (set! p2 (cdr p2)) (mloop (p1 p2)) (simplify (sort newone))))))))))

Here's the code:

(define multiplyPoly (lambda (p1 p2)
                 (
                  (let* ((hardp2 (p2)
                 (letrec 
                     ((mloop (p1 p2)
                       (if (= (length p1) 0))
                       (else if ((=(length p2) 0) ((set! p2 (hardp2)) (multiloop (cdr p1) (p2))))
                    (else
                     (let* ([prod  (append (prod) (cons(*(coeff(car p1)) (coeff(car p2))) (+(expon(car p1)) (expon(car p2)))))]))
                      (set! p2 (cdr p2))
                           (mloop (p1 p2))
                      (simplify (sort newone)))))))))))))
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

3

You have lots of syntax errors in your code. I urge you to install a IDE that does matching of parentheses and ident your code properly, like DrRacket. DrRacket supports both R5RS and R6RS so you can still program in Scheme with DrRacket.

Usually a let, let* and letrec has the following form:

(let ((var expression) ...)
  body)

In a letrec or letrec* the expression looks like (lambda (arg ...) expression).

If you take any Scheme expression like (if (positive? x) - +) and surround them with an extra pair of parenthesis you are calling the result as a procecdure: ((if (positive? -1) - +)) ; ==> 0

An if works like this:

(if predicate-expression
    consequent-expression
    alternative-expression)

If you need to nest you need to have the nested if as alternative expression.

(if predicate-expression
    consequent-expression
    (if predicate-expression2
        consequent-expression2
        alternative-expression))

If you nest there cond might be a better match. The expression above might be written:

(cond (predicate-expression consequent-expression)
      (predicate-expression2 consequent-expression)
      (else alternative-expression))

else has special meaning in a cond so it's not something that works on its own.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • I'm not good with learning from general examples. Do you have any specifics? Why is there considered to be no body? – Zack Schwartz Mar 17 '15 at 10:36
  • @ZackSchwartz Because the `let`-expression only contains the binding of a name. It also requires a body, which is the value of the expression. `(let ((x 5) (y 3)) (+ x y))` evaluates to 8, but `(let ((x 5) (y 3)))` is invalid. You appear to be trying to write C and treating the `let`-variants as variable declaration blocks. That's not how Scheme works, and your best course of action is probably to forget what you already know about programming and start re-learning by writing smaller Scheme programs than this. And don't use `set!`. – molbdnilo Mar 17 '15 at 10:56
  • So how do I declare variables? – Zack Schwartz Mar 17 '15 at 11:17
  • `(let ((varname1 (+ 4 5)) (varname2 (list 4 5)) (varname3 100)) (list varname1 varname2 varname3))` has 3 variables and lists the 3 in the body. The variables don't exist outside the body. – Sylwester Mar 17 '15 at 12:00
  • 1
    @halfer It's a typo.. *ø* is next to *l* in the norwegian keyboard so I guess I have touched it when I pressed l. fixed now :-) – Sylwester Oct 25 '15 at 20:55