1
0
Fork 0
cl-sites/guile.html_node/Closure.html
2024-12-17 12:49:28 +01:00

126 lines
5.5 KiB
HTML

<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Closure (Guile Reference Manual)</title>
<meta name="description" content="Closure (Guile Reference Manual)">
<meta name="keywords" content="Closure (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="About-Closure.html" rel="up" title="About Closure">
<link href="Serial-Number.html" rel="next" title="Serial Number">
<link href="Lexical-Scope.html" rel="prev" title="Lexical Scope">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Closure">
<div class="nav-panel">
<p>
Next: <a href="Serial-Number.html" accesskey="n" rel="next">Example 1: A Serial Number Generator</a>, Previous: <a href="Lexical-Scope.html" accesskey="p" rel="prev">Lexical Scope</a>, Up: <a href="About-Closure.html" accesskey="u" rel="up">The Concept of Closure</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Closure-1"><span>3.4.5 Closure<a class="copiable-link" href="#Closure-1"> &para;</a></span></h4>
<p>Consider a <code class="code">let</code> expression that doesn&rsquo;t contain any
<code class="code">lambda</code>s:
</p>
<div class="example lisp">
<pre class="lisp-preformatted">(let ((s (/ (+ a b c) 2)))
(sqrt (* s (- s a) (- s b) (- s c))))
</pre></div>
<p>When the Scheme interpreter evaluates this, it
</p>
<ul class="itemize mark-bullet">
<li>creates a new environment with a reference to the environment that was
current when it encountered the <code class="code">let</code>
</li><li>creates a variable binding for <code class="code">s</code> in the new environment, with
value given by <code class="code">(/ (+ a b c) 2)</code>
</li><li>evaluates the expression in the body of the <code class="code">let</code> in the context of
the new local environment, and remembers the value <code class="code">V</code>
</li><li>forgets the local environment
</li><li>continues evaluating the expression that contained the <code class="code">let</code>, using
the value <code class="code">V</code> as the value of the <code class="code">let</code> expression, in the
context of the containing environment.
</li></ul>
<p>After the <code class="code">let</code> expression has been evaluated, the local
environment that was created is simply forgotten, and there is no longer
any way to access the binding that was created in this environment. If
the same code is evaluated again, it will follow the same steps again,
creating a second new local environment that has no connection with the
first, and then forgetting this one as well.
</p>
<p>If the <code class="code">let</code> body contains a <code class="code">lambda</code> expression, however, the
local environment is <em class="emph">not</em> forgotten. Instead, it becomes
associated with the procedure that is created by the <code class="code">lambda</code>
expression, and is reinstated every time that that procedure is called.
In detail, this works as follows.
</p>
<ul class="itemize mark-bullet">
<li>When the Scheme interpreter evaluates a <code class="code">lambda</code> expression, to
create a procedure object, it stores the current environment as part of
the procedure definition.
</li><li>Then, whenever that procedure is called, the interpreter reinstates the
environment that is stored in the procedure definition and evaluates the
procedure body within the context of that environment.
</li></ul>
<p>The result is that the procedure body is always evaluated in the context
of the environment that was current when the procedure was created.
</p>
<p>This is what is meant by <em class="dfn">closure</em>. The next few subsections
present examples that explore the usefulness of this concept.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Serial-Number.html">Example 1: A Serial Number Generator</a>, Previous: <a href="Lexical-Scope.html">Lexical Scope</a>, Up: <a href="About-Closure.html">The Concept of Closure</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>