0

I am writing a function called palindrome that tests if a list is a palindrome. It works 100% for regular lists such as (1 2 1), but if I use (1 (2) 1), I get a bad argument type error.

Here is my function

(defun palindrome (x)                      
   (if (NULL x) t                           
   (let ( ( a (car x)) (b (lastelement x)))  
      (if ( = a b)                           
        (palindrome (cdr (butlast x)))       
      nil))))     

1 Answer 1

1

The function = is reserved exclusively for numbers. When used with a list or other type of element it returns an error. Use instead eql.

Incidentally, note that your function is very inefficient. Normally (i.e. not for homework) you would create a reversed copy of the list and compare it element by element with the original list.

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

2 Comments

Thank you very much. You caught me red handed as this is a homework question XD, and my instructor told us to not use reverse.
I am very new to lisp, and I completely forgot about the difference between the = and eq operator. Thank you!

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.