0
votes

So I'm trying to recursively find palindromes, I've generated a huge nested list that looks like so '((n n n)(n n n)(n n n)) Where n is numbers ofc using the function:

(defun double-lst (lst1 lst2)
  (mapcar #'(lambda (n)
               (multiply-lst n lst2))
          lst1))

the list is absolutely huge. I'm trying to write a recursive function that searches the car and cdr of my list using the template:

    (DEFUN func (X)
     (COND (end-test-1 end-value-1)
           (end-test-2 end-value-2)
           (T (combiner (func (CAR X))
                        (func (CDR X))))))

But I can't seem to get my version or anything I've tried to work. I've tried stuff along the lines of this:

(defun find-palindrome (lst)
  (cond ((equal lst (reverse lst)) lst)
        (t (or (find-palindrome (car lst))
               (find-palindrome (cdr lst))))))

The function find-palindrome is passed (reverse lst) so that it works from the highest values first returning the first palindrome, but I don't know how to get the recursive function to do its stuff. I'd really appreciate an in depth explanation on how to write car/cdr recursion. Thanks :) EDIT: my crappy find-palindrome returns this:

    CL-USER> (find-palindrome '((1 2 3 4)(13 4 51 3) (1 2 4 9009)))
; Evaluation aborted on #<TYPE-ERROR expected-type: SEQUENCE datum: 1>.

I'd expect it to return (9009).

1
Can you provide some examples of calling find-palindrome and what you would expect it to return? - Scott Hunter
All done. I was supposed to do it, but I forgot. - Floofk
Lisp shows a backtrace. Did you look at it? Have tried to trace the function calls? - Rainer Joswig

1 Answers

1
votes

Your template does not apply to this problem; (car lst) isn't necessarily a list, but the argument to find-palindrome is expected to be. Or at least you need to be sure (car lst) is a list before passing it to find-palindrome.

Similarly, you seem to want to see if the digits of a number are a palindrome; reverse is meant to reverse the elements of a list, not the digits of a number.