debugging simple LISP functions.

55 Views Asked by At

I am very new to lisp, and am having a hard time even getting my feet wet. I'm sure once, I have a few functions working, I'll be able to build upon them and work on higher order functions, and more complex problems.

Can someone point out my errors in the following code.

(defun indeHelper(A L N)
 (cond (Null N) nil)
 ((= A (first L) (cons N (indeHelper A (rest L) (+ 1 N)))))
 (t (indeHelper A (rest L) (+ 1 N))))

(defun inde(A L)
  (funcall indeHelper(A L 1)))

Also how would I call this? I have one function I think is working ok, but I can't get the syntax for calling it. Thanks for any help.

1

There are 1 best solutions below

1
On

You have a number of syntax issues.

The syntax of COND is:

(cond (test1 body1...)
      (test2 body2...)
      ...)

Your test1 is supposed to be (null n), and body1 should be nil, but you didn't wrap them in a level of parentheses. Then your other tests and bodies are outside cond, as should be apparent from the indentation.

It should be:

(defun indeHelper(A L N)
  (cond ((Null N) nil)
        ((= A (first L) (cons N (indeHelper A (rest L) (+ 1 N)))))
        (t (indeHelper A (rest L) (+ 1 N)))))

In the second function, you don't need to use funcall. That's used when you're calling a dynamically-determined function (e.g. when writing higher-order functions), but you're just calling a known function. It should just be:

(defun inde(A L)
  (indeHelper A L 1))

If you did need to use funcall, the syntax is:

(funcall someVariable A L 1)

When using funcall, the arguments aren't put into a nested list, they're just ordinary arguments to funcall.