3

I'm trying to write a recursive palindrome function. The code works using two function as follows: (set str(a b c d))

(defun reverseString (l)
    (cond
        ( (null l) nil)
            (T (append (reverseString (cdr l)) (list (car l))))
    )
)

(defun palindrome (l)
    (cond
        ( (null l) nil)
             (T (append l(reverseString (cdr l)) (list (car l))))
    )
)

However, I'm trying to combine it into a single function:

(defun palindrome (l)
    (cond
        ( (null l)
                nil
        )
        (T 
            (append str(append (palindrome (cdr l)) (list (car l))) )
        )
    )
)

This returns (A B C D A B C D A B C D A B C D D C B A)

Where I want it to return (a b c d d c b a) and then eventually (a b c d c b a) **not repeating the last character when it reverses.

I know there are easier ways to do this we predefined functions, but I'm trying to challenge myself a bit. However I'm stuck here, and help would be greatly appreciated.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
nikebOxer
  • 33
  • 4

1 Answers1

4

Here is a recursive, single function palindrome:

(defun palindrome(l)
  (cond ((null l) nil)
        (t (append (list (car l)) (palindrome (cdr l)) (list (car l))))))

The recursion is structured in this way: make a palindrome of the rest of the list, and put at the beginning and at the end the first element of the list.

If you want to have the central element only once, here is an alternative version:

(defun palindrome(l)
  (cond ((null l) nil)
        ((null (cdr l)) (list (car l)))
        (t (append (list (car l)) (palindrome (cdr l)) (list (car l))))))

that is, you have to add a new case for the termination of the recursive function: terminate also when there is only one element, and return that element.

Renzo
  • 26,848
  • 5
  • 49
  • 61
  • thank you, I would upvote but I dont have the reputation yet. Then I just have to take the CDR of the evaluated list to accomplish the second funtion where it returns A B C D C B A? – nikebOxer Jun 21 '15 at 14:15
  • Your first definition of palindrome was not recursive and solved the problem by simply concatenating the list with its reverse. Your second palindrome function instead is recursive, because it calls itself. It was almost correct, the only things to do was substitute `str` with `(list (car l))`, since `str` is a constant value (the list that you want to make palindrome), while in calling a function that build recursively some structure you should use only the parameters. I think you could mark the answer as solution to your question, even if you do not have reputation for upvoting, thanks! – Renzo Jun 21 '15 at 14:23
  • Awesome, I'm still confused as to how to get it to return A B C D C B A. I'm new to recursion but I know I essentially need to take the CDR of the list im appending to A B C D right? – nikebOxer Jun 21 '15 at 14:41
  • I changed the answer to provide the alternative version that maintains the central element only once. When writing a recursive function, one should pay attention to two foundamental things: how the recursion is “patterned” over the data, and the ways in which the recursion terminates. In this case, the termination of the function changes completely the result. – Renzo Jun 21 '15 at 14:51