237 lines
11 KiB
HTML
237 lines
11 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>while do (Guile Reference Manual)</title>
|
||
|
|
||
|
<meta name="description" content="while do (Guile Reference Manual)">
|
||
|
<meta name="keywords" content="while do (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="Control-Mechanisms.html" rel="up" title="Control Mechanisms">
|
||
|
<link href="Prompts.html" rel="next" title="Prompts">
|
||
|
<link href="and-or.html" rel="prev" title="and or">
|
||
|
<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}
|
||
|
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
|
||
|
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="while-do">
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Prompts.html" accesskey="n" rel="next">Prompts</a>, Previous: <a href="and-or.html" accesskey="p" rel="prev">Conditional Evaluation of a Sequence of Expressions</a>, Up: <a href="Control-Mechanisms.html" accesskey="u" rel="up">Controlling the Flow of Program Execution</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="Iteration-mechanisms"><span>6.11.4 Iteration mechanisms<a class="copiable-link" href="#Iteration-mechanisms"> ¶</a></span></h4>
|
||
|
|
||
|
<a class="index-entry-id" id="index-iteration"></a>
|
||
|
<a class="index-entry-id" id="index-looping"></a>
|
||
|
<a class="index-entry-id" id="index-named-let"></a>
|
||
|
|
||
|
<p>Scheme has only few iteration mechanisms, mainly because iteration in
|
||
|
Scheme programs is normally expressed using recursion. Nevertheless,
|
||
|
R5RS defines a construct for programming loops, calling <code class="code">do</code>. In
|
||
|
addition, Guile has an explicit looping syntax called <code class="code">while</code>.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-do"><span class="category-def">syntax: </span><span><strong class="def-name">do</strong> <var class="def-var-arguments">((variable init [step]) …) (test expr …) body …</var><a class="copiable-link" href="#index-do"> ¶</a></span></dt>
|
||
|
<dd><p>Bind <var class="var">variable</var>s and evaluate <var class="var">body</var> until <var class="var">test</var> is true.
|
||
|
The return value is the last <var class="var">expr</var> after <var class="var">test</var>, if given. A
|
||
|
simple example will illustrate the basic form,
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(do ((i 1 (1+ i)))
|
||
|
((> i 4))
|
||
|
(display i))
|
||
|
-| 1234
|
||
|
</pre></div>
|
||
|
|
||
|
<p>Or with two variables and a final return value,
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(do ((i 1 (1+ i))
|
||
|
(p 3 (* 3 p)))
|
||
|
((> i 4)
|
||
|
p)
|
||
|
(format #t "3**~s is ~s\n" i p))
|
||
|
-|
|
||
|
3**1 is 3
|
||
|
3**2 is 9
|
||
|
3**3 is 27
|
||
|
3**4 is 81
|
||
|
⇒
|
||
|
243
|
||
|
</pre></div>
|
||
|
|
||
|
<p>The <var class="var">variable</var> bindings are established like a <code class="code">let</code>, in that
|
||
|
the expressions are all evaluated and then all bindings made. When
|
||
|
iterating, the optional <var class="var">step</var> expressions are evaluated with the
|
||
|
previous bindings in scope, then new bindings all made.
|
||
|
</p>
|
||
|
<p>The <var class="var">test</var> expression is a termination condition. Looping stops
|
||
|
when the <var class="var">test</var> is true. It’s evaluated before running the
|
||
|
<var class="var">body</var> each time, so if it’s true the first time then <var class="var">body</var>
|
||
|
is not run at all.
|
||
|
</p>
|
||
|
<p>The optional <var class="var">expr</var>s after the <var class="var">test</var> are evaluated at the end
|
||
|
of looping, with the final <var class="var">variable</var> bindings available. The
|
||
|
last <var class="var">expr</var> gives the return value, or if there are no <var class="var">expr</var>s
|
||
|
the return value is unspecified.
|
||
|
</p>
|
||
|
<p>Each iteration establishes bindings to fresh locations for the
|
||
|
<var class="var">variable</var>s, like a new <code class="code">let</code> for each iteration. This is
|
||
|
done for <var class="var">variable</var>s without <var class="var">step</var> expressions too. The
|
||
|
following illustrates this, showing how a new <code class="code">i</code> is captured by
|
||
|
the <code class="code">lambda</code> in each iteration (see <a class="pxref" href="About-Closure.html">The
|
||
|
Concept of Closure</a>).
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(define lst '())
|
||
|
(do ((i 1 (1+ i)))
|
||
|
((> i 4))
|
||
|
(set! lst (cons (lambda () i) lst)))
|
||
|
(map (lambda (proc) (proc)) lst)
|
||
|
⇒
|
||
|
(4 3 2 1)
|
||
|
</pre></div>
|
||
|
</dd></dl>
|
||
|
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-while"><span class="category-def">syntax: </span><span><strong class="def-name">while</strong> <var class="def-var-arguments">cond body …</var><a class="copiable-link" href="#index-while"> ¶</a></span></dt>
|
||
|
<dd><p>Run a loop executing the <var class="var">body</var> forms while <var class="var">cond</var> is true.
|
||
|
<var class="var">cond</var> is tested at the start of each iteration, so if it’s
|
||
|
<code class="code">#f</code> the first time then <var class="var">body</var> is not executed at all.
|
||
|
</p>
|
||
|
<p>Within <code class="code">while</code>, two extra bindings are provided, they can be used
|
||
|
from both <var class="var">cond</var> and <var class="var">body</var>.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-break-1"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">break</strong> <var class="def-var-arguments">break-arg …</var><a class="copiable-link" href="#index-break-1"> ¶</a></span></dt>
|
||
|
<dd><p>Break out of the <code class="code">while</code> form.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-continue"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">continue</strong><a class="copiable-link" href="#index-continue"> ¶</a></span></dt>
|
||
|
<dd><p>Abandon the current iteration, go back to the start and test
|
||
|
<var class="var">cond</var> again, etc.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<p>If the loop terminates normally, by the <var class="var">cond</var> evaluating to
|
||
|
<code class="code">#f</code>, then the <code class="code">while</code> expression as a whole evaluates to
|
||
|
<code class="code">#f</code>. If it terminates by a call to <code class="code">break</code> with some number
|
||
|
of arguments, those arguments are returned from the <code class="code">while</code>
|
||
|
expression, as multiple values. Otherwise if it terminates by a call to
|
||
|
<code class="code">break</code> with no arguments, then return value is <code class="code">#t</code>.
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(while #f (error "not reached")) ⇒ #f
|
||
|
(while #t (break)) ⇒ #t
|
||
|
(while #t (break 1 2 3)) ⇒ 1 2 3
|
||
|
</pre></div>
|
||
|
|
||
|
<p>Each <code class="code">while</code> form gets its own <code class="code">break</code> and <code class="code">continue</code>
|
||
|
procedures, operating on that <code class="code">while</code>. This means when loops are
|
||
|
nested the outer <code class="code">break</code> can be used to escape all the way out.
|
||
|
For example,
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(while (test1)
|
||
|
(let ((outer-break break))
|
||
|
(while (test2)
|
||
|
(if (something)
|
||
|
(outer-break #f))
|
||
|
...)))
|
||
|
</pre></div>
|
||
|
|
||
|
<p>Note that each <code class="code">break</code> and <code class="code">continue</code> procedure can only be
|
||
|
used within the dynamic extent of its <code class="code">while</code>. Outside the
|
||
|
<code class="code">while</code> their behavior is unspecified.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<a class="index-entry-id" id="index-named-let-1"></a>
|
||
|
<p>Another very common way of expressing iteration in Scheme programs is
|
||
|
the use of the so-called <em class="dfn">named let</em>.
|
||
|
</p>
|
||
|
<p>Named let is a variant of <code class="code">let</code> which creates a procedure and calls
|
||
|
it in one step. Because of the newly created procedure, named let is
|
||
|
more powerful than <code class="code">do</code>–it can be used for iteration, but also
|
||
|
for arbitrary recursion.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-let-1"><span class="category-def">syntax: </span><span><strong class="def-name">let</strong> <var class="def-var-arguments">variable bindings body</var><a class="copiable-link" href="#index-let-1"> ¶</a></span></dt>
|
||
|
<dd><p>For the definition of <var class="var">bindings</var> see the documentation about
|
||
|
<code class="code">let</code> (see <a class="pxref" href="Local-Bindings.html">Local Variable Bindings</a>).
|
||
|
</p>
|
||
|
<p>Named <code class="code">let</code> works as follows:
|
||
|
</p>
|
||
|
<ul class="itemize mark-bullet">
|
||
|
<li>A new procedure which accepts as many arguments as are in <var class="var">bindings</var>
|
||
|
is created and bound locally (using <code class="code">let</code>) to <var class="var">variable</var>. The
|
||
|
new procedure’s formal argument names are the name of the
|
||
|
<var class="var">variables</var>.
|
||
|
|
||
|
</li><li>The <var class="var">body</var> expressions are inserted into the newly created procedure.
|
||
|
|
||
|
</li><li>The procedure is called with the <var class="var">init</var> expressions as the formal
|
||
|
arguments.
|
||
|
</li></ul>
|
||
|
|
||
|
<p>The next example implements a loop which iterates (by recursion) 1000
|
||
|
times.
|
||
|
</p>
|
||
|
<div class="example lisp">
|
||
|
<pre class="lisp-preformatted">(let lp ((x 1000))
|
||
|
(if (positive? x)
|
||
|
(lp (- x 1))
|
||
|
x))
|
||
|
⇒
|
||
|
0
|
||
|
</pre></div>
|
||
|
</dd></dl>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
<hr>
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Prompts.html">Prompts</a>, Previous: <a href="and-or.html">Conditional Evaluation of a Sequence of Expressions</a>, Up: <a href="Control-Mechanisms.html">Controlling the Flow of Program Execution</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>
|