196 lines
12 KiB
HTML
196 lines
12 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-1 Searching (Guile Reference Manual)</title>
|
|
|
|
<meta name="description" content="SRFI-1 Searching (Guile Reference Manual)">
|
|
<meta name="keywords" content="SRFI-1 Searching (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_002d1.html" rel="up" title="SRFI-1">
|
|
<link href="SRFI_002d1-Deleting.html" rel="next" title="SRFI-1 Deleting">
|
|
<link href="SRFI_002d1-Filtering-and-Partitioning.html" rel="prev" title="SRFI-1 Filtering and Partitioning">
|
|
<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="subsubsection-level-extent" id="SRFI_002d1-Searching">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="SRFI_002d1-Deleting.html" accesskey="n" rel="next">Deleting</a>, Previous: <a href="SRFI_002d1-Filtering-and-Partitioning.html" accesskey="p" rel="prev">Filtering and Partitioning</a>, Up: <a href="SRFI_002d1.html" accesskey="u" rel="up">SRFI-1 - List library</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="subsubsection" id="Searching"><span>7.5.3.7 Searching<a class="copiable-link" href="#Searching"> ¶</a></span></h4>
|
|
<a class="index-entry-id" id="index-list-search"></a>
|
|
|
|
|
|
<p>The procedures for searching elements in lists either accept a
|
|
predicate or a comparison object for determining which elements are to
|
|
be searched.
|
|
</p>
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-find"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">find</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-find"> ¶</a></span></dt>
|
|
<dd><p>Return the first element of <var class="var">lst</var> that satisfies the predicate
|
|
<var class="var">pred</var> and <code class="code">#f</code> if no such element is found.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-find_002dtail"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">find-tail</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-find_002dtail"> ¶</a></span></dt>
|
|
<dd><p>Return the first pair of <var class="var">lst</var> whose <small class="sc">CAR</small> satisfies the
|
|
predicate <var class="var">pred</var> and <code class="code">#f</code> if no such element is found.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-take_002dwhile"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">take-while</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-take_002dwhile"> ¶</a></span></dt>
|
|
<dt class="deffnx def-cmd-deffn" id="index-take_002dwhile_0021"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">take-while!</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-take_002dwhile_0021"> ¶</a></span></dt>
|
|
<dd><p>Return the longest initial prefix of <var class="var">lst</var> whose elements all
|
|
satisfy the predicate <var class="var">pred</var>.
|
|
</p>
|
|
<p><code class="code">take-while!</code> is allowed, but not required to modify the input
|
|
list while producing the result.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-drop_002dwhile"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">drop-while</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-drop_002dwhile"> ¶</a></span></dt>
|
|
<dd><p>Drop the longest initial prefix of <var class="var">lst</var> whose elements all
|
|
satisfy the predicate <var class="var">pred</var>.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-span"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">span</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-span"> ¶</a></span></dt>
|
|
<dt class="deffnx def-cmd-deffn" id="index-span_0021"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">span!</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-span_0021"> ¶</a></span></dt>
|
|
<dt class="deffnx def-cmd-deffn" id="index-break-2"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">break</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-break-2"> ¶</a></span></dt>
|
|
<dt class="deffnx def-cmd-deffn" id="index-break_0021"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">break!</strong> <var class="def-var-arguments">pred lst</var><a class="copiable-link" href="#index-break_0021"> ¶</a></span></dt>
|
|
<dd><p><code class="code">span</code> splits the list <var class="var">lst</var> into the longest initial prefix
|
|
whose elements all satisfy the predicate <var class="var">pred</var>, and the remaining
|
|
tail. <code class="code">break</code> inverts the sense of the predicate.
|
|
</p>
|
|
<p><code class="code">span!</code> and <code class="code">break!</code> are allowed, but not required to modify
|
|
the structure of the input list <var class="var">lst</var> in order to produce the
|
|
result.
|
|
</p>
|
|
<p>Note that the name <code class="code">break</code> conflicts with the <code class="code">break</code>
|
|
binding established by <code class="code">while</code> (see <a class="pxref" href="while-do.html">Iteration mechanisms</a>). Applications
|
|
wanting to use <code class="code">break</code> from within a <code class="code">while</code> loop will need
|
|
to make a new define under a different name.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-any"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">any</strong> <var class="def-var-arguments">pred lst1 lst2 …</var><a class="copiable-link" href="#index-any"> ¶</a></span></dt>
|
|
<dd><p>Test whether any set of elements from <var class="var">lst1</var> <var class="var">lst2</var> …
|
|
satisfies <var class="var">pred</var>. If so, the return value is the return value from
|
|
the successful <var class="var">pred</var> call, or if not, the return value is
|
|
<code class="code">#f</code>.
|
|
</p>
|
|
<p>If there are n list arguments, then <var class="var">pred</var> must be a predicate
|
|
taking n arguments. Each <var class="var">pred</var> call is <code class="code">(<var class="var">pred</var>
|
|
<var class="var">elem1</var> <var class="var">elem2</var> … )</code> taking an element from each
|
|
<var class="var">lst</var>. The calls are made successively for the first, second, etc.
|
|
elements of the lists, stopping when <var class="var">pred</var> returns non-<code class="code">#f</code>,
|
|
or when the end of the shortest list is reached.
|
|
</p>
|
|
<p>The <var class="var">pred</var> call on the last set of elements (i.e., when the end of
|
|
the shortest list has been reached), if that point is reached, is a
|
|
tail call.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-every"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">every</strong> <var class="def-var-arguments">pred lst1 lst2 …</var><a class="copiable-link" href="#index-every"> ¶</a></span></dt>
|
|
<dd><p>Test whether every set of elements from <var class="var">lst1</var> <var class="var">lst2</var> …
|
|
satisfies <var class="var">pred</var>. If so, the return value is the return from the
|
|
final <var class="var">pred</var> call, or if not, the return value is <code class="code">#f</code>.
|
|
</p>
|
|
<p>If there are n list arguments, then <var class="var">pred</var> must be a predicate
|
|
taking n arguments. Each <var class="var">pred</var> call is <code class="code">(<var class="var">pred</var>
|
|
<var class="var">elem1</var> <var class="var">elem2 …</var>)</code> taking an element from each
|
|
<var class="var">lst</var>. The calls are made successively for the first, second, etc.
|
|
elements of the lists, stopping if <var class="var">pred</var> returns <code class="code">#f</code>, or when
|
|
the end of any of the lists is reached.
|
|
</p>
|
|
<p>The <var class="var">pred</var> call on the last set of elements (i.e., when the end of
|
|
the shortest list has been reached) is a tail call.
|
|
</p>
|
|
<p>If one of <var class="var">lst1</var> <var class="var">lst2</var> …is empty then no calls to
|
|
<var class="var">pred</var> are made, and the return value is <code class="code">#t</code>.
|
|
</p></dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-list_002dindex"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">list-index</strong> <var class="def-var-arguments">pred lst1 lst2 …</var><a class="copiable-link" href="#index-list_002dindex"> ¶</a></span></dt>
|
|
<dd><p>Return the index of the first set of elements, one from each of
|
|
<var class="var">lst1</var> <var class="var">lst2</var> …, which satisfies <var class="var">pred</var>.
|
|
</p>
|
|
<p><var class="var">pred</var> is called as <code class="code">(<var class="var">elem1</var> <var class="var">elem2 …</var>)</code>.
|
|
Searching stops when the end of the shortest <var class="var">lst</var> is reached.
|
|
The return index starts from 0 for the first set of elements. If no
|
|
set of elements pass, then the return value is <code class="code">#f</code>.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">(list-index odd? '(2 4 6 9)) ⇒ 3
|
|
(list-index = '(1 2 3) '(3 1 2)) ⇒ #f
|
|
</pre></div>
|
|
</dd></dl>
|
|
|
|
<dl class="first-deffn">
|
|
<dt class="deffn" id="index-member-1"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">member</strong> <var class="def-var-arguments">x lst [=]</var><a class="copiable-link" href="#index-member-1"> ¶</a></span></dt>
|
|
<dd><p>Return the first sublist of <var class="var">lst</var> whose <small class="sc">CAR</small> is equal to
|
|
<var class="var">x</var>. If <var class="var">x</var> does not appear in <var class="var">lst</var>, return <code class="code">#f</code>.
|
|
</p>
|
|
<p>Equality is determined by <code class="code">equal?</code>, or by the equality predicate
|
|
<var class="var">=</var> if given. <var class="var">=</var> is called <code class="code">(= <var class="var">x</var> elem)</code>,
|
|
ie. with the given <var class="var">x</var> first, so for example to find the first
|
|
element greater than 5,
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">(member 5 '(3 5 1 7 2 9) <) ⇒ (7 2 9)
|
|
</pre></div>
|
|
|
|
<p>This version of <code class="code">member</code> extends the core <code class="code">member</code>
|
|
(see <a class="pxref" href="List-Searching.html">List Searching</a>) by accepting an equality predicate.
|
|
</p></dd></dl>
|
|
|
|
|
|
</div>
|
|
<hr>
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="SRFI_002d1-Deleting.html">Deleting</a>, Previous: <a href="SRFI_002d1-Filtering-and-Partitioning.html">Filtering and Partitioning</a>, Up: <a href="SRFI_002d1.html">SRFI-1 - List library</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>
|