⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 r5rs-z-h-9.html

📁 scheme 标准(r5rs)。Scheme是MIT发布的基于Lambda运算的语言
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!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>; &nbsp;&nbsp;</span><span><a href="r5rs-Z-H-2.html#%_toc_start">contents</a></span><span><span>; &nbsp;&nbsp;</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&nbsp;&nbsp;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>&nbsp;&nbsp;<tt>(<a name="%_idx_210"></a>eqv?<i> <i>obj<sub>1</sub></i> <i>obj<sub>2</sub></i></i>)</tt>&nbsp;</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=?&nbsp;(symbol-&gt;string&nbsp;obj1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(symbol-&gt;string&nbsp;obj2))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<tt>#t</tt><p></tt><p><blockquote><em>Note:&nbsp;&nbsp;</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&nbsp;<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&nbsp;<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&nbsp;<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&nbsp;<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&nbsp;<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&nbsp;<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=?&nbsp;(symbol-&gt;string&nbsp;<i>obj<sub>1</sub></i>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(symbol-&gt;string&nbsp;<i>obj<sub>2</sub></i>))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<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?&nbsp;'a&nbsp;'a)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<tt>#t</tt><br>(eqv?&nbsp;'a&nbsp;'b)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<tt>#f</tt><br>(eqv?&nbsp;2&nbsp;2)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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t;&nbsp;&nbsp;<tt>#f</tt><br>(let&nbsp;((p&nbsp;(lambda&nbsp;(x)&nbsp;x)))<br>&nbsp;&nbsp;(eqv?&nbsp;p&nbsp;p))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<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?&nbsp;&quot;&quot;&nbsp;&quot;&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<i>unspecified</i><br>(eqv?&nbsp;'#()&nbsp;'#())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<i>unspecified</i><br>(eqv?&nbsp;(lambda&nbsp;(x)&nbsp;x)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lambda&nbsp;(x)&nbsp;x))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<i>unspecified</i><br>(eqv?&nbsp;(lambda&nbsp;(x)&nbsp;x)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lambda&nbsp;(y)&nbsp;y))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<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&nbsp;gen-counter<br>&nbsp;&nbsp;(lambda&nbsp;()<br>&nbsp;&nbsp;&nbsp;&nbsp;(let&nbsp;((n&nbsp;0))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lambda&nbsp;()&nbsp;(set!&nbsp;n&nbsp;(+&nbsp;n&nbsp;1))&nbsp;n))))<br>(let&nbsp;((g&nbsp;(gen-counter)))<br>&nbsp;&nbsp;(eqv?&nbsp;g&nbsp;g))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<tt>#t</tt><br>(eqv?&nbsp;(gen-counter)&nbsp;(gen-counter))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<tt>#f</tt><br>(define&nbsp;gen-loser<br>&nbsp;&nbsp;(lambda&nbsp;()<br>&nbsp;&nbsp;&nbsp;&nbsp;(let&nbsp;((n&nbsp;0))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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nbsp;(if&nbsp;(eqv?&nbsp;f&nbsp;g)&nbsp;'f&nbsp;'both)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(g&nbsp;(lambda&nbsp;()&nbsp;(if&nbsp;(eqv?&nbsp;f&nbsp;g)&nbsp;'g&nbsp;'both))))<br>&nbsp;&nbsp;(eqv?&nbsp;f&nbsp;g))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<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?&nbsp;'(a)&nbsp;'(a))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<i>unspecified</i><br>(eqv?&nbsp;&quot;a&quot;&nbsp;&quot;a&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<i>unspecified</i><br>(eqv?&nbsp;'(b)&nbsp;(cdr&nbsp;'(a&nbsp;b)))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<i>unspecified</i><br>(let&nbsp;((x&nbsp;'(a)))<br>&nbsp;&nbsp;(eqv?&nbsp;x&nbsp;x))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;===&gt;&nbsp;&nbsp;<tt>#t</tt><p></tt><p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -