Is it possible in Lisp to undefine Macros and Functions?
Asked Answered
A

2

15

While using the REPL it would be helpful to undefine defined functions and macros, exspecially if you tried to make a macro for something, and then simulate it as function, and the macro is called everytime. Is it possible in Common Lisp to undefine?

Author answered 26/2, 2014 at 0:5 Comment(1)
Also see How to delete variable/forms in Lisp?Counterword
F
20

Yes, you can use fmakunbound for this.

It works for both functions and macros. Here's an example REPL session:

CL-USER> (defun add (n m) (+ n m))
ADD        
CL-USER> (add 1 2)
3
CL-USER> (fmakunbound 'add)
ADD
CL-USER> (add 1 2)
; [snip]
; Evaluation aborted on #<UNDEFINED-FUNCTION ADD {C3305F1}>.

Note that it really is fmak rather than fmake. That still trips me up from time to time.

Farsighted answered 26/2, 2014 at 1:38 Comment(2)
In clisp-REPL I only type "(fm" and tabulator, so I don't have to know if fmak or fmake.Author
In my case, for some reason, the macro is still defined after calling fmakunbound. What could be the reason?Incidentally
W
12

Undefining a macro or function does not mean this change spreads through the code.

If you have a macro and want to redefine it as a function, then you also have to recompile the code which used the macro.

Note that if you compile code with certain optimizations (inlining), you need to recompile even more code. Even redefined functions might have no effect, until the using code also gets recompiled.

Wichern answered 26/2, 2014 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.