One fun aspect of macros are that they give you the possibility of expanding the syntax of your lisp and adding new syntactic features to it, and this happens only because arguments passed to a macro will get evaluated only at runtime, and not at the time of compiling your macro. As an example the first/last thread macros in clojure -> ->>
don't evaluate their expression-arguments, otherwise these evaluated results couldn't accept anything more (say (+ 3) => 3
and 3
is not a function which could accept your first main argument in something like (-> 4 (+ 3))
).
Now if i like this syntax and i want to add it to my Common Lisp implementation (which doesn't have it) i can add it up by defining a macro myself. Something like this:
;;; in SBCL
;;; first thread:
(defmacro -> (x &rest more)
(loop for m in more
for n = `(,(first m) ,x ,@(rest m)) then `(,(first m) ,n ,@(rest m))
finally (return n)))
;;; last thread:
(defmacro ->> (x &rest more)
(loop for m in more
for n = `(,(first m) ,@(rest m) ,x) then `(,(first m) ,@(rest m) ,n)
finally (return n)))
Now i would be able to use them in Common Lisp the same way as in Clojure:
(-> #'+
(mapcar '(2 3 4) '(1 2 3))) ;; => (3 5 7)
(->> #'>
(sort '(9 8 3 5 7 2 4))) ;; => (9 8 7 5 4 3 2)
Also maybe you want to have a new syntax for clojure's range
function with your own keywords for your syntax, something like:
(from 0 to 10) ;=> (0 1 2 3 4 5 6 7 8 9)
(from 0 to 10 by 0.5) ;;=> (0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5)
can be defined like this macro:
(defmacro from
"Just another range!"
[x y z & more]
`(when (= '~y '~'to)
(if '~more (range ~x ~z (second '~more))
(range ~x ~z))))
or add something similar to Common Lisp (it is only for example since all these can already be done in the languages of course!):
(defmacro from (x y z &rest r)
`(when (eql ',y 'to)
(if (and ',r (eql (first ',r) 'by))
(loop for i from ,x to ,z by (second ',r) collect i)
(loop for i from ,x to ,z collect i))))
(from 0 to 10) ;=> (0 1 2 3 4 5 6 7 8 9 10)
(from 0 to 5 by 1/2) ;=> (0 1/2 1 3/2 2 5/2 3 7/2 4 9/2 5)