📄 r5rs-z-h-9.html
字号:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><!-- Generated from TeX source by tex2page, v 4o4, (c) Dorai Sitaram, http://www.cs.rice.edu/~dorai/tex2page --><head><title>Revised^5 Report on the Algorithmic Language Scheme</title><link rel="stylesheet" type="text/css" href="r5rs-Z-C.css" title=default><meta name=robots content="noindex,follow"></head><body><p><div class=navigation>[Go to <span><a href="r5rs.html">first</a>, <a href="r5rs-Z-H-8.html">previous</a></span><span>, <a href="r5rs-Z-H-10.html">next</a></span> page<span>; </span><span><a href="r5rs-Z-H-2.html#%_toc_start">contents</a></span><span><span>; </span><a href="r5rs-Z-H-15.html#%_index_start">index</a></span>]</div><p><a name="%_chap_6"></a><h1 class=chapter><div class=chapterheading><a href="r5rs-Z-H-2.html#%_toc_%_chap_6">Chapter 6</a></div><p><a href="r5rs-Z-H-2.html#%_toc_%_chap_6">Standard procedures</a></h1><p><p><a name="%_idx_200"></a><a name="%_idx_202"></a><a name="%_idx_204"></a><p>This chapter describes Scheme's built-in procedures. The initial (or``top level'') Scheme environment starts out with a number of variablesbound to locations containing useful values, most of which are primitiveprocedures that manipulate data. For example, the variable <tt>abs</tt> isbound to (a location initially containing) a procedure of one argumentthat computes the absolute value of a number, and the variable <tt>+</tt>is bound to a procedure that computes sums. Built-in procedures thatcan easily be written in terms of other built-in procedures are identified as``library procedures''.<p>A program may use a top-level definition to bind any variable. It maysubsequently alter any such binding by an assignment (see <a href="r5rs-Z-H-7.html#%_sec_4.1.6">4.1.6</a>).These operations do not modify the behavior of Scheme's built-inprocedures. Altering any top-level binding that has not been introduced by adefinition has an unspecified effect on the behavior of the built-in procedures.<p><a name="%_sec_6.1"></a><h2><a href="r5rs-Z-H-2.html#%_toc_%_sec_6.1">6.1 Equivalence predicates</a></h2><p><p>A <a name="%_idx_206"></a><em>predicate</em> is a procedure that always returns a booleanvalue (<tt>#t</tt> or <tt>#f</tt>). An <a name="%_idx_208"></a><em>equivalence predicate</em> isthe computational analogue of a mathematical equivalence relation (it issymmetric, reflexive, and transitive). Of the equivalence predicatesdescribed in this section, <tt>eq?</tt> is the finest or mostdiscriminating, and <tt>equal?</tt> is the coarsest. <tt>Eqv?</tt> isslightly less discriminating than <tt>eq?</tt>. <p><p><div align=left><u>procedure:</u> <tt>(<a name="%_idx_210"></a>eqv?<i> <i>obj<sub>1</sub></i> <i>obj<sub>2</sub></i></i>)</tt> </div><p>The <tt>eqv?</tt> procedure defines a useful equivalence relation on objects.Briefly, it returns <tt>#t</tt> if <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> shouldnormally be regarded as the same object. This relation is left slightlyopen to interpretation, but the following partial specification of<tt>eqv?</tt> holds for all implementations of Scheme.<p>The <tt>eqv?</tt> procedure returns <tt>#t</tt> if:<p><p><ul><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both <tt>#t</tt> or both <tt>#f</tt>.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both symbols and<p><tt><p>(string=? (symbol->string obj1)<br> (symbol->string obj2))<br> ===> <tt>#t</tt><p></tt><p><blockquote><em>Note: </em> This assumes that neither <i>obj<sub>1</sub></i> nor <i>obj<sub>2</sub></i> is an ``uninternedsymbol'' as alluded to in section <a href="#%_sec_6.3.3">6.3.3</a>. This report doesnot presume to specify the behavior of <tt>eqv?</tt> on implementation-dependentextensions.</blockquote><p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both numbers, are numericallyequal (see <tt>=</tt>, section <a href="#%_sec_6.2">6.2</a>), and are either bothexact<a name="%_idx_212"></a> or both inexact<a name="%_idx_214"></a>.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are both characters and are the samecharacter according to the <tt>char=?</tt> procedure(section <a href="#%_sec_6.3.4">6.3.4</a>).<p><li>both <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are the empty list.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are pairs, vectors, or strings that denote thesame locations in the store (section <a href="r5rs-Z-H-6.html#%_sec_3.4">3.4</a>).<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are procedures whose location tags areequal (section <a href="r5rs-Z-H-7.html#%_sec_4.1.4">4.1.4</a>).</ul><p><p>The <tt>eqv?</tt> procedure returns <tt>#f</tt> if:<p><p><ul><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are of different types(section <a href="r5rs-Z-H-6.html#%_sec_3.2">3.2</a>).<p><li>one of <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> is <tt>#t</tt> but the other is<tt>#f</tt>.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are symbols but<p><tt><p>(string=? (symbol->string <i>obj<sub>1</sub></i>)<br> (symbol->string <i>obj<sub>2</sub></i>))<br> ===> <tt>#f</tt><p></tt><p><li>one of <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> is an exact number but the otheris an inexact number.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are numbers for which the <tt>=</tt>procedure returns <tt>#f</tt>.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are characters for which the <tt>char=?</tt>procedure returns <tt>#f</tt>.<p><li>one of <i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> is the empty list but the otheris not.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are pairs, vectors, or strings that denotedistinct locations.<p><li><i>obj<sub>1</sub></i> and <i>obj<sub>2</sub></i> are procedures that would behave differently(return different value(s) or have different side effects) for some arguments.<p></ul><p><p><tt><p>(eqv? 'a 'a) ===> <tt>#t</tt><br>(eqv? 'a 'b) ===> <tt>#f</tt><br>(eqv? 2 2) ===> <tt>#t</tt><br>(eqv? '() '()) ===> <tt>#t</tt><br>(eqv? 100000000 100000000) ===> <tt>#t</tt><br>(eqv? (cons 1 2) (cons 1 2)) ===> <tt>#f</tt><br>(eqv? (lambda () 1)<br> (lambda () 2)) ===> <tt>#f</tt><br>(eqv? #f 'nil) ===> <tt>#f</tt><br>(let ((p (lambda (x) x)))<br> (eqv? p p)) ===> <tt>#t</tt><p></tt><p>The following examples illustrate cases in which the above rules donot fully specify the behavior of <tt>eqv?</tt>. All that can be saidabout such cases is that the value returned by <tt>eqv?</tt> must be aboolean.<p><tt><p>(eqv? "" "") ===> <i>unspecified</i><br>(eqv? '#() '#()) ===> <i>unspecified</i><br>(eqv? (lambda (x) x)<br> (lambda (x) x)) ===> <i>unspecified</i><br>(eqv? (lambda (x) x)<br> (lambda (y) y)) ===> <i>unspecified</i><p></tt><p>The next set of examples shows the use of <tt>eqv?</tt> with proceduresthat have local state. <tt>Gen-counter</tt> must return a distinctprocedure every time, since each procedure has its own internal counter.<tt>Gen-loser</tt>, however, returns equivalent procedures each time, sincethe local state does not affect the value or side effects of theprocedures.<p><tt><p>(define gen-counter<br> (lambda ()<br> (let ((n 0))<br> (lambda () (set! n (+ n 1)) n))))<br>(let ((g (gen-counter)))<br> (eqv? g g)) ===> <tt>#t</tt><br>(eqv? (gen-counter) (gen-counter))<br> ===> <tt>#f</tt><br>(define gen-loser<br> (lambda ()<br> (let ((n 0))<br> (lambda () (set! n (+ n 1)) 27))))<br>(let ((g (gen-loser)))<br> (eqv? g g)) ===> <tt>#t</tt><br>(eqv? (gen-loser) (gen-loser))<br> ===> <i>unspecified</i><br><br>(letrec ((f (lambda () (if (eqv? f g) 'both 'f)))<br> (g (lambda () (if (eqv? f g) 'both 'g))))<br> (eqv? f g))<br> ===> <i>unspecified</i><br><br>(letrec ((f (lambda () (if (eqv? f g) 'f 'both)))<br> (g (lambda () (if (eqv? f g) 'g 'both))))<br> (eqv? f g))<br> ===> <tt>#f</tt><p></tt><p>Since it is an error to modify constant objects (those returned byliteral expressions), implementations are permitted, though notrequired, to share structure between constants where appropriate. Thusthe value of <tt>eqv?</tt> on constants is sometimesimplementation-dependent.<p><tt><p>(eqv? '(a) '(a)) ===> <i>unspecified</i><br>(eqv? "a" "a") ===> <i>unspecified</i><br>(eqv? '(b) (cdr '(a b))) ===> <i>unspecified</i><br>(let ((x '(a)))<br> (eqv? x x)) ===> <tt>#t</tt><p></tt><p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -