;;; Gray code ;;; Make it with lists first, then make string from list ;;; Way of making it: for n = 0 it is the empty list ;;; For larger n, add 0 to code for n-1, and add 1 to reverse (defun gray (n) (mapcar #'list-to-string (gray-list n)) ) (defun list-to-string (lista) (let ((a (make-string (length lista) :initial-element #\0))) (dotimes (i (length lista) a) (setf (aref a i) (elt lista i)) ) ) ) (defun gray-list (n) (if (= n 0) '(()) (let ((prev (gray-list (1- n)))) (append (add #\0 prev) (add #\1 (reverse prev)) ) ) ) ) ;;; Adding a digit to a previous code word (defun add (digit word) (mapcar #'(lambda (x) (cons digit x)) word) )