summaryrefslogtreecommitdiff
path: root/devdocs/elisp/intro-eval.html
blob: 614cbf5849471540bb5b96fea6f32e3be36e5223 (plain)
1
2
3
4
5
6
 <h3 class="section">Introduction to Evaluation</h3> <p>The Lisp interpreter, or evaluator, is the part of Emacs that computes the value of an expression that is given to it. When a function written in Lisp is called, the evaluator computes the value of the function by evaluating the expressions in the function body. Thus, running any Lisp program really means running the Lisp interpreter. </p>     <p>A Lisp object that is intended for evaluation is called a <em>form</em> or <em>expression</em><a id="DOCF7" href="#FOOT7"><sup>7</sup></a>. The fact that forms are data objects and not merely text is one of the fundamental differences between Lisp-like languages and typical programming languages. Any object can be evaluated, but in practice only numbers, symbols, lists and strings are evaluated very often. </p> <p>In subsequent sections, we will describe the details of what evaluation means for each kind of form. </p> <p>It is very common to read a Lisp form and then evaluate the form, but reading and evaluation are separate activities, and either can be performed alone. Reading per se does not evaluate anything; it converts the printed representation of a Lisp object to the object itself. It is up to the caller of <code>read</code> to specify whether this object is a form to be evaluated, or serves some entirely different purpose. See <a href="input-functions">Input Functions</a>. </p>  <p>Evaluation is a recursive process, and evaluating a form often involves evaluating parts within that form. For instance, when you evaluate a <em>function call</em> form such as <code>(car x)</code>, Emacs first evaluates the argument (the subform <code>x</code>). After evaluating the argument, Emacs <em>executes</em> the function (<code>car</code>), and if the function is written in Lisp, execution works by evaluating the <em>body</em> of the function (in this example, however, <code>car</code> is not a Lisp function; it is a primitive function implemented in C). See <a href="functions">Functions</a>, for more information about functions and function calls. </p>  <p>Evaluation takes place in a context called the <em>environment</em>, which consists of the current values and bindings of all Lisp variables (see <a href="variables">Variables</a>).<a id="DOCF8" href="#FOOT8"><sup>8</sup></a> Whenever a form refers to a variable without creating a new binding for it, the variable evaluates to the value given by the current environment. Evaluating a form may also temporarily alter the environment by binding variables (see <a href="local-variables">Local Variables</a>). </p>  <p>Evaluating a form may also make changes that persist; these changes are called <em>side effects</em>. An example of a form that produces a side effect is <code>(setq foo 1)</code>. </p> <p>Do not confuse evaluation with command key interpretation. The editor command loop translates keyboard input into a command (an interactively callable function) using the active keymaps, and then uses <code>call-interactively</code> to execute that command. Executing the command usually involves evaluation, if the command is written in Lisp; however, this step is not considered a part of command key interpretation. See <a href="command-loop">Command Loop</a>. </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/Intro-Eval.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Intro-Eval.html</a>
  </p>
</div>