The less-familiar parts of Lisp for beginners — macro-function

Next, we arrive at macro-function.  This accessor allows the programmer to read or modify the function field of a symbol in the context of macros.  Recall from this earlier discussion that a symbol has five associated fields, one of which is a function field.  In fact, the function field can reference either a function or a macro.  The fdefinition accessor reads or modifies this field in the general case, while macro-function limits its operations to the context of macros.

If macro-function is invoked on a symbol name that is not an interned symbol, or on a symbol whose function field references a function, then it returns nil.  On the other hand, if fdefinition is invoked on a symbol name or object that is not interned, it raises a condition of type undefined-function.  Here is a transcript of the cases:
[/raw]

 

CL-USER> (defun my-function (x) (+ 1 x))
MY-FUNCTION
CL-USER> (defmacro my-macro (x) `(list 1 ,x))
MY-MACRO
CL-USER> (fdefinition 'my-function)
#<FUNCTION MY-FUNCTION>
CL-USER> 
(fdefinition 'my-macro)
#<CLOSURE (LAMBDA (&REST SB-C::ARGS) :IN MACRO-FUNCTION) {1006CDBFCB}>
CL-USER> (fdefinition 'non-existent-symbol)
; Evaluation aborted on #<UNDEFINED-FUNCTION NON-EXISTENT-SYMBOL {1002E4D3E3}>.
CL-USER> (let ((sym (make-symbol "ABC")))
           (setf (fdefinition sym) (fdefinition 'my-function))
           (fdefinition sym))
#<FUNCTION MY-FUNCTION>
CL-USER> (macro-function 'my-function)
NIL
CL-USER> (macro-function 'my-macro)
#<FUNCTION (MACRO-FUNCTION MY-MACRO) {1005A3A46B}>
CL-USER> (macro-function 'non-existent-symbol)
NIL

[/raw]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.