1 / 3
Jul 2010

I am receiving runtime error (NZEC) while trying to solve the PALIN problem with LISP but I couldn't find informations about what this error is about.

Could someone help me?

My code follows:

(defun half-floor (lst)
  (floor (1- (length lst)) 2))
(defun half-ceil (lst)
  (ceiling (1- (length lst)) 2))
(defun str-dig-list-p (str)
  (loop for c across str collect (digit-char-p c)))
(defun list-str-dig-p (digit-list)
  (with-output-to-string (out)
    (format out "~{~a~}" digit-list)))
(defun mirror (digit-list)
  (replace (reverse digit-list) digit-list :end1 (floor (length digit-list) 2)))
(defun next-magic-palindrome (digit-list pos1 pos2)
  (cond ((< pos1 0)
         (setf (elt digit-list (1- (length digit-list))) 1)
         (append '(1) digit-list))
        ((< (elt digit-list pos1) 9)
         (setf (elt digit-list pos2) (1+ (elt digit-list pos1)))
         (setf (elt digit-list pos1) (elt digit-list pos2))
         digit-list)
        (t
         (setf (elt digit-list pos1) 0)
         (setf (elt digit-list pos2) 0)
         (next-magic-palindrome digit-list (1- pos1) (1+ pos2)))))
(defun greater-than (digit-list number)
  (let ((number-dl (list-str-dig-p digit-list)))
    (string> number-dl number)))
(defun next-palindrome (number)
  (let ((digit-list (mirror (str-dig-list-p number))))
    (list-str-dig-p (if (greater-than digit-list number)
                        digit-list
                        (next-magic-palindrome digit-list
                                               (half-floor digit-list)
                                               (half-ceil digit-list))))))
(dotimes (i (parse-integer (read-line)))
  (format t "~a~%" (next-palindrome (read-line))))
  • created

    Jul '10
  • last reply

    Jul '10
  • 2

    replies

  • 629

    views

  • 2

    users