1
0
Fork 0
cl-sites/guile.html_node/Equality.html

223 lines
12 KiB
HTML
Raw Normal View History

2024-12-17 12:49:28 +01:00
<!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>Equality (Guile Reference Manual)</title>
<meta name="description" content="Equality (Guile Reference Manual)">
<meta name="keywords" content="Equality (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="Utility-Functions.html" rel="up" title="Utility Functions">
<link href="Object-Properties.html" rel="next" title="Object Properties">
<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="Equality">
<div class="nav-panel">
<p>
Next: <a href="Object-Properties.html" accesskey="n" rel="next">Object Properties</a>, Up: <a href="Utility-Functions.html" accesskey="u" rel="up">General Utility Functions</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="Equality-1"><span>6.9.1 Equality<a class="copiable-link" href="#Equality-1"> &para;</a></span></h4>
<a class="index-entry-id" id="index-sameness"></a>
<a class="index-entry-id" id="index-equality"></a>
<p>There are three kinds of core equality predicates in Scheme, described
below. The same kinds of comparisons arise in other functions, like
<code class="code">memq</code> and friends (see <a class="pxref" href="List-Searching.html">List Searching</a>).
</p>
<p>For all three tests, objects of different types are never equal. So
for instance a list and a vector are not <code class="code">equal?</code>, even if their
contents are the same. Exact and inexact numbers are considered
different types too, and are hence not equal even if their values are
the same.
</p>
<p><code class="code">eq?</code> tests just for the same object (essentially a pointer
comparison). This is fast, and can be used when searching for a
particular object, or when working with symbols or keywords (which are
always unique objects).
</p>
<p><code class="code">eqv?</code> extends <code class="code">eq?</code> to look at the value of numbers and
characters. It can for instance be used somewhat like <code class="code">=</code>
(see <a class="pxref" href="Comparison.html">Comparison Predicates</a>) but without an error if one operand isn&rsquo;t a
number.
</p>
<p><code class="code">equal?</code> goes further, it looks (recursively) into the contents
of lists, vectors, etc. This is good for instance on lists that have
been read or calculated in various places and are the same, just not
made up of the same pairs. Such lists look the same (when printed),
and <code class="code">equal?</code> will consider them the same.
</p>
<br>
<dl class="first-deffn">
<dt class="deffn" id="index-eq_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">eq?</strong> <var class="def-var-arguments">&hellip;</var><a class="copiable-link" href="#index-eq_003f"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005feq_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_eq_p</strong> <var class="def-var-arguments">(x, y)</var><a class="copiable-link" href="#index-scm_005feq_005fp"> &para;</a></span></dt>
<dd><a class="index-entry-id" id="index-eq_003f-3"></a>
<p>The Scheme procedure returns <code class="code">#t</code> if all of its arguments are the
same object, except for numbers and characters. The C function does the
same but takes exactly two arguments. For example,
</p>
<div class="example">
<pre class="example-preformatted">(define x (vector 1 2 3))
(define y (vector 1 2 3))
(eq? x x) &rArr; #t
(eq? x y) &rArr; #f
</pre></div>
<p>Numbers and characters are not equal to any other object, but the
problem is they&rsquo;re not necessarily <code class="code">eq?</code> to themselves either.
This is even so when the number comes directly from a variable,
</p>
<div class="example">
<pre class="example-preformatted">(let ((n (+ 2 3)))
(eq? n n)) &rArr; *unspecified*
</pre></div>
<p>Generally <code class="code">eqv?</code> below should be used when comparing numbers or
characters. <code class="code">=</code> (see <a class="pxref" href="Comparison.html">Comparison Predicates</a>) or <code class="code">char=?</code>
(see <a class="pxref" href="Characters.html">Characters</a>) can be used too.
</p>
<p>It&rsquo;s worth noting that end-of-list <code class="code">()</code>, <code class="code">#t</code>, <code class="code">#f</code>, a
symbol of a given name, and a keyword of a given name, are unique
objects. There&rsquo;s just one of each, so for instance no matter how
<code class="code">()</code> arises in a program, it&rsquo;s the same object and can be
compared with <code class="code">eq?</code>,
</p>
<div class="example">
<pre class="example-preformatted">(define x (cdr '(123)))
(define y (cdr '(456)))
(eq? x y) &rArr; #t
(define x (string-&gt;symbol &quot;foo&quot;))
(eq? x 'foo) &rArr; #t
</pre></div>
</dd></dl>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-scm_005fis_005feq"><span class="category-def">C Function: </span><span><code class="def-type">int</code> <strong class="def-name">scm_is_eq</strong> <code class="def-code-arguments">(SCM x, SCM y)</code><a class="copiable-link" href="#index-scm_005fis_005feq"> &para;</a></span></dt>
<dd><p>Return <code class="code">1</code> when <var class="var">x</var> and <var class="var">y</var> are equal in the sense of
<code class="code">eq?</code>, otherwise return <code class="code">0</code>.
</p>
<a class="index-entry-id" id="index-_003d_003d"></a>
<p>The <code class="code">==</code> operator should not be used on <code class="code">SCM</code> values, an
<code class="code">SCM</code> is a C type which cannot necessarily be compared using
<code class="code">==</code> (see <a class="pxref" href="The-SCM-Type.html">The SCM Type</a>).
</p></dd></dl>
<br>
<dl class="first-deffn">
<dt class="deffn" id="index-eqv_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">eqv?</strong> <var class="def-var-arguments">&hellip;</var><a class="copiable-link" href="#index-eqv_003f"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005feqv_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_eqv_p</strong> <var class="def-var-arguments">(x, y)</var><a class="copiable-link" href="#index-scm_005feqv_005fp"> &para;</a></span></dt>
<dd><a class="index-entry-id" id="index-eqv_003f-2"></a>
<p>The Scheme procedure returns <code class="code">#t</code> if all of its arguments are the
same object, or for characters and numbers the same value. The C function
is similar but takes exactly two arguments.
</p>
<p>On objects except characters and numbers, <code class="code">eqv?</code> is the same as
<code class="code">eq?</code> above. <code class="code">(eqv? x y)</code> is true if <var class="var">x</var> and <var class="var">y</var> are
the same object.
</p>
<p>If <var class="var">x</var> and <var class="var">y</var> are numbers or characters, <code class="code">eqv?</code> compares
their type and value. An exact number is not <code class="code">eqv?</code> to an
inexact number (even if their value is the same).
</p>
<div class="example">
<pre class="example-preformatted">(eqv? 3 (+ 1 2)) &rArr; #t
(eqv? 1 1.0) &rArr; #f
</pre></div>
</dd></dl>
<br>
<dl class="first-deffn">
<dt class="deffn" id="index-equal_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">equal?</strong> <var class="def-var-arguments">&hellip;</var><a class="copiable-link" href="#index-equal_003f"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-scm_005fequal_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_equal_p</strong> <var class="def-var-arguments">(x, y)</var><a class="copiable-link" href="#index-scm_005fequal_005fp"> &para;</a></span></dt>
<dd><a class="index-entry-id" id="index-equal_003f-2"></a>
<p>The Scheme procedure returns <code class="code">#t</code> if all of its arguments are the
same type, and their contents or value are equal. The C function is
similar, but takes exactly two arguments.
</p>
<p>For a pair, string, vector, array or structure, <code class="code">equal?</code> compares the
contents, and does so using the same <code class="code">equal?</code> recursively,
so a deep structure can be traversed.
</p>
<div class="example">
<pre class="example-preformatted">(equal? (list 1 2 3) (list 1 2 3)) &rArr; #t
(equal? (list 1 2 3) (vector 1 2 3)) &rArr; #f
</pre></div>
<p>For other objects, <code class="code">equal?</code> compares as per <code class="code">eqv?</code> above,
which means characters and numbers are compared by type and value (and
like <code class="code">eqv?</code>, exact and inexact numbers are not <code class="code">equal?</code>,
even if their value is the same).
</p>
<div class="example">
<pre class="example-preformatted">(equal? 3 (+ 1 2)) &rArr; #t
(equal? 1 1.0) &rArr; #f
</pre></div>
<p>Hash tables are currently only compared as per <code class="code">eq?</code>, so two
different tables are not <code class="code">equal?</code>, even if their contents are the
same.
</p>
<p><code class="code">equal?</code> does not support circular data structures, it may go
into an infinite loop if asked to compare two circular lists or
similar.
</p>
<p>GOOPS object types (see <a class="pxref" href="GOOPS.html">GOOPS</a>), including foreign object types
(see <a class="pxref" href="Defining-New-Foreign-Object-Types.html">Defining New Foreign Object Types</a>), can have an <code class="code">equal?</code>
implementation specialized on two values of the same type. If
<code class="code">equal?</code> is called on two GOOPS objects of the same type,
<code class="code">equal?</code> will dispatch out to a generic function. This lets an
application traverse the contents or control what is considered
<code class="code">equal?</code> for two objects of such a type. If there&rsquo;s no such
handler, the default is to just compare as per <code class="code">eq?</code>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Object-Properties.html">Object Properties</a>, Up: <a href="Utility-Functions.html">General Utility Functions</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>