Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in Lisp to undefine Macros and Functions?

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?

like image 303
porky11 Avatar asked Feb 26 '14 00:02

porky11


People also ask

Is a macro not a function lisp?

Macros do code transformations at compile time or runtime. That's different from functions. Just look into the Common Lisp standard for many different pre-defined macros and their different syntax. Now think about, why these are macros and not functions.

How can you define macros in Lisp give an example?

Advertisements. Macros allow you to extend the syntax of standard LISP. Technically, a macro is a function that takes an s-expression as arguments and returns a LISP form, which is then evaluated.

How do Lisp macros work?

The Common Lisp macro facility allows the user to define arbitrary functions that convert certain Lisp forms into different forms before evaluating or compiling them. This is done at the expression level, not at the character-string level as in most other languages.


2 Answers

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.

like image 132
jbm Avatar answered Oct 06 '22 08:10

jbm


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.

like image 38
Rainer Joswig Avatar answered Oct 06 '22 08:10

Rainer Joswig