VOOZH about

URL: https://www.geeksforgeeks.org/lisp/macros-in-lisp/

⇱ Macros in LISP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Macros in LISP

Last Updated : 18 Nov, 2021

Macro is similar to a function in popular languages like C++, java, etc. that takes arguments and returns a LISP form to be evaluated. It is useful when the same code has to be executed with a few variable changes.
For example, rather than writing the same code for squaring a number, we can define a macro that holds the code for squaring.

Defining a Macro:

To define a named macro in LISP, a default macro - defmacro is used.
Syntax:

(defmacro macroName (parameters)
 expressions
)

Where, 

  • macroName: It is the identifier of the macro, wherever it is used the macro expressions will be executed
  • parameters: Parameters that can be passed whenever calling a macro
  • expressions: Statements executed wherever the macro is used.

Example

Output:

Hello everyone
GfG
Hello everyone

Welcome "GfG audience" 
Welcome "Awesome people" 

7 + 18 : 25
Square of 5 is 25 
Comment