blob: b2e75c466316b22eb0a00633bafac73d39805f73 (
plain)
1
2
3
4
5
6
7
8
9
10
11
|
<h3 class="section">A Simple Example of a Macro</h3> <p>Suppose we would like to define a Lisp construct to increment a variable value, much like the <code>++</code> operator in C. We would like to write <code>(inc x)</code> and have the effect of <code>(setq x (1+ x))</code>. Here’s a macro definition that does the job: </p> <div class="example"> <pre class="example">(defmacro inc (var)
(list 'setq var (list '1+ var)))
</pre>
</div> <p>When this is called with <code>(inc x)</code>, the argument <var>var</var> is the symbol <code>x</code>—<em>not</em> the <em>value</em> of <code>x</code>, as it would be in a function. The body of the macro uses this to construct the expansion, which is <code>(setq x (1+ x))</code>. Once the macro definition returns this expansion, Lisp proceeds to evaluate it, thus incrementing <code>x</code>. </p> <dl> <dt id="macrop">Function: <strong>macrop</strong> <em>object</em>
</dt> <dd><p>This predicate tests whether its argument is a macro, and returns <code>t</code> if so, <code>nil</code> otherwise. </p></dd>
</dl><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Simple-Macro.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Simple-Macro.html</a>
</p>
</div>
|