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.
Here is a recursive, single function palindrome:
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:
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.