Pergunta

I was playing around with macros today and saw the term macro-function appear in the REPL. I am familiar with Macros, compiler macros and reader macros but have not run into these.

CL-USER> (defmacro fnaa (&rest rest) `(lambda ,@rest))
  FNAA

CL-USER> #'fnaa
  #<CLOSURE (LAMBDA (&REST SB-C::ARGS) :IN MACRO-FUNCTION) {1003A6DD6B}>

I inspected 'fnaa and got this:

#<SYMBOL {1003A40A5F}>
--------------------
Its name is: "FNAA"
It is unbound.
It a macro with macro-function: #<FUNCTION (MACRO-FUNCTION FNAA) {1003A610BB}>
It is internal to the package: COMMON-LISP-USER 
Property list: NIL

I have read this from the CLHS but couldn't understand what it was and what it does

Any help illuminating macro-function's purpose would be greatly appreciated.

[EDIT] It seems from reading this that the function implements the expansion? Well that makes sense as a macro is really just a function that runs at macro-expansion time (and results in code) but if that is correct then why the terminology of "a macro with macro-function"?

I feel close but I'm still not quite getting it

[EDIT AGAIN] Ok so looking at the definition for macro I get this

macro n. 1. a macro form 2. a macro function. 3. a macro name. 

So is the macro the combination of these and thus the macro-function is just the implementation?

[EDIT MORE] Given the above, the CLHS entry for defmacro seems to make sense. Would anyone be able to confirm if I am on the right track here and also if it is possible to have a macro without a macro-function as, if not then I don't understand why it specifies the fact that it is a 'macro with a macro-function'

[FINAL EDIT] Is a macro a symbol with the function slot bound to a macro-function? (this wording is crap I could do with help here!)

CL-USER> (setf (macro-function 'jam) (lambda (x y) nil))

Inspect 'jam

#<SYMBOL {100400317F}>
--------------------
Its name is: "JAM"
It is a global variable bound to: NIL [unbind]
It a macro with macro-function: #<FUNCTION (LAMBDA (X Y)) {1005522FAB}> [unbind]
It is internal to the package: COMMON-LISP-USER [export] [unintern]
Property list: NIL
Foi útil?

Solução

Yes, macro-function is the function which is called on the code during macroexpansion and implements the macro.

See also my answer to another question.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top