summaryrefslogtreecommitdiff
path: root/devdocs/elisp/anonymous-functions.html
blob: 97bc463c976713b9e82a74235163a7f68bc8aabd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 <h3 class="section">Anonymous Functions</h3>  <p>Although functions are usually defined with <code>defun</code> and given names at the same time, it is sometimes convenient to use an explicit lambda expression—an <em>anonymous function</em>. Anonymous functions are valid wherever function names are. They are often assigned as variable values, or as arguments to functions; for instance, you might pass one as the <var>function</var> argument to <code>mapcar</code>, which applies that function to each element of a list (see <a href="mapping-functions">Mapping Functions</a>). See <a href="accessing-documentation#describe_002dsymbols-example">describe-symbols example</a>, for a realistic example of this. </p> <p>When defining a lambda expression that is to be used as an anonymous function, you can in principle use any method to construct the list. But typically you should use the <code>lambda</code> macro, or the <code>function</code> special form, or the <code>#'</code> read syntax: </p> <dl> <dt id="lambda">Macro: <strong>lambda</strong> <em>args [doc] [interactive] body…</em>
</dt> <dd>
<p>This macro returns an anonymous function with argument list <var>args</var>, documentation string <var>doc</var> (if any), interactive spec <var>interactive</var> (if any), and body forms given by <var>body</var>. </p> <p>Under dynamic binding, this macro effectively makes <code>lambda</code> forms self-quoting: evaluating a form whose <small>CAR</small> is <code>lambda</code> yields the form itself: </p> <div class="example"> <pre class="example">(lambda (x) (* x x))
     ⇒ (lambda (x) (* x x))
</pre>
</div> <p>Note that when evaluating under lexical binding the result is a closure object (see <a href="closures">Closures</a>). </p> <p>The <code>lambda</code> form has one other effect: it tells the Emacs evaluator and byte-compiler that its argument is a function, by using <code>function</code> as a subroutine (see below). </p>
</dd>
</dl> <dl> <dt id="function">Special Form: <strong>function</strong> <em>function-object</em>
</dt> <dd>
 <p>This special form returns <var>function-object</var> without evaluating it. In this, it is similar to <code>quote</code> (see <a href="quoting">Quoting</a>). But unlike <code>quote</code>, it also serves as a note to the Emacs evaluator and byte-compiler that <var>function-object</var> is intended to be used as a function. Assuming <var>function-object</var> is a valid lambda expression, this has two effects: </p> <ul> <li> When the code is byte-compiled, <var>function-object</var> is compiled into a byte-code function object (see <a href="byte-compilation">Byte Compilation</a>). </li>
<li> When lexical binding is enabled, <var>function-object</var> is converted into a closure. See <a href="closures">Closures</a>. </li>
</ul> <p>When <var>function-object</var> is a symbol and the code is byte compiled, the byte-compiler will warn if that function is not defined or might not be known at run time. </p>
</dd>
</dl>  <p>The read syntax <code>#'</code> is a short-hand for using <code>function</code>. The following forms are all equivalent: </p> <div class="example"> <pre class="example">(lambda (x) (* x x))
(function (lambda (x) (* x x)))
#'(lambda (x) (* x x))
</pre>
</div> <p>In the following example, we define a <code>change-property</code> function that takes a function as its third argument, followed by a <code>double-property</code> function that makes use of <code>change-property</code> by passing it an anonymous function: </p> <div class="example"> <pre class="example">(defun change-property (symbol prop function)
  (let ((value (get symbol prop)))
    (put symbol prop (funcall function value))))
</pre>

<pre class="example">(defun double-property (symbol prop)
  (change-property symbol prop (lambda (x) (* 2 x))))
</pre>
</div> <p>Note that we do not quote the <code>lambda</code> form. </p> <p>If you compile the above code, the anonymous function is also compiled. This would not happen if, say, you had constructed the anonymous function by quoting it as a list: </p> <div class="example"> <pre class="example">(defun double-property (symbol prop)
  (change-property symbol prop '(lambda (x) (* 2 x))))
</pre>
</div> <p>In that case, the anonymous function is kept as a lambda expression in the compiled code. The byte-compiler cannot assume this list is a function, even though it looks like one, since it does not know that <code>change-property</code> intends to use it as a function. </p><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 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/Anonymous-Functions.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Anonymous-Functions.html</a>
  </p>
</div>