Palindrome in Lisp

Here’s my version of palindrome for lisp problems 99 no 06 found here


(defun last1 (lst)
  (car (last lst)))

(defun palin1 (lst)
  (if (equal nil lst)
  t
  (let ((start (first lst)) (finish (last1 lst)))
    (if (equal start finish)
    (palin1 (rest (butlast lst)))
    nil))))

5 thoughts on “Palindrome in Lisp

  1. an iterative approach

    (defun make-palindrome(list)
    (if (= (length list) 1)
    list
    (dotimes (count (length list) list)
    (setf list (append list (list (nth (- (length list) (+ count (+ count 1))) list))))
    ))
    )

  2. A solution using mapcar and reduce

    (defun is-palindrome (lst)
    (reduce #'(lambda (x y) (if (and x y) t nil))
    (mapcar #’eql lst (reverse lst))))

Leave a reply to Rajesh Cancel reply