I’ve been playing around with (and also learning) common lisp recently and found its macro system very intriguing. So I started to implement some standard imperative control structures that closely resemble those found in most imperative mainstream programming languages like Java or C.
Here’s what I’ve done so far:
while macro:
1 2 3 4 5 6 7 8 | ;; example: ;; (defparameter x 0) ;; (while (< x 10) (print x) (incf x)) ;; output numbers 0 - 9. (defmacro while (cond &body body) `(if ,cond (progn ,@body (while ,cond ,@body)) nil)) |
until macro
1 2 3 4 5 6 | ;; example: (until (= x 10) (print x) (incf x)) (defmacro until (cond &body body) `(if (not ,cond) (progn ,@body (until ,cond ,@body)) nil)) |
for macro
1 2 3 4 5 6 7 | ;; example: ;; (for ((i 0) (< i 10) (incf i)) ;; (print i)) (defmacro for (((var init-value) cond step-block) &body body) `(do ((,var ,init-value ,step-block)) ((not ,cond)) ,@body)) |
I started this yesterday and I’ll try to come up with some more. Lets see what else i can come up with ![]()
Not that this is particularly useful or anything since you really don’t need them, but it’s still a nice way to learn the language and especially how macros work. I’ll just keep it as a little exercise to myself in order to get to know common lisp in greater detail.


2 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
Also wenn das mit den Makros so geht (was ich dir natürlich so glaube!
), sind die ja um einiges geiler als in C! sind ja quasi, wie ich das so sehe, eigene komplexe funktionen und mehr…
until und while funktionieren auch nicht. Ein Lisp Compiler kann Funktionen, die diese Macros nicht übersetzen -> Endlosrekursion.
macroexpand while -> macroexpand while -> macroexpand while …
Als Literatur zu Macros: On Lisp von Paul Graham. Gibt es auch umsonst als PDF.