Chriskt
Chriskt

Reputation: 87

Syntax error on Scheme tail-recursive function

I'm creating a tail recursive function that evaluates a polynomial by passing a list of coefficients and an x value.

example: evaluate x^3 + 2x^2 + 5, so the user would pass the list '(5 0 2 1) and an x like 1 in a functional call (poly '(5 0 2 1) 1).

I can't figure out why i'm getting the following error:

if: bad syntax in: (if (null? (cdr lst)) (+ total (car lst)) eval-poly-tail-helper ((cdr lst) x (+ (* (expt x n) (car lst)) total) (+ 1 n)))

(define (poly lst x)
  (poly-assistant lst x 0 0))


(define (poly-assistant lst x total n)
   (if (null? (cdr lst))
      (+ total (car lst))
      poly-assistant((cdr lst) x (+ (* (expt x n) (car lst)) total) (+ 1 n))))

Upvotes: 0

Views: 187

Answers (1)

You need a left paren before poly-assistant in the last line.

In Scheme, function applications start with a left paren. And if takes 2 or 3  operands.

Use a better editor (e.g. emacs) able to match parenthesis.

The two left parenthesis before cdr looks suspicious. You might need only one.

Learn to use your Scheme debugger, or at least add debugging prints.

Upvotes: 1

Related Questions