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

145 lines
6.6 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>SRFI-2 (Guile Reference Manual)</title>
<meta name="description" content="SRFI-2 (Guile Reference Manual)">
<meta name="keywords" content="SRFI-2 (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="SRFI-Support.html" rel="up" title="SRFI Support">
<link href="SRFI_002d4.html" rel="next" title="SRFI-4">
<link href="SRFI_002d1.html" rel="prev" title="SRFI-1">
<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}
-->
</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="SRFI_002d2">
<div class="nav-panel">
<p>
Next: <a href="SRFI_002d4.html" accesskey="n" rel="next">SRFI-4 - Homogeneous numeric vector datatypes</a>, Previous: <a href="SRFI_002d1.html" accesskey="p" rel="prev">SRFI-1 - List library</a>, Up: <a href="SRFI-Support.html" accesskey="u" rel="up">SRFI Support Modules</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="SRFI_002d2-_002d-and_002dlet_002a"><span>7.5.4 SRFI-2 - and-let*<a class="copiable-link" href="#SRFI_002d2-_002d-and_002dlet_002a"> &para;</a></span></h4>
<a class="index-entry-id" id="index-SRFI_002d2"></a>
<p>The following syntax can be obtained with
</p>
<div class="example lisp">
<pre class="lisp-preformatted">(use-modules (srfi srfi-2))
</pre></div>
<p>or alternatively
</p>
<div class="example lisp">
<pre class="lisp-preformatted">(use-modules (ice-9 and-let-star))
</pre></div>
<dl class="first-deffn">
<dt class="deffn" id="index-and_002dlet_002a"><span class="category-def">library syntax: </span><span><strong class="def-name">and-let*</strong> <var class="def-var-arguments">(clause &hellip;) body &hellip;</var><a class="copiable-link" href="#index-and_002dlet_002a"> &para;</a></span></dt>
<dd><p>A combination of <code class="code">and</code> and <code class="code">let*</code>.
</p>
<p>Each <var class="var">clause</var> is evaluated in turn, and if <code class="code">#f</code> is obtained
then evaluation stops and <code class="code">#f</code> is returned. If all are
non-<code class="code">#f</code> then <var class="var">body</var> is evaluated and the last form gives the
return value, or if <var class="var">body</var> is empty then the result is <code class="code">#t</code>.
Each <var class="var">clause</var> should be one of the following,
</p>
<dl class="table">
<dt><code class="code">(symbol expr)</code></dt>
<dd><p>Evaluate <var class="var">expr</var>, check for <code class="code">#f</code>, and bind it to <var class="var">symbol</var>.
Like <code class="code">let*</code>, that binding is available to subsequent clauses.
</p></dd>
<dt><code class="code">(expr)</code></dt>
<dd><p>Evaluate <var class="var">expr</var> and check for <code class="code">#f</code>.
</p></dd>
<dt><code class="code">symbol</code></dt>
<dd><p>Get the value bound to <var class="var">symbol</var> and check for <code class="code">#f</code>.
</p></dd>
</dl>
<p>Notice that <code class="code">(expr)</code> has an &ldquo;extra&rdquo; pair of parentheses, for
instance <code class="code">((eq? x y))</code>. One way to remember this is to imagine
the <code class="code">symbol</code> in <code class="code">(symbol expr)</code> is omitted.
</p>
<p><code class="code">and-let*</code> is good for calculations where a <code class="code">#f</code> value means
termination, but where a non-<code class="code">#f</code> value is going to be needed in
subsequent expressions.
</p>
<p>The following illustrates this, it returns text between brackets
&lsquo;<samp class="samp">[...]</samp>&rsquo; in a string, or <code class="code">#f</code> if there are no such brackets
(ie. either <code class="code">string-index</code> gives <code class="code">#f</code>).
</p>
<div class="example">
<pre class="example-preformatted">(define (extract-brackets str)
(and-let* ((start (string-index str #\[))
(end (string-index str #\] start)))
(substring str (1+ start) end)))
</pre></div>
<p>The following shows plain variables and expressions tested too.
<code class="code">diagnostic-levels</code> is taken to be an alist associating a
diagnostic type with a level. <code class="code">str</code> is printed only if the type
is known and its level is high enough.
</p>
<div class="example">
<pre class="example-preformatted">(define (show-diagnostic type str)
(and-let* (want-diagnostics
(level (assq-ref diagnostic-levels type))
((&gt;= level current-diagnostic-level)))
(display str)))
</pre></div>
<p>The advantage of <code class="code">and-let*</code> is that an extended sequence of
expressions and tests doesn&rsquo;t require lots of nesting as would arise
from separate <code class="code">and</code> and <code class="code">let*</code>, or from <code class="code">cond</code> with
<code class="code">=&gt;</code>.
</p>
</dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="SRFI_002d4.html">SRFI-4 - Homogeneous numeric vector datatypes</a>, Previous: <a href="SRFI_002d1.html">SRFI-1 - List library</a>, Up: <a href="SRFI-Support.html">SRFI Support Modules</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>