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

165 lines
9.3 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-43 Searching (Guile Reference Manual)</title>
<meta name="description" content="SRFI-43 Searching (Guile Reference Manual)">
<meta name="keywords" content="SRFI-43 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_002d43.html" rel="up" title="SRFI-43">
<link href="SRFI_002d43-Mutators.html" rel="next" title="SRFI-43 Mutators">
<link href="SRFI_002d43-Iteration.html" rel="prev" title="SRFI-43 Iteration">
<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_002d43-Searching">
<div class="nav-panel">
<p>
Next: <a href="SRFI_002d43-Mutators.html" accesskey="n" rel="next">SRFI-43 Mutators</a>, Previous: <a href="SRFI_002d43-Iteration.html" accesskey="p" rel="prev">SRFI-43 Iteration</a>, Up: <a href="SRFI_002d43.html" accesskey="u" rel="up">SRFI-43 - Vector Library</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="subsubsection" id="SRFI_002d43-Searching-1"><span>7.5.30.5 SRFI-43 Searching<a class="copiable-link" href="#SRFI_002d43-Searching-1"> &para;</a></span></h4>
<dl class="first-deffn">
<dt class="deffn" id="index-vector_002dindex"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">vector-index</strong> <var class="def-var-arguments">pred? vec1 vec2 &hellip;</var><a class="copiable-link" href="#index-vector_002dindex"> &para;</a></span></dt>
<dd><p>Find and return the index of the first elements in <var class="var">vec1</var> <var class="var">vec2</var>
&hellip; that satisfy <var class="var">pred?</var>. If no matching element is found by
the end of the shortest vector, return <code class="code">#f</code>.
</p>
<div class="example">
<pre class="example-preformatted">(vector-index even? '#(3 1 4 1 5 9))
&rArr; 2
(vector-index &lt; '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
&rArr; 1
(vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
&rArr; #f
</pre></div>
</dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-vector_002dindex_002dright"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">vector-index-right</strong> <var class="def-var-arguments">pred? vec1 vec2 &hellip;</var><a class="copiable-link" href="#index-vector_002dindex_002dright"> &para;</a></span></dt>
<dd><p>Like <code class="code">vector-index</code>, but it searches right-to-left, rather than
left-to-right. Note that the SRFI 43 specification requires that all
the vectors must have the same length, but both the SRFI 43 reference
implementation and Guile&rsquo;s implementation allow vectors with unequal
lengths, and start searching from the last index of the shortest vector.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-vector_002dskip"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">vector-skip</strong> <var class="def-var-arguments">pred? vec1 vec2 &hellip;</var><a class="copiable-link" href="#index-vector_002dskip"> &para;</a></span></dt>
<dd><p>Find and return the index of the first elements in <var class="var">vec1</var> <var class="var">vec2</var>
&hellip; that do not satisfy <var class="var">pred?</var>. If no matching element is
found by the end of the shortest vector, return <code class="code">#f</code>. Equivalent
to <code class="code">vector-index</code> but with the predicate inverted.
</p>
<div class="example">
<pre class="example-preformatted">(vector-skip number? '#(1 2 a b 3 4 c d)) &rArr; 2
</pre></div>
</dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-vector_002dskip_002dright"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">vector-skip-right</strong> <var class="def-var-arguments">pred? vec1 vec2 &hellip;</var><a class="copiable-link" href="#index-vector_002dskip_002dright"> &para;</a></span></dt>
<dd><p>Like <code class="code">vector-skip</code>, but it searches for a non-matching element
right-to-left, rather than left-to-right. Note that the SRFI 43
specification requires that all the vectors must have the same length,
but both the SRFI 43 reference implementation and Guile&rsquo;s implementation
allow vectors with unequal lengths, and start searching from the last
index of the shortest vector.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-vector_002dbinary_002dsearch"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">vector-binary-search</strong> <var class="def-var-arguments">vec value cmp [start [end]]</var><a class="copiable-link" href="#index-vector_002dbinary_002dsearch"> &para;</a></span></dt>
<dd><p>Find and return an index of <var class="var">vec</var> between <var class="var">start</var> and <var class="var">end</var>
whose value is <var class="var">value</var> using a binary search. If no matching
element is found, return <code class="code">#f</code>. The default <var class="var">start</var> is 0 and
the default <var class="var">end</var> is the length of <var class="var">vec</var>.
</p>
<p><var class="var">cmp</var> must be a procedure of two arguments such that <code class="code">(cmp a
b)</code> returns a negative integer if <em class="math">a &lt; b</em>, a positive integer if
<em class="math">a &gt; b</em>, or zero if <em class="math">a = b</em>. The elements of <var class="var">vec</var> must
be sorted in non-decreasing order according to <var class="var">cmp</var>.
</p>
<p>Note that SRFI 43 does not document the <var class="var">start</var> and <var class="var">end</var>
arguments, but both its reference implementation and Guile&rsquo;s
implementation support them.
</p>
<div class="example">
<pre class="example-preformatted">(define (char-cmp c1 c2)
(cond ((char&lt;? c1 c2) -1)
((char&gt;? c1 c2) 1)
(else 0)))
(vector-binary-search '#(#\a #\b #\c #\d #\e #\f #\g #\h)
#\g
char-cmp)
&rArr; 6
</pre></div>
</dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-vector_002dany"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">vector-any</strong> <var class="def-var-arguments">pred? vec1 vec2 &hellip;</var><a class="copiable-link" href="#index-vector_002dany"> &para;</a></span></dt>
<dd><p>Find the first parallel set of elements from <var class="var">vec1</var> <var class="var">vec2</var>
&hellip; for which <var class="var">pred?</var> returns a true value. If such a parallel
set of elements exists, <code class="code">vector-any</code> returns the value that
<var class="var">pred?</var> returned for that set of elements. The iteration is
strictly left-to-right.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-vector_002devery"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">vector-every</strong> <var class="def-var-arguments">pred? vec1 vec2 &hellip;</var><a class="copiable-link" href="#index-vector_002devery"> &para;</a></span></dt>
<dd><p>If, for every index i between 0 and the length of the shortest vector
argument, the set of elements <code class="code">(vector-ref vec1 i)</code>
<code class="code">(vector-ref vec2 i)</code> &hellip; satisfies <var class="var">pred?</var>,
<code class="code">vector-every</code> returns the value that <var class="var">pred?</var> returned for the
last set of elements, at the last index of the shortest vector.
Otherwise it returns <code class="code">#f</code>. The iteration is strictly
left-to-right.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="SRFI_002d43-Mutators.html">SRFI-43 Mutators</a>, Previous: <a href="SRFI_002d43-Iteration.html">SRFI-43 Iteration</a>, Up: <a href="SRFI_002d43.html">SRFI-43 - Vector Library</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>