;; -- Common Lisp version of Wolfram's experiment -- ;; ;; Simply run the experiment with this line of code: ;; (run-it '(0 1 1 0 1 0 0 0) 40) ;; ;; Change the set of rules: try different rules ;; to see how it affects the result. ;; (0 1 1 1 1 0 0 0) is one of the seemingly random ;; outputs. ;; ;; You can also change the # of iterations, also ;; the ouptut is raw ASCII, so you have to extend ;; the width of your terminal to see something... (defun print-line (line) (loop for i from 0 to (- (length line) 1) do (if (eq (nth i line) 0) (format t ".") (format t "H"))) (format t "~%")) (defun next-line (line rule) (let ((n-line (copy-list line)) (state 0)) (loop for i from 1 to (- (length line) 2) do (progn (setf state (+ (* 4 (nth (- i 1) line)) (* 2 (nth i line)) (* 1 (nth (+ i 1) line)))) (dotimes (rule-idx 8) (if (eq state rule-idx) (setf (nth i n-line) (nth rule-idx rule)))))) n-line)) (defun run-it (rule iterations) (let ((current-line ()) (width (+ 1 (* iterations 2)))) (dotimes (i width) (setf current-line (append current-line '(0)))) (setf (nth (floor (/ width 2)) current-line) 1) (print-line current-line) (dotimes (i iterations) (setf current-line (copy-list (next-line current-line rule))) (print-line current-line))))