Monthly Archives: June 2007

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))))

My implementation for factorial in Lisp. This is a response to a post found here.

Also, it is tail recursive.


(defun fact (n)
       (labels ((rec (n acc)
           (if (< n 2)
           acc
           (rec (1- n ) (* acc n)))))
         (rec n 1)))

ps: Factorial in Haskell seems easier though


fact 0 = 1
fact n = n * fact (n-1)

or


fact = product . enumFromTo 1

Edit: fixed the (< n 1) bug