"until" loop - My first elisp macro
Recently I was reading On Lisp by Paul Graham and got interested in using macros in Emacs Lisp. I decided to play with it.
Here’s my first macro: a
(defmacro until (test &rest body) `(while (not ,test) ,@body))
until as a negative
while. The loop runs until the test becomes true.
To use it:
(setq jz/counter 1) (setq jz/res '()) (until (>= jz/counter 5) (setq jz/counter (+ 1 jz/counter)) (setq jz/res (cons jz/counter jz/res))) ;; jz/res => (5 4 3 2)
One note about defining macros: If we want to splice the rest of the lisp statement, we need two things:
- Add a
&restin the macro argument List. It means that there are arbitrary number of statements there, and we wrap them all into
- use comma-at, or
,@to splice the
body. It is similar to the spread operator
I’m not sure whether this is the most efficient way to define macros. But it definitely kicks off an interesting adventure!