Search code examples
lispcommon-lispundefined-function

Undefined function in lisp


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.


Solution

  • 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).