Benz
Benz

Reputation: 289

Unbound Variable error in Lisp

I am trying to do a function that removes duplicates from a list using Common Lisp on LispWorks.

So I did two functions. The first one "remove-e" removes an element from a list and a second one "remove-rep" uses the first one to return a list without duplicates.

Here is my code for the first one:

(defun remove-e (L e)
  (if (null L)
    L
    (if (= e (car L))
      (remove-e (cdr L) e)
      (cons (car L) (remove-e (cdr L) e)))))

It works good when given a list of numbers but when I give it letters, I get this error:

(remove-e '(a a b a d a g a h t) a)

Error: The variable A is unbound.

For my second function:

(defun remove-rep (l)    
  (if (null l)
    l
    (cons (car l)
          (remove-rep (remove-e (cdr l) (car l))))))

This is the error message that I get when trying to test it:

CL-USER 12 : 6 > (remove-rep '(1 2 3 1 5 1 1))

Error: The variable   is unbound.

I saw that there are some similar questions, but couldn't find the common points of my program with these: Unbound variable in Lisp , Unbound variable in Common Lisp

Upvotes: 0

Views: 2997

Answers (1)

Barmar
Barmar

Reputation: 780655

remove-e only works with a list of numbers, because you're using = to compare elements. If you want to be able to work with symbols as well, use eql:

(defun remove-e (L e)
  (if (null L) L
    (if (eql e (car L)) (remove-e (cdr L) e)
      (cons (car L) (remove-e (cdr L) e)))))

Then when you call it, you have to quote the symbol argument, to prevent it from being evaluated as a variable.

(remove-e '(a a b a d a g a h t) 'a)

The problem in remove-rep is that you somehow typed some non-printing, multi-byte characters at the end of the first line. It works correctly with those characters removed. Try retyping the function, or just copy/paste this:

(defun remove-rep (l)
  (if (null l) l
    (cons (car l) (remove-rep (remove-e (cdr l) (car l))))))

BTW, Lisp programmers don't generally put ) on new lines like you do, they put them at the end of the line like this.

Upvotes: 3

Related Questions