Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Lisp recursive macro expansion

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.

like image 257
alexey.e.egorov Avatar asked Nov 25 '13 07:11

alexey.e.egorov


1 Answers

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)))))
like image 136
Lars Brinkhoff Avatar answered Sep 19 '22 06:09

Lars Brinkhoff