How to check if a symbol is a macro
Asked Answered
P

1

6

In common lisp, I would like to be able to find out wether or not a symbol is a macro or not. Is there a predicate such as (macrop) which will allow me to detect if a name/symbol is a macro?

Pascia answered 2/6, 2019 at 20:4 Comment(3)
Nitpick: a symbol is never a macro. It may name a macro.Benford
@Svante: 'may' opens up an interesting can of worms: local&lexical macros where symbols are not direct names and local&lexical symbol macros, where symbols are kind macros...Ptah
@RainerJoswig: _may_be ^^Benford
P
6

If macro-function returns non-NIL, then it is a macro.

CL-USER 1 > (defmacro foo (bar) bar)
FOO

CL-USER 2 > (macro-function 'foo)
#<anonymous interpreted function 40600108FC>

Note that this works for typical global macros. There are also local&lexical macros, symbol macros, ...

Ptah answered 2/6, 2019 at 20:52 Comment(2)
I found this whilst trying to determine whether or not a symbol names a symbol macro. The marked answer doesn't work. The situtation is this: I am passed a symbol in a function. If it's a symbol macro I get the value with eval; if it's not, I get the value with symbol-value. To do that without error though, I need to know if it's a symbol-macro. Is there an equivalent to macro-function that works on symbol macros?Vittle
@Vittle : see the non-standard function VARIABLE-INFORMATION.Ptah

© 2022 - 2024 — McMap. All rights reserved.