0

I am writing a lisp function and I keep getting EVAL - undefined function x when I try to return the value of x from the function.

(defun p+ (x y)
    (recurcollect (gluelist x y) '()))

(defun isinlist (x y)
    (if (car y)
        (if (equal (cdr x) (cdar y))
            t
            (if (cdr y)
                (isinlist(x (cdr y))) 
                NIL))
        NIL))

(defun collectvalue (x y) ;takes an expression and a list and returns the sum of all like expressions from the list
    (if (equal x NIL)
        (print '(x is NIL))
        (if (equal (cdr x) (cdar y)) 
             (if (cdr y)
                (collectvalue (list (+ (car x) (caar y)) (cdr x)) (cdr y))
                (list (+ (car x) (caar y)) (cdr x)))
        (if (cdr y)
            (collectvalue x (cdr y))
            x))))

(defun recurcollect (x y) ;returns a flat list of collected expressions
    (if (isinlist (car x) y)
        (recurcollect (cdr x) y)
        (if (cdr x)
            (recurcollect x (cons y (collectvalue (car x) (cdr x))))
            (cons y (car x)))))

(defun gluelist (x y)
    (if (cdr x)
        (cons (car x) (gluelist (cdr x) y))
        (cons (car x) y)))

(print (p+ '(2 0 1) '(4 0 1)))  ;(6 0 1)

I believe the error is caused by the x at the end of the function but I cant see why, as far as I can tell my brackets are correctly paired up and I cant see why it is trying to evaluate x as a function.

6
  • Are you getting this problem when you try to define the function, or when you try to run it? What exactly are you evaluating at the REPL? Commented Dec 11, 2014 at 22:01
  • @L33tminion It happens when I run it. What I'm evaluating is polynomials, each value is represented as (coefficient, power of first variable, power of 2nd variable, etc.) so 3x^2 would be (3 2) 3xy^2 would be (3 1 2) so on. x is a single term and y is a list of terms representing a polynomial. The purpose of this function is to collect all the like terms. Commented Dec 11, 2014 at 22:05
  • 1
    please format your code properly (emacs will do that for you) and copy and paste your complete interaction, i.e., how you are calling your function and what Lisp prints. Commented Dec 11, 2014 at 22:13
  • @sds I dont have access to emacs as I'm using a public computer, could you tell me which areas of my formatting need fixing? Commented Dec 11, 2014 at 22:15
  • What exactly are you running at the REPL? Can you copy and paste the exact thing? Commented Dec 11, 2014 at 22:23

1 Answer 1

2

The problem is in isinlist which is called recursively as (isinlist(x (cdr y))). (x ...) is interpreted as a function call of function x. You probably want (isinlist x (cdr y)) instead.

Incidentally, you can replace isinlist with member (with :key #'cdr :test #'equal).

Sign up to request clarification or add additional context in comments.

2 Comments

I think you may be right, but member wont work due to the nature of the input, it needs to test the equality of the cdr only.
I added proposed member keyword args

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.