116 lines
5.9 KiB
HTML
116 lines
5.9 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>Chaining (Guile Reference Manual)</title>
|
|
|
|
<meta name="description" content="Chaining (Guile Reference Manual)">
|
|
<meta name="keywords" content="Chaining (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="Lexical-Scope.html" rel="next" title="Lexical Scope">
|
|
<link href="Local-Variables.html" rel="prev" title="Local Variables">
|
|
<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}
|
|
-->
|
|
</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="Chaining">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="Lexical-Scope.html" accesskey="n" rel="next">Lexical Scope</a>, Previous: <a href="Local-Variables.html" accesskey="p" rel="prev">Local Variables and Environments</a>, Up: <a href="About-Closure.html" accesskey="u" rel="up">The Concept of Closure</a> [<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="Environment-Chaining"><span>3.4.3 Environment Chaining<a class="copiable-link" href="#Environment-Chaining"> ¶</a></span></h4>
|
|
|
|
<a class="index-entry-id" id="index-shadowing-an-imported-variable-binding"></a>
|
|
<a class="index-entry-id" id="index-chaining-environments"></a>
|
|
|
|
<p>In the example of the previous subsection, we glossed over an important
|
|
point. The body of the <code class="code">let</code> expression in that example refers not
|
|
only to the local variable <code class="code">s</code>, but also to the top level variables
|
|
<code class="code">a</code>, <code class="code">b</code>, <code class="code">c</code> and <code class="code">sqrt</code>. (<code class="code">sqrt</code> is the
|
|
standard Scheme procedure for calculating a square root.) If the body
|
|
of the <code class="code">let</code> expression is evaluated in the context of the
|
|
<em class="emph">local</em> <code class="code">let</code> environment, how does the evaluation get at the
|
|
values of these top level variables?
|
|
</p>
|
|
<p>The answer is that the local environment created by a <code class="code">let</code>
|
|
expression automatically has a reference to its containing environment
|
|
— in this case the top level environment — and that the Scheme
|
|
interpreter automatically looks for a variable binding in the containing
|
|
environment if it doesn’t find one in the local environment. More
|
|
generally, every environment except for the top level one has a
|
|
reference to its containing environment, and the interpreter keeps
|
|
searching back up the chain of environments — from most local to top
|
|
level — until it either finds a variable binding for the required
|
|
identifier or exhausts the chain.
|
|
</p>
|
|
<p>This description also determines what happens when there is more than
|
|
one variable binding with the same name. Suppose, continuing the
|
|
example of the previous subsection, that there was also a pre-existing
|
|
top level variable <code class="code">s</code> created by the expression:
|
|
</p>
|
|
<div class="example lisp">
|
|
<pre class="lisp-preformatted">(define s "Some beans, my lord!")
|
|
</pre></div>
|
|
|
|
<p>Then both the top level environment and the local <code class="code">let</code> environment
|
|
would contain bindings for the name <code class="code">s</code>. When evaluating code
|
|
within the <code class="code">let</code> body, the interpreter looks first in the local
|
|
<code class="code">let</code> environment, and so finds the binding for <code class="code">s</code> created by
|
|
the <code class="code">let</code> syntax. Even though this environment has a reference to
|
|
the top level environment, which also has a binding for <code class="code">s</code>, the
|
|
interpreter doesn’t get as far as looking there. When evaluating code
|
|
outside the <code class="code">let</code> body, the interpreter looks up variable names in
|
|
the top level environment, so the name <code class="code">s</code> refers to the top level
|
|
variable.
|
|
</p>
|
|
<p>Within the <code class="code">let</code> body, the binding for <code class="code">s</code> in the local
|
|
environment is said to <em class="dfn">shadow</em> the binding for <code class="code">s</code> in the top
|
|
level environment.
|
|
</p>
|
|
|
|
</div>
|
|
<hr>
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="Lexical-Scope.html">Lexical Scope</a>, Previous: <a href="Local-Variables.html">Local Variables and Environments</a>, Up: <a href="About-Closure.html">The Concept of Closure</a> [<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>
|