Once upon a time I was playing with macros and came up with this:
(defmacro my-recursive-fact (n)
(if (= 0 n) '1
(let ((m (1- n)))
`(* ,n (my-recursive-fact ,m)))))
And it worked.
CL-USER> (my-recursive-fact 5)
120
So then I thought it could be a nice way to show students an example of recursion, if I expand this macro using macroexpand
:
CL-USER> (macroexpand '(my-recursive-fact 5))
(* 5 (MY-RECURSIVE-FACT 4))
T
That is, no difference between macroexpand-1
and macroexpand
in this case. I'm sure that I'm missing some crucial point in understanding macroexpand
, and HyperSpec says nothing special about recursive macros.
And also I'm still curious to know if there is a way to expand such kind of macro to it's end.
Slime has a code-walking slime-macroexpand-all
command: http://common-lisp.net/project/slime/doc/html/Macro_002dexpansion.html
This is probably undocumented and/or unsupported, but maybe you can call it from the REPL:
CL-USER> (swank-backend:macroexpand-all '(my-recursive-fact 5))
(* 5 (* 4 (* 3 (* 2 (* 1 1)))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With