How to Create a Lisp Macro
You can grow and extend the power of the Lisp programming language through the use of macros. Lisp macros apply a name to a combination of existing parts of the Lisp language, Lisp libraries or functions you write to create a new language feature that will evaluate like any other Lisp language component. Macros are a form of shorthand --- when you evaluate a macro, the interpreter expands the macro into the code it encapsulates, then executes that code. An example of an Lisp macro you can create is a Fahrenheit to Celsius temperature conversion utility. This example shows how a macro can combine another macro and a Lisp function.
Instructions
-
-
1
Launch the command line terminal by clicking the "Start" button and clicking on "Run" menu option. Type "command" into the input box, then click "OK."
-
2
Start the Common Lisp interpreter by typing "clisp" at the command prompt. Press the "Enter" key.
-
-
3
Type the code for the first part of the application at the Lisp command prompt. Your new macro consists of another macro called Ratio. The Defmacro command creates the macro, which contains the slope of the plot of Celsius versus Fahrenheit.
(defmacro ratio () (/ 5.0 9.0))
-
4
Type the code for the second part of the macro at the Lisp command prompt. The function "diff" subtracts 32 from the temperature in Fahrenheit.
(defun diff (x) (- x 32.0))
-
5
Type the code for the final part of the macro at the Lisp command prompt. The macro "fc" accepts the temperature in Fahrenheit, multiplies it by the ratio, multiplies it by "diff," then prints the result.
(defmacro fc (x) (* (ratio) (diff x)))
-
6
Test the macro by typing "(fc 98.6)" at the Lisp command prompt. The interpreter should print 37.0, which is 98.6 or body temperature in degrees Celsius.
-
1
Tips & Warnings
You can write your Lisp code with a plain text editor and load it into the Common Lisp interpreter. Enter the following code into your text editor by typing it in or selecting the code with the mouse, pressing "Ctrl" plus "C," then pasting it into the editor with a "Ctrl" plus "V" command.
(defmacro ratio () (/ 5.0 9.0))
(defun diff (x) (- x 32.0))
(defmacro fc (x) (* (ratio) (diff x)))
Save the file under the name "test.lisp" and restart your Common Lisp interpreter. At the Lisp command prompt, type the following line then press the "Enter" key:
(load "test.lisp")
If you get the error message "DEFUN/DEFMACRO(RATIO): #<PACKAGE COMMON-LISP> is locked, " type "(continue)" at the command line and press "Enter." Finally, type "(fc 110)" at the command prompt and press "Enter." The result should be 43.333336.
A typical Lisp program can have many levels of nested parentheses. If you omit a parenthesis, your program will not run. You can avoid missing parentheses by writing and editing your programs in a text editor. Another way avoid programming errors is to spread your code over multiple lines and use indentation to set apart logical units of code. For example, the macro "fc" is easier to follow and debug if you type it in as below (omit the periods, which are there for proper placement):
(defmacro fc (x)
.... (*
.......(ratio)
.......(diff x)
.....)
)
References
Resources
- Photo Credit Stockbyte/Stockbyte/Getty Images