;; Fun with macros and loops ;; Implementation of a C-like for loop facility (defmacro for-continue () `(go loop-continue)) (defmacro for-break () `(go loop-break)) (defmacro for-loop ((&rest initializations) condition (&rest iterations) &body operations) `(block nil (let ,(loop for n in initializations collect `(,@n)) (tagbody loop (if (not ,condition) (go loop-break)) (progn ,@operations) loop-continue (progn ,@iterations) (go loop) loop-break)))) ;; Iterating integer (for-loop ((i 0)) (< i 10) ((incf i)) (print i)) ;; Iterating over a list (for-loop ((n (list 1 2 3))) (not (null n)) ((setf n (rest n))) (print (first n))) ;; Iterating over both a list repeatedly and an integer limit (defvar *l*) (setf *l* (list 1 2 3)) (for-loop ((i 0) (n *l*)) (< i 256) ((incf i) (setf n (rest n))) (if (null (first n)) (setf n *l*)) (print (list i (first n))))