<!-- 95% W3C COMPLIANT, 95% CSS FREE, RAW HTML --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"> <title>R5RsScheme Revised(5) Report on the Algorithmic Language Scheme</title> <style type="text/css"> <!-- pre { font-family: monospace } tt { font-family: monospace } code { font-family: monospace } p.flushright { text-align: right } p.flushleft { text-align: left } span.sc { font-variant: small-caps } span.sf { font-family: sans-serif } span.skribetitle { font-family: sans-serif; font-weight: bolder; font-size: x-large; } span.refscreen { } span.refprint { display: none; } --> </style> </head> <body class="chapter" bgcolor="#ffffff"> <table width="100%" class="skribetitle" cellspacing="0" cellpadding="0"><tbody> <tr><td align="center" bgcolor="#8381de"><div class="skribetitle"><strong><big><big><big>6. R5Rs<br/>Scheme Revised(5) Report on the Algorithmic Language Scheme -- Standard procedures</big></big></big></strong></div><center> </center> </td></tr></tbody></table> <table cellpadding="3" cellspacing="0" width="100%" class="skribe-margins"><tr> <td align="left" valign="top" class="skribe-left-margin" width="20%" bgcolor="#dedeff"><div class="skribe-left-margin"> <br/><center id='center7883' ><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody> <tr bgcolor="#8381de"><th id="tc7873" align="center" colspan="1"><font color="#ffffff"><strong id='bold7871' >main page</strong></font></th></tr> <tr bgcolor="#ffffff"><td id="tc7880" align="center" colspan="1"><table width="100%" border="0" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc7876" align="left" valign="top" colspan="1"><strong id='bold7875' >top:</strong></td><td id="tc7877" align="right" valign="top" colspan="1"><a href="r5rs.html#R5Rs-Scheme-Revised(5)-Report-on-the-Algorithmic-Language-Scheme" class="inbound">R5Rs<br/>Scheme Revised(5) Report on the Algorithmic Language Scheme</a></td></tr> </tbody></table> </td></tr> </tbody></table> </center> <br/><br/><center id='center7893' ><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody> <tr bgcolor="#8381de"><th id="tc7887" align="center" colspan="1"><font color="#ffffff"><strong id='bold7885' >Standard procedures</strong></font></th></tr> <tr bgcolor="#ffffff"><td id="tc7890" align="center" colspan="1"><table cellspacing="1" cellpadding="1" width="100%" class="toc"> <tbody> <tr><td valign="top" align="left">6.1</td><td colspan="4" width="100%"><a href="r5rs-9.html#Equivalence-predicates">Equivalence predicates</a></td></tr> <tr><td valign="top" align="left">6.2</td><td colspan="4" width="100%"><a href="r5rs-9.html#Numbers">Numbers</a></td></tr> <tr><td></td><td valign="top" align="left">6.2.1</td><td colspan="3" width="100%"><a href="r5rs-9.html#Numerical-types">Numerical types</a></td></tr> <tr><td></td><td valign="top" align="left">6.2.2</td><td colspan="3" width="100%"><a href="r5rs-9.html#Exactness">Exactness</a></td></tr> <tr><td></td><td valign="top" align="left">6.2.3</td><td colspan="3" width="100%"><a href="r5rs-9.html#Implementation-restrictions">Implementation restrictions</a></td></tr> <tr><td></td><td valign="top" align="left">6.2.4</td><td colspan="3" width="100%"><a href="r5rs-9.html#Syntax-of-numerical-constants">Syntax of numerical constants</a></td></tr> <tr><td></td><td valign="top" align="left">6.2.5</td><td colspan="3" width="100%"><a href="r5rs-9.html#Numerical-operations">Numerical operations</a></td></tr> <tr><td></td><td valign="top" align="left">6.2.6</td><td colspan="3" width="100%"><a href="r5rs-9.html#Numerical-input-and-output">Numerical input and output</a></td></tr> <tr><td valign="top" align="left">6.3</td><td colspan="4" width="100%"><a href="r5rs-9.html#Other-data-types">Other data types</a></td></tr> <tr><td></td><td valign="top" align="left">6.3.1</td><td colspan="3" width="100%"><a href="r5rs-9.html#Booleans">Booleans</a></td></tr> <tr><td></td><td valign="top" align="left">6.3.2</td><td colspan="3" width="100%"><a href="r5rs-9.html#Pairs-and-lists">Pairs and lists</a></td></tr> <tr><td></td><td valign="top" align="left">6.3.3</td><td colspan="3" width="100%"><a href="r5rs-9.html#Symbols">Symbols</a></td></tr> <tr><td></td><td valign="top" align="left">6.3.4</td><td colspan="3" width="100%"><a href="r5rs-9.html#Characters">Characters</a></td></tr> <tr><td></td><td valign="top" align="left">6.3.5</td><td colspan="3" width="100%"><a href="r5rs-9.html#Strings">Strings</a></td></tr> <tr><td></td><td valign="top" align="left">6.3.6</td><td colspan="3" width="100%"><a href="r5rs-9.html#Vectors">Vectors</a></td></tr> <tr><td valign="top" align="left">6.4</td><td colspan="4" width="100%"><a href="r5rs-9.html#Control-features">Control features</a></td></tr> <tr><td valign="top" align="left">6.5</td><td colspan="4" width="100%"><a href="r5rs-9.html#Eval">Eval</a></td></tr> <tr><td valign="top" align="left">6.6</td><td colspan="4" width="100%"><a href="r5rs-9.html#Input-and-output">Input and output</a></td></tr> <tr><td></td><td valign="top" align="left">6.6.1</td><td colspan="3" width="100%"><a href="r5rs-9.html#Ports">Ports</a></td></tr> <tr><td></td><td valign="top" align="left">6.6.2</td><td colspan="3" width="100%"><a href="r5rs-9.html#Input">Input</a></td></tr> <tr><td></td><td valign="top" align="left">6.6.3</td><td colspan="3" width="100%"><a href="r5rs-9.html#Output">Output</a></td></tr> <tr><td></td><td valign="top" align="left">6.6.4</td><td colspan="3" width="100%"><a href="r5rs-9.html#System-interface">System interface</a></td></tr> </tbody> </table> </td></tr> </tbody></table> </center> <br/><br/><center id='center7903' ><table width="97%" border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" frame="box" rules="none"><tbody> <tr bgcolor="#8381de"><th id="tc7897" align="center" colspan="1"><font color="#ffffff"><strong id='bold7895' >Chapters</strong></font></th></tr> <tr bgcolor="#ffffff"><td id="tc7900" align="center" colspan="1"><table cellspacing="1" cellpadding="1" width="100%" class="toc"> <tbody> <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-1.html#Summary">Summary</a></td></tr> <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-2.html#Introduction">Introduction</a></td></tr> <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-3.html#Table-of-contents">Table of contents</a></td></tr> <tr><td valign="top" align="left">1</td><td colspan="4" width="100%"><a href="r5rs-4.html#Overview-of-Scheme">Overview of Scheme</a></td></tr> <tr><td valign="top" align="left">2</td><td colspan="4" width="100%"><a href="r5rs-5.html#Lexical-conventions">Lexical conventions</a></td></tr> <tr><td valign="top" align="left">3</td><td colspan="4" width="100%"><a href="r5rs-6.html#Basic-concepts">Basic concepts</a></td></tr> <tr><td valign="top" align="left">4</td><td colspan="4" width="100%"><a href="r5rs-7.html#Expressions">Expressions</a></td></tr> <tr><td valign="top" align="left">5</td><td colspan="4" width="100%"><a href="r5rs-8.html#Program-structure">Program structure</a></td></tr> <tr><td valign="top" align="left">6</td><td colspan="4" width="100%"><a href="r5rs-9.html#Standard-procedures">Standard procedures</a></td></tr> <tr><td valign="top" align="left">7</td><td colspan="4" width="100%"><a href="r5rs-10.html#Formal-syntax-and-semantics">Formal syntax and semantics</a></td></tr> <tr><td valign="top" align="left">8</td><td colspan="4" width="100%"><a href="r5rs-11.html#Concepts">Concepts</a></td></tr> <tr><td valign="top" align="left">9</td><td colspan="4" width="100%"><a href="r5rs-12.html#Variables-and-Procedures">Variables and Procedures</a></td></tr> <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-13.html#Notes">Notes</a></td></tr> <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-14.html#Additional-material">Additional material</a></td></tr> <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-15.html#Example">Example</a></td></tr> <tr><td valign="top" align="left"></td><td colspan="4" width="100%"><a href="r5rs-16.html#Bibliography">Bibliography</a></td></tr> </tbody> </table> </td></tr> </tbody></table> </center> </div></td> <td align="left" valign="top" class="skribe-body"><div class="skribe-body"> <a name="Standard-procedures" class="mark"></a> <a name="g2648" class="mark"></a> <a name="g2651" class="mark"></a> <a name="g2654" class="mark"></a> This chapter describes Scheme's built-in procedures. The initial (or ``top level'') Scheme environment starts out with a number of variables bound to locations containing useful values, most of which are primitive procedures that manipulate data. For example, the variable <samp id='samp2656' >abs</samp> is bound to (a location initially containing) a procedure of one argument that computes the absolute value of a number, and the variable <samp id='samp2657' >+</samp> is bound to a procedure that computes sums. Built-in procedures that can easily be written in terms of other built-in procedures are identified as ``library procedures''.<br/><br/>A program may use a top-level definition to bind any variable. It may subsequently alter any such binding by an assignment (see <a href="r5rs-7.html#Assignments" class="inbound">Assignments</a>). These operations do not modify the behavior of Scheme's built-in procedures. Altering any top-level binding that has not been introduced by a definition has an unspecified effect on the behavior of the built-in procedures.<br/><br/><!-- Equivalence predicates --> <a name="Equivalence-predicates"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">6.1 Equivalence predicates</font> </h3></td></tr></table> </div><div class="section"> <a name="Equivalence-predicates" class="mark"></a> A "predicate" is a procedure that always returns a boolean <a name="g2661" class="mark"></a>value (<tt id='tt2663' >#t</tt> or <tt id='tt2664' >#f</tt>). An "equivalence predicate" is <a name="g2666" class="mark"></a>the computational analogue of a mathematical equivalence relation (it is symmetric, reflexive, and transitive). Of the equivalence predicates described in this section, <samp id='samp2668' >eq?</samp> is the finest or most discriminating, and <samp id='samp2669' >equal?</samp> is the coarsest. <samp id='samp2670' >Eqv?</samp> is slightly less discriminating than <samp id='samp2671' >eq?</samp>. <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g2673" class="mark"></a><a name="eqv?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc2677" align="left" colspan="1"><strong id='bold2675' >eqv?</strong><em id='it2676' > obj1 obj2</em></td><td id="tc2678" align="right" colspan="1">procedure</td></tr> </tbody></table> The <samp id='samp2681' >eqv?</samp> procedure defines a useful equivalence relation on objects. Briefly, it returns <tt id='tt2682' >#t</tt> if <code id='code2684' ><em id='it2683' >obj1</em></code> and <code id='code2686' ><em id='it2685' >obj2</em></code> should normally be regarded as the same object. This relation is left slightly open to interpretation, but the following partial specification of <samp id='samp2687' >eqv?</samp> holds for all implementations of Scheme.<br/><br/>The <samp id='samp2689' >eqv?</samp> procedure returns <tt id='tt2690' >#t</tt> if:<br/><br/><ul class="itemize" id='itemize2749' ><li><code id='code2693' ><em id='it2692' >obj1</em></code> and <code id='code2695' ><em id='it2694' >obj2</em></code> are both <tt id='tt2696' >#t</tt> or both <tt id='tt2697' >#f</tt>.<br/><br/></li> <li><code id='code2701' ><em id='it2700' >obj1</em></code> and <code id='code2703' ><em id='it2702' >obj2</em></code> are both symbols and<br/><br/><center id='center2707' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2705' >(string=? (symbol->string obj1) (symbol->string obj2)) => #t </pre> </td></tr> </tbody></table></center> <font size="-1"><em id='emph2708' >Note:</em> This assumes that neither <code id='code2710' ><em id='it2709' >obj1</em></code> nor <code id='code2712' ><em id='it2711' >obj2</em></code> is an ``uninterned symbol'' as alluded to in section <a href="r5rs-9.html#Symbols" class="inbound">Symbols</a>. This report does not presume to specify the behavior of <samp id='samp2713' >eqv?</samp> on implementation-dependent extensions. </font><br/><br/></li> <li><code id='code2718' ><em id='it2717' >obj1</em></code> and <code id='code2720' ><em id='it2719' >obj2</em></code> are both numbers, are numerically equal (see <samp id='samp2721' >=</samp>, section <a href="r5rs-9.html#Numbers" class="inbound">Numbers</a>), and are either both exact or both inexact.<br/><br/></li> <li><code id='code2725' ><em id='it2724' >obj1</em></code> and <code id='code2727' ><em id='it2726' >obj2</em></code> are both characters and are the same character according to the <samp id='samp2728' >char=?</samp> procedure (section <a href="r5rs-9.html#Characters" class="inbound">Characters</a>).<br/><br/></li> <li>both <code id='code2732' ><em id='it2731' >obj1</em></code> and <code id='code2734' ><em id='it2733' >obj2</em></code> are the empty list.<br/><br/></li> <li><code id='code2738' ><em id='it2737' >obj1</em></code> and <code id='code2740' ><em id='it2739' >obj2</em></code> are pairs, vectors, or strings that denote the same locations in the store (section <a href="r5rs-6.html#Storage-model" class="inbound">Storage model</a>).<br/><br/></li> <li><code id='code2744' ><em id='it2743' >obj1</em></code> and <code id='code2746' ><em id='it2745' >obj2</em></code> are procedures whose location tags are equal (section <a href="r5rs-7.html#Procedures" class="inbound">Procedures</a>).<br/><br/></li> </ul> <a name="g2751" class="mark"></a><a name="g2754" class="mark"></a> The <samp id='samp2756' >eqv?</samp> procedure returns <tt id='tt2757' >#f</tt> if:<br/><br/><ul class="itemize" id='itemize2826' ><li><code id='code2760' ><em id='it2759' >obj1</em></code> and <code id='code2762' ><em id='it2761' >obj2</em></code> are of different types (section <a href="r5rs-6.html#Disjointness-of-types" class="inbound">Disjointness of types</a>).<br/><br/></li> <li>one of <code id='code2766' ><em id='it2765' >obj1</em></code> and <code id='code2768' ><em id='it2767' >obj2</em></code> is <tt id='tt2769' >#t</tt> but the other is <tt id='tt2770' >#f</tt>.<br/><br/></li> <li><code id='code2774' ><em id='it2773' >obj1</em></code> and <code id='code2776' ><em id='it2775' >obj2</em></code> are symbols but<br/><br/><center id='center2784' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2782' >(string=? (symbol->string <code id='code2779' ><em id='it2778' >obj1</em></code>) (symbol->string <code id='code2781' ><em id='it2780' >obj2</em></code>)) => #f </pre> </td></tr> </tbody></table></center> </li> <li>one of <code id='code2787' ><em id='it2786' >obj1</em></code> and <code id='code2789' ><em id='it2788' >obj2</em></code> is an exact number but the other is an inexact number.<br/><br/></li> <li><code id='code2793' ><em id='it2792' >obj1</em></code> and <code id='code2795' ><em id='it2794' >obj2</em></code> are numbers for which the <samp id='samp2796' >=</samp> procedure returns <tt id='tt2797' >#f</tt>.<br/><br/></li> <li><code id='code2801' ><em id='it2800' >obj1</em></code> and <code id='code2803' ><em id='it2802' >obj2</em></code> are characters for which the <samp id='samp2804' >char=?</samp> procedure returns <tt id='tt2805' >#f</tt>.<br/><br/></li> <li>one of <code id='code2809' ><em id='it2808' >obj1</em></code> and <code id='code2811' ><em id='it2810' >obj2</em></code> is the empty list but the other is not.<br/><br/></li> <li><code id='code2815' ><em id='it2814' >obj1</em></code> and <code id='code2817' ><em id='it2816' >obj2</em></code> are pairs, vectors, or strings that denote distinct locations.<br/><br/></li> <li><code id='code2821' ><em id='it2820' >obj1</em></code> and <code id='code2823' ><em id='it2822' >obj2</em></code> are procedures that would behave differently (return different value(s) or have different side effects) for some arguments.<br/><br/></li> </ul> <center id='center2833' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2831' >(eqv? 'a 'a) => #t (eqv? 'a 'b) => #f (eqv? 2 2) => #t (eqv? '() '()) => #t (eqv? 100000000 100000000) => #t (eqv? (cons 1 2) (cons 1 2)) => #f (eqv? (<strong id='bold7904' >lambda</strong> () 1) (<strong id='bold7905' >lambda</strong> () 2)) => #f (eqv? #f 'nil) => #f (<strong id='bold7906' >let</strong> ((p (<strong id='bold7907' >lambda</strong> (x) x))) (eqv? p p)) => #t </pre> </td></tr> </tbody></table></center> The following examples illustrate cases in which the above rules do not fully specify the behavior of <samp id='samp2834' >eqv?</samp>. All that can be said about such cases is that the value returned by <samp id='samp2835' >eqv?</samp> must be a boolean.<br/><br/><center id='center2849' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2847' >(eqv? <font color="red">""</font> <font color="red">""</font>) => <em id='emph2837' >unspecified</em> (eqv? '#() '#()) => <em id='emph2838' >unspecified</em> (eqv? (<strong id='bold7910' >lambda</strong> (x) x) (<strong id='bold7911' >lambda</strong> (x) x)) => <em id='emph2839' >unspecified</em> (eqv? (<strong id='bold7912' >lambda</strong> (x) x) (<strong id='bold7913' >lambda</strong> (y) y)) => <em id='emph2840' >unspecified</em> </pre> </td></tr> </tbody></table></center> The next set of examples shows the use of <samp id='samp2850' >eqv?</samp> with procedures that have local state. <samp id='samp2851' >Gen-counter</samp> must return a distinct procedure every time, since each procedure has its own internal counter. <samp id='samp2852' >Gen-loser</samp>, however, returns equivalent procedures each time, since the local state does not affect the value or side effects of the procedures.<br/><br/><center id='center2882' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2880' >(<font color="#6959cf"><strong id='bold7914' >define</strong></font> <font color="#6959cf"><strong id='bold7916' >gen-counter</strong></font> (<strong id='bold7918' >lambda</strong> () (<strong id='bold7919' >let</strong> ((n 0)) (<strong id='bold7920' >lambda</strong> () (<strong id='bold7921' >set!</strong> n (+ n 1)) n)))) (<strong id='bold7922' >let</strong> ((g (gen-counter))) (eqv? g g)) => #t (eqv? (gen-counter) (gen-counter)) => #f (<font color="#6959cf"><strong id='bold7923' >define</strong></font> <font color="#6959cf"><strong id='bold7925' >gen-loser</strong></font> (<strong id='bold7927' >lambda</strong> () (<strong id='bold7928' >let</strong> ((n 0)) (<strong id='bold7929' >lambda</strong> () (<strong id='bold7930' >set!</strong> n (+ n 1)) 27)))) (<strong id='bold7931' >let</strong> ((g (gen-loser))) (eqv? g g)) => #t (eqv? (gen-loser) (gen-loser)) => <em id='emph2854' >unspecified</em><br/><br/>(<strong id='bold7932' >letrec</strong> ((f (<strong id='bold7933' >lambda</strong> () (<strong id='bold7934' >if</strong> (eqv? f g) 'both 'f))) (g (<strong id='bold7935' >lambda</strong> () (<strong id='bold7936' >if</strong> (eqv? f g) 'both 'g)))) (eqv? f g)) => <em id='emph2856' >unspecified</em><br/><br/>(<strong id='bold7937' >letrec</strong> ((f (<strong id='bold7938' >lambda</strong> () (<strong id='bold7939' >if</strong> (eqv? f g) 'f 'both))) (g (<strong id='bold7940' >lambda</strong> () (<strong id='bold7941' >if</strong> (eqv? f g) 'g 'both)))) (eqv? f g)) => #f </pre> </td></tr> </tbody></table></center> Since it is an error to modify constant objects (those returned by literal expressions), implementations are permitted, though not required, to share structure between constants where appropriate. Thus the value of <samp id='samp2883' >eqv?</samp> on constants is sometimes implementation-dependent.<br/><br/><center id='center2893' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2891' >(eqv? '(a) '(a)) => <em id='emph2885' >unspecified</em> (eqv? <font color="red">"a"</font> <font color="red">"a"</font>) => <em id='emph2886' >unspecified</em> (eqv? '(b) (cdr '(a b))) => <em id='emph2887' >unspecified</em> (<strong id='bold7944' >let</strong> ((x '(a))) (eqv? x x)) => #t </pre> </td></tr> </tbody></table></center> <br/><em id='emph2895' >Rationale:</em> The above definition of <samp id='samp2896' >eqv?</samp> allows implementations latitude in their treatment of procedures and literals: implementations are free either to detect or to fail to detect that two procedures or two literals are equivalent to each other, and can decide whether or not to merge representations of equivalent objects by using the same pointer or bit pattern to represent both. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g2900" class="mark"></a><a name="eq?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc2904" align="left" colspan="1"><strong id='bold2902' >eq?</strong><em id='it2903' > obj1 obj2</em></td><td id="tc2905" align="right" colspan="1">procedure</td></tr> </tbody></table> <samp id='samp2908' >Eq?</samp> is similar to <samp id='samp2909' >eqv?</samp> except that in some cases it is capable of discerning distinctions finer than those detectable by <samp id='samp2910' >eqv?</samp>.<br/><br/><samp id='samp2912' >Eq?</samp> and <samp id='samp2913' >eqv?</samp> are guaranteed to have the same behavior on symbols, booleans, the empty list, pairs, procedures, and non-empty strings and vectors. <samp id='samp2914' >Eq?</samp>'s behavior on numbers and characters is implementation-dependent, but it will always return either true or false, and will return true only when <samp id='samp2915' >eqv?</samp> would also return true. <samp id='samp2916' >Eq?</samp> may also behave differently from <samp id='samp2917' >eqv?</samp> on empty vectors and empty strings.<br/><br/><center id='center2936' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2934' >(eq? 'a 'a) => #t (eq? '(a) '(a)) => <em id='emph2919' >unspecified</em> (eq? (list 'a) (list 'a)) => #f (eq? <font color="red">"a"</font> <font color="red">"a"</font>) => <em id='emph2920' >unspecified</em> (eq? <font color="red">""</font> <font color="red">""</font>) => <em id='emph2921' >unspecified</em> (eq? '() '()) => #t (eq? 2 2) => <em id='emph2922' >unspecified</em> (eq? #\A #\A) => <em id='emph2923' >unspecified</em> (eq? car car) => #t (<strong id='bold7949' >let</strong> ((n (+ 2 3))) (eq? n n)) => <em id='emph2924' >unspecified</em> (<strong id='bold7950' >let</strong> ((x '(a))) (eq? x x)) => #t (<strong id='bold7951' >let</strong> ((x '#())) (eq? x x)) => #t (<strong id='bold7952' >let</strong> ((p (<strong id='bold7953' >lambda</strong> (x) x))) (eq? p p)) => #t </pre> </td></tr> </tbody></table></center> <br/><em id='emph2938' >Rationale:</em> It will usually be possible to implement <samp id='samp2939' >eq?</samp> much more efficiently than <samp id='samp2940' >eqv?</samp>, for example, as a simple pointer comparison instead of as some more complicated operation. One reason is that it may not be possible to compute <samp id='samp2941' >eqv?</samp> of two numbers in constant time, whereas <samp id='samp2942' >eq?</samp> implemented as pointer comparison will always finish in constant time. <samp id='samp2943' >Eq?</samp> may be used like <samp id='samp2944' >eqv?</samp> in applications using procedures to implement objects with state since it obeys the same constraints as <samp id='samp2945' >eqv?</samp>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g2949" class="mark"></a><a name="equal?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc2953" align="left" colspan="1"><strong id='bold2951' >equal?</strong><em id='it2952' > obj1 obj2</em></td><td id="tc2954" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp2957' >Equal?</samp> recursively compares the contents of pairs, vectors, and strings, applying <samp id='samp2958' >eqv?</samp> on other objects such as numbers and symbols. A rule of thumb is that objects are generally <samp id='samp2959' >equal?</samp> if they print the same. <samp id='samp2960' >Equal?</samp> may fail to terminate if its arguments are circular data structures.<br/><br/><center id='center2969' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog2967' >(equal? 'a 'a) => #t (equal? '(a) '(a)) => #t (equal? '(a (b) c) '(a (b) c)) => #t (equal? <font color="red">"abc"</font> <font color="red">"abc"</font>) => #t (equal? 2 2) => #t (equal? (make-vector 5 'a) (make-vector 5 'a)) => #t (equal? (<strong id='bold7956' >lambda</strong> (x) x) (<strong id='bold7957' >lambda</strong> (y) y)) => <em id='emph2962' >unspecified</em> </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> </div><br> <!-- Numbers --> <a name="Numbers"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">6.2 Numbers</font> </h3></td></tr></table> </div><div class="section"> <a name="Numbers" class="mark"></a> <a name="g2973" class="mark"></a> Numerical computation has traditionally been neglected by the Lisp community. Until Common Lisp there was no carefully thought out strategy for organizing numerical computation, and with the exception of the MacLisp system [Pitman83] little effort was made to execute numerical code efficiently. This report recognizes the excellent work of the Common Lisp committee and accepts many of their recommendations. In some ways this report simplifies and generalizes their proposals in a manner consistent with the purposes of Scheme.<br/><br/>It is important to distinguish between the mathematical numbers, the Scheme numbers that attempt to model them, the machine representations used to implement the Scheme numbers, and notations used to write numbers. This report uses the types <em id='it2976' >number</em>, <em id='it2977' >complex</em>, <em id='it2978' >real</em>, <em id='it2979' >rational</em>, and <em id='it2980' >integer</em> to refer to both mathematical numbers and Scheme numbers. Machine representations such as fixed point and floating point are referred to by names such as <em id='it2981' >fixnum</em> and <em id='it2982' >flonum</em>.<br/><br/> <!-- Numerical types --> <a name="Numerical-types"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.2.1 Numerical types</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Numerical-types" class="mark"></a> <a name="g2985" class="mark"></a> Mathematically, numbers may be arranged into a tower of subtypes in which each level is a subset of the level above it:<br/><br/><center id='center2990' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog2988' > number complex real rational integer </pre> </td></tr> </tbody></table></center> For example, 3 is an integer. Therefore 3 is also a rational, a real, and a complex. The same is true of the Scheme numbers that model 3. For Scheme numbers, these types are defined by the predicates <code id='code2991' >number?</code>, <code id='code2992' >complex?</code>, <code id='code2993' >real?</code>, <code id='code2994' >rational?</code>, <a name="g2996" class="mark"></a><a name="g2999" class="mark"></a><a name="g3002" class="mark"></a><a name="g3005" class="mark"></a>and <code id='code3007' >integer?</code>. <a name="g3009" class="mark"></a> There is no simple relationship between a number's type and its representation inside a computer. Although most implementations of Scheme will offer at least two different representations of 3, these different representations denote the same integer.<br/><br/> Scheme's numerical operations treat numbers as abstract data, as independent of their representation as possible. Although an implementation of Scheme may use fixnum, flonum, and perhaps other representations for numbers, this should not be apparent to a casual programmer writing simple programs.<br/><br/>It is necessary, however, to distinguish between numbers that are represented exactly and those that may not be. For example, indexes into data structures must be known exactly, as must some polynomial coefficients in a symbolic algebra system. On the other hand, the results of measurements are inherently inexact, and irrational numbers may be approximated by rational and therefore inexact approximations. In order to catch uses of inexact numbers where exact numbers are required, Scheme explicitly distinguishes exact from inexact numbers. This distinction is orthogonal to the dimension of type.<br/><br/></div> <!-- Exactness --> <a name="Exactness"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.2.2 Exactness</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Exactness" class="mark"></a> <a name="g3015" class="mark"></a>Scheme numbers are either <em id='it3017' >exact</em> or <em id='it3018' >inexact</em>. A number is exact if it was written as an exact constant or was derived from exact numbers using only exact operations. A number is inexact if it was written as an inexact constant, if it was derived using inexact ingredients, or if it was derived using inexact operations. Thus inexactness is a contagious property of a number. If two implementations produce exact results for a computation that did not involve inexact intermediate results, the two ultimate results will be mathematically equivalent. This is generally not true of computations involving inexact numbers since approximate methods such as floating point arithmetic may be used, but it is the duty of each implementation to make the result as close as practical to the mathematically ideal result.<br/><br/>Rational operations such as <samp id='samp3020' >+</samp> should always produce exact results when given exact arguments. If the operation is unable to produce an exact result, then it may either report the violation of an implementation restriction or it may silently coerce its result to an inexact value. See section <a href="r5rs-9.html#Implementation-restrictions" class="inbound">Implementation restrictions</a>.<br/><br/>With the exception of <code id='code3022' >inexact->exact</code>, the operations described in <a name="g3024" class="mark"></a>this section must generally return inexact results when given any inexact arguments. An operation may, however, return an exact result if it can prove that the value of the result is unaffected by the inexactness of its arguments. For example, multiplication of any number by an exact zero may produce an exact zero result, even if the other argument is inexact.<br/><br/></div> <!-- Implementation restrictions --> <a name="Implementation-restrictions"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.2.3 Implementation restrictions</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Implementation-restrictions" class="mark"></a> <a name="g3028" class="mark"></a> Implementations of Scheme are not required to implement the whole tower of subtypes given in section <a href="r5rs-9.html#Numerical-types" class="inbound">Numerical types</a>, but they must implement a coherent subset consistent with both the purposes of the implementation and the spirit of the Scheme language. For example, an implementation in which all numbers are real may still be quite useful.<br/><br/>Implementations may also support only a limited range of numbers of any type, subject to the requirements of this section. The supported range for exact numbers of any type may be different from the supported range for inexact numbers of that type. For example, an implementation that uses flonums to represent all its inexact real numbers may support a practically unbounded range of exact integers and rationals while limiting the range of inexact reals (and therefore the range of inexact integers and rationals) to the dynamic range of the flonum format. Furthermore the gaps between the representable inexact integers and rationals are likely to be very large in such an implementation as the limits of this range are approached.<br/><br/>An implementation of Scheme must support exact integers throughout the range of numbers that may be used for indexes of lists, vectors, and strings or that may result from computing the length of a list, vector, or string. The <code id='code3032' >length</code>, <code id='code3033' >vector-length</code>, <a name="g3035" class="mark"></a><a name="g3038" class="mark"></a>and <code id='code3040' >string-length</code> procedures must return an exact <a name="g3042" class="mark"></a>integer, and it is an error to use anything but an exact integer as an index. Furthermore any integer constant within the index range, if expressed by an exact integer syntax, will indeed be read as an exact integer, regardless of any implementation restrictions that may apply outside this range. Finally, the procedures listed below will always return an exact integer result provided all their arguments are exact integers and the mathematically expected result is representable as an exact integer within the implementation:<br/><br/><center id='center3047' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog3045' >+ - * quotient remainder modulo max min abs numerator denominator gcd lcm floor ceiling truncate round rationalize expt </pre> </td></tr> </tbody></table></center> Implementations are encouraged, but not required, to support exact integers and exact rationals of practically unlimited size and precision, and to implement the above procedures and the <samp id='samp3048' >/</samp> procedure in such a way that they always return exact results when given exact arguments. If one of these procedures is unable to deliver an exact result when given exact arguments, then it may either report a violation of an implementation restriction or it may silently coerce its result to an inexact number. Such a coercion may cause an error later.<br/><br/> An implementation may use floating point and other approximate representation strategies for inexact numbers. This report recommends, but does not require, that the IEEE 32-bit and 64-bit floating point standards be followed by implementations that use flonum representations, and that implementations using other representations should match or exceed the precision achievable using these floating point standards [IEEE].<br/><br/>In particular, implementations that use flonum representations must follow these rules: A flonum result must be represented with at least as much precision as is used to express any of the inexact arguments to that operation. It is desirable (but not required) for potentially inexact operations such as <samp id='samp3051' >sqrt</samp>, when applied to exact arguments, to produce exact answers whenever possible (for example the square root of an exact 4 ought to be an exact 2). If, however, an exact number is operated upon so as to produce an inexact result (as by <samp id='samp3052' >sqrt</samp>), and if the result is represented as a flonum, then the most precise flonum format available must be used; but if the result is represented in some other way then the representation must have at least as much precision as the most precise flonum format available.<br/><br/>Although Scheme allows a variety of written notations for numbers, any particular implementation may support only some of them. For example, an implementation in which all numbers are real need not support the rectangular and polar notations for complex numbers. If an implementation encounters an exact numerical constant that it cannot represent as an exact number, then it may either report a violation of an implementation restriction or it may silently represent the constant by an inexact number.<br/><br/></div> <!-- Syntax of numerical constants --> <a name="Syntax-of-numerical-constants"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.2.4 Syntax of numerical constants</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Syntax-of-numerical-constants" class="mark"></a> The syntax of the written representations for numbers is described formally in section <a href="r5rs-10.html#Lexical-structure" class="inbound">Lexical structure</a>. Note that case is not significant in numerical constants.<br/><br/> A number may be written in binary, octal, decimal, or hexadecimal by the use of a radix prefix. The radix prefixes are <samp id='samp3056' >#b</samp> (binary), <samp id='samp3057' >#o</samp> (octal), <samp id='samp3058' >#d</samp> (decimal), and <samp id='samp3059' >#x</samp> (hexadecimal). With <a name="g3060" class="mark"></a><a name="g3062" class="mark"></a><a name="g3064" class="mark"></a><a name="g3066" class="mark"></a><a name="g3068" class="mark"></a><a name="g3070" class="mark"></a><a name="g3072" class="mark"></a>no radix prefix, a number is assumed to be expressed in decimal.<br/><br/>A numerical constant may be specified to be either exact or inexact by a prefix. The prefixes are <samp id='samp3075' >#e</samp> <a name="g3076" class="mark"></a>for exact, and <samp id='samp3078' >#i</samp> for inexact. An exactness <a name="g3079" class="mark"></a>prefix may appear before or after any radix prefix that is used. If the written representation of a number has no exactness prefix, the constant may be either inexact or exact. It is inexact if it contains a decimal point, an exponent, or a ``#'' character in the place of a digit, otherwise it is exact. In systems with inexact numbers of varying precisions it may be useful to specify the precision of a constant. For this purpose, numerical constants may be written with an exponent marker that indicates the desired precision of the inexact representation. The letters <samp id='samp3081' >s</samp>, <samp id='samp3082' >f</samp>, <samp id='samp3083' >d</samp>, and <samp id='samp3084' >l</samp> specify the use of <code id='code3086' ><em id='it3085' >short</em></code>, <code id='code3088' ><em id='it3087' >single</em></code>, <code id='code3090' ><em id='it3089' >double</em></code>, and <code id='code3092' ><em id='it3091' >long</em></code> precision, respectively. (When fewer than four internal inexact representations exist, the four size specifications are mapped onto those available. For example, an implementation with two internal representations may map short and single together and long and double together.) In addition, the exponent marker <samp id='samp3093' >e</samp> specifies the default precision for the implementation. The default precision has at least as much precision as <code id='code3095' ><em id='it3094' >double</em></code>, but implementations may wish to allow this default to be set by the user.<br/><br/><center id='center3099' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3097' >3.14159265358979F0 Round to single --- 3.141593 0.6L0 Extend to long --- .600000000000000 </pre> </td></tr> </tbody></table></center> </div> <!-- Numerical operations --> <a name="Numerical-operations"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.2.5 Numerical operations</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Numerical-operations" class="mark"></a> The reader is referred to section <a href="r5rs-4.html#Entry-format" class="inbound">Entry format</a> for a summary of the naming conventions used to specify restrictions on the types of arguments to numerical routines. The examples used in this section assume that any numerical constant written using an exact notation is indeed represented as an exact number. Some examples also assume that certain numerical constants written using an inexact notation can be represented without loss of accuracy; the inexact constants were chosen so that this is likely to be true in implementations that use flonums to represent inexact numbers.<br/><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3102" class="mark"></a><a name="number?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3106" align="left" colspan="1"><strong id='bold3104' >number?</strong><em id='it3105' > obj</em></td><td id="tc3107" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3111" class="mark"></a><a name="complex?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3115" align="left" colspan="1"><strong id='bold3113' >complex?</strong><em id='it3114' > obj</em></td><td id="tc3116" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3120" class="mark"></a><a name="real?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3124" align="left" colspan="1"><strong id='bold3122' >real?</strong><em id='it3123' > obj</em></td><td id="tc3125" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3129" class="mark"></a><a name="rational?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3133" align="left" colspan="1"><strong id='bold3131' >rational?</strong><em id='it3132' > obj</em></td><td id="tc3134" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3138" class="mark"></a><a name="integer?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3142" align="left" colspan="1"><strong id='bold3140' >integer?</strong><em id='it3141' > obj</em></td><td id="tc3143" align="right" colspan="1">procedure</td></tr> </tbody></table> These numerical type predicates can be applied to any kind of argument, including non-numbers. They return <tt id='tt3146' >#t</tt> if the object is of the named type, and otherwise they return <tt id='tt3147' >#f</tt>. In general, if a type predicate is true of a number then all higher type predicates are also true of that number. Consequently, if a type predicate is false of a number, then all lower type predicates are also false of that number. If <code id='code3149' ><em id='it3148' >z</em></code> is an inexact complex number, then <samp id='samp3152' >(real? <code id='code3151' ><em id='it3150' >z</em></code>)</samp> is true if and only if <samp id='samp3155' >(zero? (imag-part <code id='code3154' ><em id='it3153' >z</em></code>))</samp> is true. If <code id='code3157' ><em id='it3156' >x</em></code> is an inexact real number, then <samp id='samp3160' >(integer? <code id='code3159' ><em id='it3158' >x</em></code>)</samp> is true if and only if <samp id='samp3165' >(= <code id='code3162' ><em id='it3161' >x</em></code> (round <code id='code3164' ><em id='it3163' >x</em></code>))</samp>.<br/><br/><center id='center3169' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3167' >(complex? 3+4i) => #t (complex? 3) => #t (real? 3) => #t (real? -2.5+0.0i) => #t (real? #e1e10) => #t (rational? 6/10) => #t (rational? 6/3) => #t (integer? 3+0i) => #t (integer? 3.0) => #t (integer? 8/4) => #t </pre> </td></tr> </tbody></table></center> <font size="-1"><em id='emph3170' >Note:</em> The behavior of these type predicates on inexact numbers is unreliable, since any inaccuracy may affect the result. </font> <font size="-1"><em id='emph3172' >Note:</em> In many implementations the <code id='code3173' >rational?</code> procedure will be the same <a name="g3175" class="mark"></a>as <code id='code3177' >real?</code>, and the <code id='code3178' >complex?</code> procedure will be the same as <a name="g3180" class="mark"></a><a name="g3183" class="mark"></a><code id='code3185' >number?</code>, but unusual implementations may be able to represent <a name="g3187" class="mark"></a>some irrational numbers exactly or may extend the number system to support some kind of non-complex numbers. </font></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3193" class="mark"></a><a name="exact?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3199" align="left" colspan="1"><strong id='bold3195' >exact?</strong><em id='it3198' > <code id='code3197' ><em id='it3196' >z</em></code></em></td><td id="tc3200" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3204" class="mark"></a><a name="inexact?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3210" align="left" colspan="1"><strong id='bold3206' >inexact?</strong><em id='it3209' > <code id='code3208' ><em id='it3207' >z</em></code></em></td><td id="tc3211" align="right" colspan="1">procedure</td></tr> </tbody></table> These numerical predicates provide tests for the exactness of a quantity. For any Scheme number, precisely one of these predicates is true. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3217" class="mark"></a><a name="=" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3221" align="left" colspan="1"><strong id='bold3219' >=</strong><em id='it3220' > z1 z2 z3 ...,</em></td><td id="tc3222" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3226" class="mark"></a><a name="<" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3230" align="left" colspan="1"><strong id='bold3228' ><</strong><em id='it3229' > x1 x2 x3 ...,</em></td><td id="tc3231" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3235" class="mark"></a><a name=">" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3239" align="left" colspan="1"><strong id='bold3237' >></strong><em id='it3238' > x1 x2 x3 ...,</em></td><td id="tc3240" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3244" class="mark"></a><a name="<=" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3248" align="left" colspan="1"><strong id='bold3246' ><=</strong><em id='it3247' > x1 x2 x3 ...,</em></td><td id="tc3249" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3253" class="mark"></a><a name=">=" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3257" align="left" colspan="1"><strong id='bold3255' >>=</strong><em id='it3256' > x1 x2 x3 ...,</em></td><td id="tc3258" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures return <tt id='tt3261' >#t</tt> if their arguments are (respectively): equal, monotonically increasing, monotonically decreasing, monotonically nondecreasing, or monotonically nonincreasing.<br/><br/>These predicates are required to be transitive.<br/><br/><font size="-1"><em id='emph3264' >Note:</em> The traditional implementations of these predicates in Lisp-like languages are not transitive. </font> <font size="-1"><em id='emph3266' >Note:</em> While it is not an error to compare inexact numbers using these predicates, the results may be unreliable because a small inaccuracy may affect the result; this is especially true of <code id='code3267' >=</code> and <code id='code3268' >zero?</code>. <a name="g3270" class="mark"></a><a name="g3273" class="mark"></a>When in doubt, consult a numerical analyst. </font> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3279" class="mark"></a><a name="zero?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3285" align="left" colspan="1"><strong id='bold3281' >zero?</strong><em id='it3284' > <code id='code3283' ><em id='it3282' >z</em></code></em></td><td id="tc3286" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g3290" class="mark"></a><a name="positive?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3296" align="left" colspan="1"><strong id='bold3292' >positive?</strong><em id='it3295' > <code id='code3294' ><em id='it3293' >x</em></code></em></td><td id="tc3297" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g3301" class="mark"></a><a name="negative?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3307" align="left" colspan="1"><strong id='bold3303' >negative?</strong><em id='it3306' > <code id='code3305' ><em id='it3304' >x</em></code></em></td><td id="tc3308" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g3312" class="mark"></a><a name="odd?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3318" align="left" colspan="1"><strong id='bold3314' >odd?</strong><em id='it3317' > <code id='code3316' ><em id='it3315' >n</em></code></em></td><td id="tc3319" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g3323" class="mark"></a><a name="even?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3329" align="left" colspan="1"><strong id='bold3325' >even?</strong><em id='it3328' > <code id='code3327' ><em id='it3326' >n</em></code></em></td><td id="tc3330" align="right" colspan="1">library procedure</td></tr> </tbody></table> These numerical predicates test a number for a particular property, returning <tt id='tt3333' >#t</tt> or <tt id='tt3334' >#f</tt>. See note above.<br/><br/></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3339" class="mark"></a><a name="max" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3343" align="left" colspan="1"><strong id='bold3341' >max</strong><em id='it3342' > x1 x2 ...,</em></td><td id="tc3344" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g3348" class="mark"></a><a name="min" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3352" align="left" colspan="1"><strong id='bold3350' >min</strong><em id='it3351' > x1 x2 ...,</em></td><td id="tc3353" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures return the maximum or minimum of their arguments.<br/><br/><center id='center3359' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3357' >(max 3 4) => 4 ; exact (max 3.9 4) => 4.0 ; inexact </pre> </td></tr> </tbody></table></center> <font size="-1"><em id='emph3360' >Note:</em> If any argument is inexact, then the result will also be inexact (unless the procedure can prove that the inaccuracy is not large enough to affect the result, which is possible only in unusual implementations). If <samp id='samp3361' >min</samp> or <samp id='samp3362' >max</samp> is used to compare numbers of mixed exactness, and the numerical value of the result cannot be represented as an inexact number without loss of accuracy, then the procedure may report a violation of an implementation restriction. </font></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3367" class="mark"></a><a name="+" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3371" align="left" colspan="1"><strong id='bold3369' >+</strong><em id='it3370' > z1 ...,</em></td><td id="tc3372" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3376" class="mark"></a><a name="*" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3380" align="left" colspan="1"><strong id='bold3378' >*</strong><em id='it3379' > z1 ...,</em></td><td id="tc3381" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures return the sum or product of their arguments. <center id='center3386' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3384' >(+ 3 4) => 7 (+ 3) => 3 (+) => 0 (* 4) => 4 (*) => 1 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3390" class="mark"></a><a name="-" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3394" align="left" colspan="1"><strong id='bold3392' >-</strong><em id='it3393' > z1 z2</em></td><td id="tc3395" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3402" align="left" colspan="1"><strong id='bold3398' >-</strong><em id='it3401' > <code id='code3400' ><em id='it3399' >z</em></code></em></td><td id="tc3403" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3408" align="left" colspan="1"><strong id='bold3406' >-</strong><em id='it3407' > z1 z2 ...,</em></td><td id="tc3409" align="right" colspan="1">optional procedure</td></tr> </tbody></table> <a name="g3413" class="mark"></a><a name="/" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3417" align="left" colspan="1"><strong id='bold3415' >/</strong><em id='it3416' > z1 z2</em></td><td id="tc3418" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3425" align="left" colspan="1"><strong id='bold3421' >/</strong><em id='it3424' > <code id='code3423' ><em id='it3422' >z</em></code></em></td><td id="tc3426" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3431" align="left" colspan="1"><strong id='bold3429' >/</strong><em id='it3430' > z1 z2 ...,</em></td><td id="tc3432" align="right" colspan="1">optional procedure</td></tr> </tbody></table> With two or more arguments, these procedures return the difference or quotient of their arguments, associating to the left. With one argument, however, they return the additive or multiplicative inverse of their argument. <center id='center3437' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3435' >(- 3 4) => -1 (- 3 4 5) => -6 (- 3) => -3 (/ 3 4 5) => 3/20 (/ 3) => 1/3 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3441" class="mark"></a><a name="abs" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3445" align="left" colspan="1"><strong id='bold3443' >abs</strong><em id='it3444' > x</em></td><td id="tc3446" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp3449' >Abs</samp> returns the absolute value of its argument. <center id='center3452' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3450' >(abs -7) => 7 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3456" class="mark"></a><a name="quotient" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3460" align="left" colspan="1"><strong id='bold3458' >quotient</strong><em id='it3459' > n1 n2</em></td><td id="tc3461" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3465" class="mark"></a><a name="remainder" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3469" align="left" colspan="1"><strong id='bold3467' >remainder</strong><em id='it3468' > n1 n2</em></td><td id="tc3470" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3474" class="mark"></a><a name="modulo" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3478" align="left" colspan="1"><strong id='bold3476' >modulo</strong><em id='it3477' > n1 n2</em></td><td id="tc3479" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures implement number-theoretic (integer) division. <code id='code3483' ><em id='it3482' >n2</em></code> should be non-zero. All three procedures return integers. If <code id='code3485' ><em id='it3484' >n1</em></code>/<code id='code3487' ><em id='it3486' >n2</em></code> is an integer:<br/><br/><center id='center3507' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3505' > (quotient <code id='code3490' ><em id='it3489' >n1</em></code> <code id='code3492' ><em id='it3491' >n2</em></code>) => <code id='code3494' ><em id='it3493' >n1</em></code>/<code id='code3496' ><em id='it3495' >n2</em></code> (remainder <code id='code3498' ><em id='it3497' >n1</em></code> <code id='code3500' ><em id='it3499' >n2</em></code>) => 0 (modulo <code id='code3502' ><em id='it3501' >n1</em></code> <code id='code3504' ><em id='it3503' >n2</em></code>) => 0 </pre> </td></tr> </tbody></table></center> If <code id='code3509' ><em id='it3508' >n1</em></code>/<code id='code3511' ><em id='it3510' >n2</em></code> is not an integer:<br/><br/><center id='center3533' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3531' > (quotient <code id='code3514' ><em id='it3513' >n1</em></code> <code id='code3516' ><em id='it3515' >n2</em></code>) => <code id='code3518' ><em id='it3517' >n_q</em></code> (remainder <code id='code3520' ><em id='it3519' >n1</em></code> <code id='code3522' ><em id='it3521' >n2</em></code>) => <code id='code3524' ><em id='it3523' >n_r</em></code> (modulo <code id='code3526' ><em id='it3525' >n1</em></code> <code id='code3528' ><em id='it3527' >n2</em></code>) => <code id='code3530' ><em id='it3529' >n_m</em></code> </pre> </td></tr> </tbody></table></center> where <code id='code3535' ><em id='it3534' >n_q</em></code> is <code id='code3537' ><em id='it3536' >n1</em></code>/<code id='code3539' ><em id='it3538' >n2</em></code> rounded towards zero, 0 < |<code id='code3541' ><em id='it3540' >n_r</em></code>| < |<code id='code3543' ><em id='it3542' >n2</em></code>|, 0 < |<code id='code3545' ><em id='it3544' >n_m</em></code>| < |<code id='code3547' ><em id='it3546' >n2</em></code>|, <code id='code3549' ><em id='it3548' >n_r</em></code> and <code id='code3551' ><em id='it3550' >n_m</em></code> differ from <code id='code3553' ><em id='it3552' >n1</em></code> by a multiple of <code id='code3555' ><em id='it3554' >n2</em></code>, <code id='code3557' ><em id='it3556' >n_r</em></code> has the same sign as <code id='code3559' ><em id='it3558' >n1</em></code>, and <code id='code3561' ><em id='it3560' >n_m</em></code> has the same sign as <code id='code3563' ><em id='it3562' >n2</em></code>.<br/><br/>From this we can conclude that for integers <code id='code3566' ><em id='it3565' >n1</em></code> and <code id='code3568' ><em id='it3567' >n2</em></code> with <code id='code3570' ><em id='it3569' >n2</em></code> not equal to 0,<br/><br/><center id='center3586' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3584' > (= <code id='code3573' ><em id='it3572' >n1</em></code> (+ (* <code id='code3575' ><em id='it3574' >n2</em></code> (quotient <code id='code3577' ><em id='it3576' >n1</em></code> <code id='code3579' ><em id='it3578' >n2</em></code>)) (remainder <code id='code3581' ><em id='it3580' >n1</em></code> <code id='code3583' ><em id='it3582' >n2</em></code>))) => #t </pre> </td></tr> </tbody></table></center> provided all numbers involved in that computation are exact.<br/><br/><center id='center3594' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3592' >(modulo 13 4) => 1 (remainder 13 4) => 1<br/><br/>(modulo -13 4) => 3 (remainder -13 4) => -1<br/><br/>(modulo 13 -4) => -3 (remainder 13 -4) => 1<br/><br/>(modulo -13 -4) => -1 (remainder -13 -4) => -1<br/><br/>(remainder -13 -4.0) => -1.0 ; inexact </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3598" class="mark"></a><a name="gcd" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3602" align="left" colspan="1"><strong id='bold3600' >gcd</strong><em id='it3601' > n1 ...,</em></td><td id="tc3603" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g3607" class="mark"></a><a name="lcm" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3611" align="left" colspan="1"><strong id='bold3609' >lcm</strong><em id='it3610' > n1 ...,</em></td><td id="tc3612" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures return the greatest common divisor or least common multiple of their arguments. The result is always non-negative. <center id='center3617' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3615' >(gcd 32 -36) => 4 (gcd) => 0 (lcm 32 -36) => 288 (lcm 32.0 -36) => 288.0 ; inexact (lcm) => 1 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3621" class="mark"></a><a name="numerator" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3627" align="left" colspan="1"><strong id='bold3623' >numerator</strong><em id='it3626' > <code id='code3625' ><em id='it3624' >q</em></code></em></td><td id="tc3628" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3632" class="mark"></a><a name="denominator" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3638" align="left" colspan="1"><strong id='bold3634' >denominator</strong><em id='it3637' > <code id='code3636' ><em id='it3635' >q</em></code></em></td><td id="tc3639" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures return the numerator or denominator of their argument; the result is computed as if the argument was represented as a fraction in lowest terms. The denominator is always positive. The denominator of 0 is defined to be 1. <center id='center3644' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3642' >(numerator (/ 6 4)) => 3 (denominator (/ 6 4)) => 2 (denominator (exact->inexact (/ 6 4))) => 2.0 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3648" class="mark"></a><a name="floor" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3652" align="left" colspan="1"><strong id='bold3650' >floor</strong><em id='it3651' > x</em></td><td id="tc3653" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3657" class="mark"></a><a name="ceiling" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3661" align="left" colspan="1"><strong id='bold3659' >ceiling</strong><em id='it3660' > x</em></td><td id="tc3662" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3666" class="mark"></a><a name="truncate" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3670" align="left" colspan="1"><strong id='bold3668' >truncate</strong><em id='it3669' > x</em></td><td id="tc3671" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3675" class="mark"></a><a name="round" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3679" align="left" colspan="1"><strong id='bold3677' >round</strong><em id='it3678' > x</em></td><td id="tc3680" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures return integers. <samp id='samp3683' >Floor</samp> returns the largest integer not larger than <code id='code3685' ><em id='it3684' >x</em></code>. <samp id='samp3686' >Ceiling</samp> returns the smallest integer not smaller than <code id='code3688' ><em id='it3687' >x</em></code>. <samp id='samp3689' >Truncate</samp> returns the integer closest to <code id='code3691' ><em id='it3690' >x</em></code> whose absolute value is not larger than the absolute value of <code id='code3693' ><em id='it3692' >x</em></code>. <samp id='samp3694' >Round</samp> returns the closest integer to <code id='code3696' ><em id='it3695' >x</em></code>, rounding to even when <code id='code3698' ><em id='it3697' >x</em></code> is halfway between two integers.<br/><br/><br/><em id='emph3701' >Rationale:</em> <samp id='samp3702' >Round</samp> rounds to even for consistency with the default rounding mode specified by the IEEE floating point standard.<br/><br/><font size="-1"><em id='emph3704' >Note:</em> If the argument to one of these procedures is inexact, then the result will also be inexact. If an exact value is needed, the result should be passed to the <samp id='samp3705' >inexact->exact</samp> procedure. </font> <center id='center3711' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3709' >(floor -4.3) => -5.0 (ceiling -4.3) => -4.0 (truncate -4.3) => -4.0 (round -4.3) => -4.0<br/><br/>(floor 3.5) => 3.0 (ceiling 3.5) => 4.0 (truncate 3.5) => 3.0 (round 3.5) => 4.0 ; inexact<br/><br/>(round 7/2) => 4 ; exact (round 7) => 7 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3715" class="mark"></a><a name="rationalize" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3719" align="left" colspan="1"><strong id='bold3717' >rationalize</strong><em id='it3718' > x y</em></td><td id="tc3720" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp3723' >Rationalize</samp> returns the <em id='emph3724' >simplest</em> rational number differing from <code id='code3726' ><em id='it3725' >x</em></code> by no more than <code id='code3728' ><em id='it3727' >y</em></code>. A rational number r_1 is <em id='emph3729' >simpler</em> than another rational number <a name="g3731" class="mark"></a>r_2 if r_1 = p_1/q_1 and r_2 = p_2/q_2 (in lowest terms) and |p_1|<= |p_2| and |q_1| <= |q_2|. Thus 3/5 is simpler than 4/7. Although not all rationals are comparable in this ordering (consider 2/7 and 3/5) any interval contains a rational number that is simpler than every other rational number in that interval (the simpler 2/5 lies between 2/7 and 3/5). Note that 0 = 0/1 is the simplest rational of all.<br/><br/><center id='center3736' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog3734' >(rationalize (inexact->exact .3) 1/10) => 1/3 ; exact (rationalize .3 1/10) => #i1/3 ; inexact </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3740" class="mark"></a><a name="exp" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3746" align="left" colspan="1"><strong id='bold3742' >exp</strong><em id='it3745' > <code id='code3744' ><em id='it3743' >z</em></code></em></td><td id="tc3747" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3751" class="mark"></a><a name="log" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3757" align="left" colspan="1"><strong id='bold3753' >log</strong><em id='it3756' > <code id='code3755' ><em id='it3754' >z</em></code></em></td><td id="tc3758" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3762" class="mark"></a><a name="sin" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3768" align="left" colspan="1"><strong id='bold3764' >sin</strong><em id='it3767' > <code id='code3766' ><em id='it3765' >z</em></code></em></td><td id="tc3769" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3773" class="mark"></a><a name="cos" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3779" align="left" colspan="1"><strong id='bold3775' >cos</strong><em id='it3778' > <code id='code3777' ><em id='it3776' >z</em></code></em></td><td id="tc3780" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3784" class="mark"></a><a name="tan" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3790" align="left" colspan="1"><strong id='bold3786' >tan</strong><em id='it3789' > <code id='code3788' ><em id='it3787' >z</em></code></em></td><td id="tc3791" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3795" class="mark"></a><a name="asin" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3801" align="left" colspan="1"><strong id='bold3797' >asin</strong><em id='it3800' > <code id='code3799' ><em id='it3798' >z</em></code></em></td><td id="tc3802" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3806" class="mark"></a><a name="acos" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3812" align="left" colspan="1"><strong id='bold3808' >acos</strong><em id='it3811' > <code id='code3810' ><em id='it3809' >z</em></code></em></td><td id="tc3813" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3817" class="mark"></a><a name="atan" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3823" align="left" colspan="1"><strong id='bold3819' >atan</strong><em id='it3822' > <code id='code3821' ><em id='it3820' >z</em></code></em></td><td id="tc3824" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3833" align="left" colspan="1"><strong id='bold3827' >atan</strong><em id='it3832' > <code id='code3829' ><em id='it3828' >y</em></code> <code id='code3831' ><em id='it3830' >x</em></code></em></td><td id="tc3834" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures are part of every implementation that supports general real numbers; they compute the usual transcendental functions. <samp id='samp3837' >Log</samp> computes the natural logarithm of <code id='code3839' ><em id='it3838' >z</em></code> (not the base ten logarithm). <samp id='samp3840' >Asin</samp>, <samp id='samp3841' >acos</samp>, and <samp id='samp3842' >atan</samp> compute arcsine (sin^-1), arccosine (cos^-1), and arctangent (tan^-1), respectively. The two-argument variant of <samp id='samp3843' >atan</samp> computes <tt id='tt3848' >(angle (make-rectangular <code id='code3845' ><em id='it3844' >x</em></code> <code id='code3847' ><em id='it3846' >y</em></code>))</tt> (see below), even in implementations that don't support general complex numbers.<br/><br/>In general, the mathematical functions log, arcsine, arccosine, and arctangent are multiply defined. The value of log z is defined to be the one whose imaginary part lies in the range from -pi (exclusive) to pi (inclusive). log 0 is undefined. With log defined this way, the values of sin^-1 z, cos^-1 z, and tan^-1 z are according to the following formulae:<br/><br/><center id='center3851' >sin^-1 z = -i log (i z + sqrt1 - z^2)</center> <center id='center3852' >cos^-1 z = pi / 2 - sin^-1 z</center> <center id='center3853' >tan^-1 z = (log (1 + i z) - log (1 - i z)) / (2 i)</center> The above specification follows [CLtL], which in turn cites [Penfield81]; refer to these sources for more detailed discussion of branch cuts, boundary conditions, and implementation of these functions. When it is possible these procedures produce a real result from a real argument.<br/><br/> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3858" class="mark"></a><a name="sqrt" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3864" align="left" colspan="1"><strong id='bold3860' >sqrt</strong><em id='it3863' > <code id='code3862' ><em id='it3861' >z</em></code></em></td><td id="tc3865" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the principal square root of <code id='code3869' ><em id='it3868' >z</em></code>. The result will have either positive real part, or zero real part and non-negative imaginary part. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3873" class="mark"></a><a name="expt" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3877" align="left" colspan="1"><strong id='bold3875' >expt</strong><em id='it3876' > z1 z2</em></td><td id="tc3878" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <code id='code3882' ><em id='it3881' >z1</em></code> raised to the power <code id='code3884' ><em id='it3883' >z2</em></code>. For z_1 ~= 0<br/><br/><center id='center3886' >z_1^z_2 = e^z_2 log z_1</center> 0^z is 1 if z = 0 and 0 otherwise. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g3890" class="mark"></a><a name="make-rectangular" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3894" align="left" colspan="1"><strong id='bold3892' >make-rectangular</strong><em id='it3893' > x1 x2</em></td><td id="tc3895" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3899" class="mark"></a><a name="make-polar" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3903" align="left" colspan="1"><strong id='bold3901' >make-polar</strong><em id='it3902' > x3 x4</em></td><td id="tc3904" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3908" class="mark"></a><a name="real-part" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3914" align="left" colspan="1"><strong id='bold3910' >real-part</strong><em id='it3913' > <code id='code3912' ><em id='it3911' >z</em></code></em></td><td id="tc3915" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3919" class="mark"></a><a name="imag-part" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3925" align="left" colspan="1"><strong id='bold3921' >imag-part</strong><em id='it3924' > <code id='code3923' ><em id='it3922' >z</em></code></em></td><td id="tc3926" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3930" class="mark"></a><a name="magnitude" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3936" align="left" colspan="1"><strong id='bold3932' >magnitude</strong><em id='it3935' > <code id='code3934' ><em id='it3933' >z</em></code></em></td><td id="tc3937" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g3941" class="mark"></a><a name="angle" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc3947" align="left" colspan="1"><strong id='bold3943' >angle</strong><em id='it3946' > <code id='code3945' ><em id='it3944' >z</em></code></em></td><td id="tc3948" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures are part of every implementation that supports general complex numbers. Suppose <code id='code3952' ><em id='it3951' >x1</em></code>, <code id='code3954' ><em id='it3953' >x2</em></code>, <code id='code3956' ><em id='it3955' >x3</em></code>, and <code id='code3958' ><em id='it3957' >x4</em></code> are real numbers and <code id='code3960' ><em id='it3959' >z</em></code> is a complex number such that<br/><br/><center id='center3974' > <code id='code3963' ><em id='it3962' >z</em></code> = <code id='code3965' ><em id='it3964' >x1</em></code> + <code id='code3967' ><em id='it3966' >x2</em></code><strong id='bold3968' >i</strong> = <code id='code3970' ><em id='it3969' >x3</em></code> . e^<strong id='bold3971' >i</strong> <code id='code3973' ><em id='it3972' >x4</em></code></center> Then<br/><br/><center id='center4004' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4002' >(make-rectangular <code id='code3977' ><em id='it3976' >x1</em></code> <code id='code3979' ><em id='it3978' >x2</em></code>) => <code id='code3981' ><em id='it3980' >z</em></code> (make-polar <code id='code3983' ><em id='it3982' >x3</em></code> <code id='code3985' ><em id='it3984' >x4</em></code>) => <code id='code3987' ><em id='it3986' >z</em></code> (real-part <code id='code3989' ><em id='it3988' >z</em></code>) => <code id='code3991' ><em id='it3990' >x1</em></code> (imag-part <code id='code3993' ><em id='it3992' >z</em></code>) => <code id='code3995' ><em id='it3994' >x2</em></code> (magnitude <code id='code3997' ><em id='it3996' >z</em></code>) => |<code id='code3999' ><em id='it3998' >x3</em></code>| (angle <code id='code4001' ><em id='it4000' >z</em></code>) => x_angle </pre> </td></tr> </tbody></table></center> where -pi < x_angle <= pi with x_angle = <code id='code4006' ><em id='it4005' >x4</em></code> + 2pi n for some integer n.<br/><br/><br/><em id='emph4009' >Rationale:</em> <samp id='samp4010' >Magnitude</samp> is the same as <code id='code4011' >abs</code> for a real argument, <a name="g4013" class="mark"></a>but <samp id='samp4015' >abs</samp> must be present in all implementations, whereas <samp id='samp4016' >magnitude</samp> need only be present in implementations that support general complex numbers. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4020" class="mark"></a><a name="exact->inexact" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4026" align="left" colspan="1"><strong id='bold4022' >exact->inexact</strong><em id='it4025' > <code id='code4024' ><em id='it4023' >z</em></code></em></td><td id="tc4027" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g4031" class="mark"></a><a name="inexact->exact" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4037" align="left" colspan="1"><strong id='bold4033' >inexact->exact</strong><em id='it4036' > <code id='code4035' ><em id='it4034' >z</em></code></em></td><td id="tc4038" align="right" colspan="1">procedure</td></tr> </tbody></table> <samp id='samp4041' >Exact->inexact</samp> returns an inexact representation of <code id='code4043' ><em id='it4042' >z</em></code>. The value returned is the inexact number that is numerically closest to the argument. If an exact argument has no reasonably close inexact equivalent, then a violation of an implementation restriction may be reported.<br/><br/><samp id='samp4045' >Inexact->exact</samp> returns an exact representation of <code id='code4047' ><em id='it4046' >z</em></code>. The value returned is the exact number that is numerically closest to the argument. If an inexact argument has no reasonably close exact equivalent, then a violation of an implementation restriction may be reported.<br/><br/> These procedures implement the natural one-to-one correspondence between exact and inexact integers throughout an implementation-dependent range. See section <a href="r5rs-9.html#Implementation-restrictions" class="inbound">Implementation restrictions</a>. </td></tr> </tbody></table><br/> <br/><br/><br/> </div> <!-- Numerical input and output --> <a name="Numerical-input-and-output"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.2.6 Numerical input and output</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Numerical-input-and-output" class="mark"></a> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4053" class="mark"></a><a name="number->string" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4057" align="left" colspan="1"><strong id='bold4055' >number->string</strong><em id='it4056' > z</em></td><td id="tc4058" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4063" align="left" colspan="1"><strong id='bold4061' >number->string</strong><em id='it4062' > z radix</em></td><td id="tc4064" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code4068' ><em id='it4067' >Radix</em></code> must be an exact integer, either 2, 8, 10, or 16. If omitted, <code id='code4070' ><em id='it4069' >radix</em></code> defaults to 10. The procedure <samp id='samp4071' >number->string</samp> takes a number and a radix and returns as a string an external representation of the given number in the given radix such that<br/><br/><center id='center4080' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4078' >(<strong id='bold7958' >let</strong> ((number <code id='code4074' ><em id='it4073' >number</em></code>) (radix <code id='code4076' ><em id='it4075' >radix</em></code>)) (eqv? number (string->number (number->string number radix) radix))) </pre> </td></tr> </tbody></table></center> is true. It is an error if no possible result makes this expression true.<br/><br/>If <code id='code4083' ><em id='it4082' >z</em></code> is inexact, the radix is 10, and the above expression can be satisfied by a result that contains a decimal point, then the result contains a decimal point and is expressed using the minimum number of digits (exclusive of exponent and trailing zeroes) needed to make the above expression true [howtoprint], [howtoread]; otherwise the format of the result is unspecified.<br/><br/>The result returned by <samp id='samp4085' >number->string</samp> never contains an explicit radix prefix.<br/><br/><font size="-1"><em id='emph4087' >Note:</em> The error case can occur only when <code id='code4089' ><em id='it4088' >z</em></code> is not a complex number or is a complex number with a non-rational real or imaginary part. </font> <br/><em id='emph4092' >Rationale:</em> If <code id='code4094' ><em id='it4093' >z</em></code> is an inexact number represented using flonums, and the radix is 10, then the above expression is normally satisfied by a result containing a decimal point. The unspecified case allows for infinities, NaNs, and non-flonum representations. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4098" class="mark"></a><a name="string->number" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4102" align="left" colspan="1"><strong id='bold4100' >string->number</strong><em id='it4101' > string</em></td><td id="tc4103" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4108" align="left" colspan="1"><strong id='bold4106' >string->number</strong><em id='it4107' > string radix</em></td><td id="tc4109" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns a number of the maximally precise representation expressed by the given <code id='code4113' ><em id='it4112' >string</em></code>. <code id='code4115' ><em id='it4114' >Radix</em></code> must be an exact integer, either 2, 8, 10, or 16. If supplied, <code id='code4117' ><em id='it4116' >radix</em></code> is a default radix that may be overridden by an explicit radix prefix in <code id='code4119' ><em id='it4118' >string</em></code> (e.g. <tt id='tt4120' >"#o177"</tt>). If <code id='code4122' ><em id='it4121' >radix</em></code> is not supplied, then the default radix is 10. If <code id='code4124' ><em id='it4123' >string</em></code> is not a syntactically valid notation for a number, then <samp id='samp4125' >string->number</samp> returns <tt id='tt4126' >#f</tt>.<br/><br/><center id='center4134' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4132' >(string->number <font color="red">"100"</font>) => 100 (string->number <font color="red">"100"</font> 16) => 256 (string->number <font color="red">"1e2"</font>) => 100.0 (string->number <font color="red">"15##"</font>) => 1500.0 </pre> </td></tr> </tbody></table></center> <font size="-1"><em id='emph4135' >Note:</em> The domain of <samp id='samp4136' >string->number</samp> may be restricted by implementations in the following ways. <samp id='samp4137' >String->number</samp> is permitted to return <tt id='tt4138' >#f</tt> whenever <code id='code4140' ><em id='it4139' >string</em></code> contains an explicit radix prefix. If all numbers supported by an implementation are real, then <samp id='samp4141' >string->number</samp> is permitted to return <tt id='tt4142' >#f</tt> whenever <code id='code4144' ><em id='it4143' >string</em></code> uses the polar or rectangular notations for complex numbers. If all numbers are integers, then <samp id='samp4145' >string->number</samp> may return <tt id='tt4146' >#f</tt> whenever the fractional notation is used. If all numbers are exact, then <samp id='samp4147' >string->number</samp> may return <tt id='tt4148' >#f</tt> whenever an exponent marker or explicit exactness prefix is used, or if a <tt id='tt4149' >#</tt> appears in place of a digit. If all inexact numbers are integers, then <samp id='samp4150' >string->number</samp> may return <tt id='tt4151' >#f</tt> whenever a decimal point is used. </font></td></tr> </tbody></table><br/> </div> </div><br> <!-- Other data types --> <a name="Other-data-types"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">6.3 Other data types</font> </h3></td></tr></table> </div><div class="section"> <a name="Other-data-types" class="mark"></a> This section describes operations on some of Scheme's non-numeric data types: booleans, pairs, lists, symbols, characters, strings and vectors.<br/><br/><!-- Booleans --> <a name="Booleans"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.3.1 Booleans</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Booleans" class="mark"></a> The standard boolean objects for true and false are written as <tt id='tt4156' >#t</tt> and <tt id='tt4157' >#f</tt>. What really <a name="g4158" class="mark"></a><a name="g4160" class="mark"></a>matters, though, are the objects that the Scheme conditional expressions (<samp id='samp4162' >if</samp>, <samp id='samp4163' >cond</samp>, <samp id='samp4164' >and</samp>, <samp id='samp4165' >or</samp>, <samp id='samp4166' >do</samp>) treat as true or false. The phrase ``a true value'' <a name="g4168" class="mark"></a><a name="g4171" class="mark"></a>(or sometimes just ``true'') means any object treated as true by the conditional expressions, and the phrase ``a false value'' (or <a name="g4174" class="mark"></a>``false'') means any object treated as false by the conditional expressions.<br/><br/>Of all the standard Scheme values, only <tt id='tt4177' >#f</tt> counts as false in conditional expressions. Except for <tt id='tt4178' >#f</tt>, all standard Scheme values, including <tt id='tt4179' >#t</tt>, pairs, the empty list, symbols, numbers, strings, vectors, and procedures, count as true.<br/><br/> <font size="-1"><em id='emph4181' >Note:</em> Programmers accustomed to other dialects of Lisp should be aware that Scheme distinguishes both <tt id='tt4182' >#f</tt> and the empty list <a name="g4184" class="mark"></a>from the symbol <code id='code4186' >nil</code>. <a name="g4188" class="mark"></a></font> Boolean constants evaluate to themselves, so they do not need to be quoted in programs.<br/><br/><center id='center4194' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4192' >#t => #t #f => #f '#f => #f </pre> </td></tr> </tbody></table></center> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4196" class="mark"></a><a name="not" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4200" align="left" colspan="1"><strong id='bold4198' >not</strong><em id='it4199' > obj</em></td><td id="tc4201" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp4204' >Not</samp> returns <tt id='tt4205' >#t</tt> if <code id='code4207' ><em id='it4206' >obj</em></code> is false, and returns <tt id='tt4208' >#f</tt> otherwise.<br/><br/><center id='center4212' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4210' >(not #t) => #f (not 3) => #f (not (list 3)) => #f (not #f) => #t (not '()) => #f (not (list)) => #f (not 'nil) => #f </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4216" class="mark"></a><a name="boolean?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4220" align="left" colspan="1"><strong id='bold4218' >boolean?</strong><em id='it4219' > obj</em></td><td id="tc4221" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp4224' >Boolean?</samp> returns <tt id='tt4225' >#t</tt> if <code id='code4227' ><em id='it4226' >obj</em></code> is either <tt id='tt4228' >#t</tt> or <tt id='tt4229' >#f</tt> and returns <tt id='tt4230' >#f</tt> otherwise.<br/><br/><center id='center4234' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4232' >(boolean? #f) => #t (boolean? 0) => #f (boolean? '()) => #f </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> </div> <!-- Pairs and lists --> <a name="Pairs-and-lists"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.3.2 Pairs and lists</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Pairs-and-lists" class="mark"></a> A "pair" (sometimes called a "dotted pair") is a <a name="g4238" class="mark"></a><a name="g4241" class="mark"></a>record structure with two fields called the car and cdr fields (for historical reasons). Pairs are created by the procedure <samp id='samp4243' >cons</samp>. The car and cdr fields are accessed by the procedures <samp id='samp4244' >car</samp> and <samp id='samp4245' >cdr</samp>. The car and cdr fields are assigned by the procedures <samp id='samp4246' >set-car!</samp> and <samp id='samp4247' >set-cdr!</samp>.<br/><br/>Pairs are used primarily to represent lists. A list can be defined recursively as either the empty list or a pair whose <a name="g4250" class="mark"></a>cdr is a list. More precisely, the set of lists is defined as the smallest set <code id='code4253' ><em id='it4252' >X</em></code> such that<br/><br/><ul class="itemize" id='itemize4268' ><li>The empty list is in <code id='code4256' ><em id='it4255' >X</em></code>. </li> <li>If <code id='code4259' ><em id='it4258' >list</em></code> is in <code id='code4261' ><em id='it4260' >X</em></code>, then any pair whose cdr field contains <code id='code4263' ><em id='it4262' >list</em></code> is also in <code id='code4265' ><em id='it4264' >X</em></code>.<br/><br/></li> </ul> The objects in the car fields of successive pairs of a list are the elements of the list. For example, a two-element list is a pair whose car is the first element and whose cdr is a pair whose car is the second element and whose cdr is the empty list. The length of a list is the number of elements, which is the same as the number of pairs.<br/><br/>The empty list is a special object of its own type <a name="g4271" class="mark"></a>(it is not a pair); it has no elements and its length is zero.<br/><br/><font size="-1"><em id='emph4274' >Note:</em> The above definitions imply that all lists have finite length and are terminated by the empty list. </font> The most general notation (external representation) for Scheme pairs is the ``dotted'' notation <strong id='bold4281' ><samp id='samp4280' >(<code id='code4277' ><em id='it4276' >c1</em></code> .: <code id='code4279' ><em id='it4278' >c2</em></code>)</samp></strong> where <code id='code4283' ><em id='it4282' >c1</em></code> is the value of the car field and <code id='code4285' ><em id='it4284' >c2</em></code> is the value of the cdr field. For example <samp id='samp4286' >(4 .: 5)</samp> is a pair whose car is 4 and whose cdr is 5. Note that <samp id='samp4287' >(4 .: 5)</samp> is the external representation of a pair, not an expression that evaluates to a pair.<br/><br/>A more streamlined notation can be used for lists: the elements of the list are simply enclosed in parentheses and separated by spaces. The empty list is written <tt id='tt4289' >()</tt> . For example, <a name="g4291" class="mark"></a> <center id='center4295' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4293' >(a b c d e) </pre> </td></tr> </tbody></table></center> and<br/><br/><center id='center4299' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4297' >(a . (b . (c . (d . (e . ()))))) </pre> </td></tr> </tbody></table></center> are equivalent notations for a list of symbols.<br/><br/>A chain of pairs not ending in the empty list is called an "improper list". Note that an improper list is not a list. <a name="g4302" class="mark"></a>The list and dotted notations can be combined to represent improper lists:<br/><br/><center id='center4307' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4305' >(a b c . d) </pre> </td></tr> </tbody></table></center> is equivalent to<br/><br/><center id='center4311' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4309' >(a . (b . (c . d))) </pre> </td></tr> </tbody></table></center> Whether a given pair is a list depends upon what is stored in the cdr field. When the <code id='code4312' >set-cdr!</code> procedure is used, an object can be a <a name="g4314" class="mark"></a>list one moment and not the next:<br/><br/><center id='center4323' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4321' >(<font color="#6959cf"><strong id='bold7963' >define</strong></font> <font color="#6959cf"><strong id='bold7965' >x</strong></font> (list 'a 'b 'c)) (<font color="#6959cf"><strong id='bold7967' >define</strong></font> <font color="#6959cf"><strong id='bold7969' >y</strong></font> x) y => (a b c) (list? y) => #t (set-cdr! x 4) => <em id='emph4317' >unspecified</em> x => (a . 4) (eqv? x y) => #t y => (a . 4) (list? y) => #f (set-cdr! x x) => <em id='emph4318' >unspecified</em> (list? x) => #f </pre> </td></tr> </tbody></table></center> Within literal expressions and representations of objects read by the <code id='code4324' >read</code> procedure, the forms <tt id='tt4325' >'</tt><datum>, <a name="g4326" class="mark"></a><a name="g4329" class="mark"></a><tt id='tt4331' >`</tt><datum>, <tt id='tt4332' >,</tt><datum>, and <a name="g4333" class="mark"></a><tt id='tt4335' >,@</tt><datum> denote two-element lists whose first elements are the symbols <code id='code4336' >quote</code>, <code id='code4337' >quasiquote</code>, <strong id='bold4339' ><code id='code4338' >unquote</code></strong>, and <a name="g4341" class="mark"></a><a name="g4344" class="mark"></a><a name="g4347" class="mark"></a><code id='code4349' >unquote-splicing</code>, respectively. The second element in each case <a name="g4351" class="mark"></a>is <datum>. This convention is supported so that arbitrary Scheme programs may be represented as lists. That is, according to Scheme's grammar, every <expression> is also a <datum> (see section <a href="r5rs-10.html#External-representation" class="inbound">External representation</a>). Among other things, this permits the use of the <samp id='samp4353' >read</samp> procedure to parse Scheme programs. See section <a href="r5rs-6.html#External-representations" class="inbound">External representations</a>. <br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4356" class="mark"></a><a name="pair?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4360" align="left" colspan="1"><strong id='bold4358' >pair?</strong><em id='it4359' > obj</em></td><td id="tc4361" align="right" colspan="1">procedure</td></tr> </tbody></table> <samp id='samp4364' >Pair?</samp> returns <tt id='tt4365' >#t</tt> if <code id='code4367' ><em id='it4366' >obj</em></code> is a pair, and otherwise returns <tt id='tt4368' >#f</tt>.<br/><br/><center id='center4372' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4370' >(pair? '(a . b)) => #t (pair? '(a b c)) => #t (pair? '()) => #f (pair? '#(a b)) => #f </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4376" class="mark"></a><a name="cons" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4380" align="left" colspan="1"><strong id='bold4378' >cons</strong><em id='it4379' > obj1 obj2</em></td><td id="tc4381" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns a newly allocated pair whose car is <code id='code4385' ><em id='it4384' >obj1</em></code> and whose cdr is <code id='code4387' ><em id='it4386' >obj2</em></code>. The pair is guaranteed to be different (in the sense of <samp id='samp4388' >eqv?</samp>) from every existing object.<br/><br/><center id='center4394' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4392' >(cons 'a '()) => (a) (cons '(a) '(b c d)) => ((a) b c d) (cons <font color="red">"a"</font> '(b c)) => (<font color="red">"a"</font> b c) (cons 'a 3) => (a . 3) (cons '(a b) 'c) => ((a b) . c) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4398" class="mark"></a><a name="car" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4402" align="left" colspan="1"><strong id='bold4400' >car</strong><em id='it4401' > pair</em></td><td id="tc4403" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the contents of the car field of <code id='code4407' ><em id='it4406' >pair</em></code>. Note that it is an error to take the car of the empty list. <a name="g4409" class="mark"></a> <center id='center4414' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4412' >(car '(a b c)) => a (car '((a) b c d)) => (a) (car '(1 . 2)) => 1 (car '()) => <em id='emph4411' >error</em> </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4418" class="mark"></a><a name="cdr" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4422" align="left" colspan="1"><strong id='bold4420' >cdr</strong><em id='it4421' > pair</em></td><td id="tc4423" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the contents of the cdr field of <code id='code4427' ><em id='it4426' >pair</em></code>. Note that it is an error to take the cdr of the empty list.<br/><br/><center id='center4432' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4430' >(cdr '((a) b c d)) => (b c d) (cdr '(1 . 2)) => 2 (cdr '()) => <em id='emph4429' >error</em> </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4436" class="mark"></a><a name="set-car!" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4440" align="left" colspan="1"><strong id='bold4438' >set-car!</strong><em id='it4439' > pair obj</em></td><td id="tc4441" align="right" colspan="1">procedure</td></tr> </tbody></table> Stores <code id='code4445' ><em id='it4444' >obj</em></code> in the car field of <code id='code4447' ><em id='it4446' >pair</em></code>. The value returned by <samp id='samp4448' >set-car!</samp> is unspecified. <center id='center4455' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4453' >(<font color="#6959cf"><strong id='bold7973' >define</strong></font> (<font color="#6959cf"><strong id='bold7975' >f</strong></font>) (list 'not-a-constant-list)) (<font color="#6959cf"><strong id='bold7977' >define</strong></font> (<font color="#6959cf"><strong id='bold7979' >g</strong></font>) '(constant-list)) (set-car! (f) 3) => <em id='emph4449' >unspecified</em> (set-car! (g) 3) => <em id='emph4450' >error</em> </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4459" class="mark"></a><a name="set-cdr!" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4463" align="left" colspan="1"><strong id='bold4461' >set-cdr!</strong><em id='it4462' > pair obj</em></td><td id="tc4464" align="right" colspan="1">procedure</td></tr> </tbody></table> Stores <code id='code4468' ><em id='it4467' >obj</em></code> in the cdr field of <code id='code4470' ><em id='it4469' >pair</em></code>. The value returned by <samp id='samp4471' >set-cdr!</samp> is unspecified. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4475" class="mark"></a><a name="caar" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4479" align="left" colspan="1"><strong id='bold4477' >caar</strong><em id='it4478' > pair</em></td><td id="tc4480" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g4484" class="mark"></a><a name="cadr" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4488" align="left" colspan="1"><strong id='bold4486' >cadr</strong><em id='it4487' > pair</em></td><td id="tc4489" align="right" colspan="1">library procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4494" align="left" colspan="1"><em id='it4493' ><strong id='bold4492' > ...</strong></em></td><td id="tc4496" align="right" colspan="1"> <strong id='bold4495' > ...</strong></td></tr> </tbody></table> <a name="g4500" class="mark"></a><a name="cdddar" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4504" align="left" colspan="1"><strong id='bold4502' >cdddar</strong><em id='it4503' > pair</em></td><td id="tc4505" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g4509" class="mark"></a><a name="cddddr" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4513" align="left" colspan="1"><strong id='bold4511' >cddddr</strong><em id='it4512' > pair</em></td><td id="tc4514" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures are compositions of <samp id='samp4517' >car</samp> and <samp id='samp4518' >cdr</samp>, where for example <samp id='samp4519' >caddr</samp> could be defined by<br/><br/><center id='center4525' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4523' >(<font color="#6959cf"><strong id='bold7981' >define</strong></font> <font color="#6959cf"><strong id='bold7983' >caddr</strong></font> (<strong id='bold7985' >lambda</strong> (x) (car (cdr (cdr x))))). </pre> </td></tr> </tbody></table></center> Arbitrary compositions, up to four deep, are provided. There are twenty-eight of these procedures in all. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4529" class="mark"></a><a name="null?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4533" align="left" colspan="1"><strong id='bold4531' >null?</strong><em id='it4532' > obj</em></td><td id="tc4534" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns <tt id='tt4537' >#t</tt> if <code id='code4539' ><em id='it4538' >obj</em></code> is the empty list, <a name="g4541" class="mark"></a>otherwise returns <tt id='tt4543' >#f</tt>.<br/><br/> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4548" class="mark"></a><a name="list?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4552" align="left" colspan="1"><strong id='bold4550' >list?</strong><em id='it4551' > obj</em></td><td id="tc4553" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns <tt id='tt4556' >#t</tt> if <code id='code4558' ><em id='it4557' >obj</em></code> is a list, otherwise returns <tt id='tt4559' >#f</tt>. By definition, all lists have finite length and are terminated by the empty list.<br/><br/><center id='center4564' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4562' > (list? '(a b c)) => #t (list? '()) => #t (list? '(a . b)) => #f (<strong id='bold7986' >let</strong> ((x (list 'a))) (set-cdr! x x) (list? x)) => #f </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4568" class="mark"></a><a name="list" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4574" align="left" colspan="1"><strong id='bold4570' >list</strong><em id='it4573' > <code id='code4572' ><em id='it4571' >obj</em></code> ...,</em></td><td id="tc4575" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns a newly allocated list of its arguments.<br/><br/><center id='center4581' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4579' >(list 'a (+ 3 4) 'c) => (a 7 c) (list) => () </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4585" class="mark"></a><a name="length" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4589" align="left" colspan="1"><strong id='bold4587' >length</strong><em id='it4588' > list</em></td><td id="tc4590" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns the length of <code id='code4594' ><em id='it4593' >list</em></code>.<br/><br/><center id='center4598' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4596' >(length '(a b c)) => 3 (length '(a (b) (c d e))) => 3 (length '()) => 0 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4602" class="mark"></a><a name="append" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4606" align="left" colspan="1"><strong id='bold4604' >append</strong><em id='it4605' > list ...,</em></td><td id="tc4607" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns a list consisting of the elements of the first <code id='code4611' ><em id='it4610' >list</em></code> followed by the elements of the other <code id='code4613' ><em id='it4612' >list</em></code>s.<br/><br/><center id='center4617' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4615' >(append '(x) '(y)) => (x y) (append '(a) '(b c d)) => (a b c d) (append '(a (b)) '((c))) => (a (b) (c)) </pre> </td></tr> </tbody></table></center> The resulting list is always newly allocated, except that it shares structure with the last <code id='code4619' ><em id='it4618' >list</em></code> argument. The last argument may actually be any object; an improper list results if the last argument is not a proper list. <center id='center4622' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4620' >(append '(a b) '(c . d)) => (a b c . d) (append '() 'a) => a </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4626" class="mark"></a><a name="reverse" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4630" align="left" colspan="1"><strong id='bold4628' >reverse</strong><em id='it4629' > list</em></td><td id="tc4631" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns a newly allocated list consisting of the elements of <code id='code4635' ><em id='it4634' >list</em></code> in reverse order.<br/><br/><center id='center4639' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4637' >(reverse '(a b c)) => (c b a) (reverse '(a (b c) d (e (f)))) => ((e (f)) d (b c) a) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4643" class="mark"></a><a name="list-tail" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4649" align="left" colspan="1"><strong id='bold4645' >list-tail</strong><em id='it4648' > list <code id='code4647' ><em id='it4646' >k</em></code></em></td><td id="tc4650" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns the sublist of <code id='code4654' ><em id='it4653' >list</em></code> obtained by omitting the first <code id='code4656' ><em id='it4655' >k</em></code> elements. It is an error if <code id='code4658' ><em id='it4657' >list</em></code> has fewer than <code id='code4660' ><em id='it4659' >k</em></code> elements. <samp id='samp4661' >List-tail</samp> could be defined by<br/><br/><center id='center4668' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4666' >(<font color="#6959cf"><strong id='bold7987' >define</strong></font> <font color="#6959cf"><strong id='bold7989' >list-tail</strong></font> (<strong id='bold7991' >lambda</strong> (x k) (<strong id='bold7992' >if</strong> (zero? k) x (list-tail (cdr x) (- k 1))))) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4672" class="mark"></a><a name="list-ref" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4678" align="left" colspan="1"><strong id='bold4674' >list-ref</strong><em id='it4677' > list <code id='code4676' ><em id='it4675' >k</em></code></em></td><td id="tc4679" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns the <code id='code4683' ><em id='it4682' >k</em></code>th element of <code id='code4685' ><em id='it4684' >list</em></code>. (This is the same as the car of <tt id='tt4690' >(list-tail <code id='code4687' ><em id='it4686' >list</em></code> <code id='code4689' ><em id='it4688' >k</em></code>)</tt>.) It is an error if <code id='code4692' ><em id='it4691' >list</em></code> has fewer than <code id='code4694' ><em id='it4693' >k</em></code> elements.<br/><br/><center id='center4698' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4696' >(list-ref '(a b c d) 2) => c (list-ref '(a b c d) (inexact->exact (round 1.8))) => c </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4702" class="mark"></a><a name="memq" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4706" align="left" colspan="1"><strong id='bold4704' >memq</strong><em id='it4705' > obj list</em></td><td id="tc4707" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g4711" class="mark"></a><a name="memv" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4715" align="left" colspan="1"><strong id='bold4713' >memv</strong><em id='it4714' > obj list</em></td><td id="tc4716" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g4720" class="mark"></a><a name="member" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4724" align="left" colspan="1"><strong id='bold4722' >member</strong><em id='it4723' > obj list</em></td><td id="tc4725" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures return the first sublist of <code id='code4729' ><em id='it4728' >list</em></code> whose car is <code id='code4731' ><em id='it4730' >obj</em></code>, where the sublists of <code id='code4733' ><em id='it4732' >list</em></code> are the non-empty lists returned by <tt id='tt4738' >(list-tail <code id='code4735' ><em id='it4734' >list</em></code> <code id='code4737' ><em id='it4736' >k</em></code>)</tt> for <code id='code4740' ><em id='it4739' >k</em></code> less than the length of <code id='code4742' ><em id='it4741' >list</em></code>. If <code id='code4744' ><em id='it4743' >obj</em></code> does not occur in <code id='code4746' ><em id='it4745' >list</em></code>, then <tt id='tt4747' >#f</tt> (not the empty list) is returned. <samp id='samp4748' >Memq</samp> uses <samp id='samp4749' >eq?</samp> to compare <code id='code4751' ><em id='it4750' >obj</em></code> with the elements of <code id='code4753' ><em id='it4752' >list</em></code>, while <samp id='samp4754' >memv</samp> uses <samp id='samp4755' >eqv?</samp> and <samp id='samp4756' >member</samp> uses <samp id='samp4757' >equal?</samp>.<br/><br/><center id='center4762' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4760' >(memq 'a '(a b c)) => (a b c) (memq 'b '(a b c)) => (b c) (memq 'a '(b c d)) => #f (memq (list 'a) '(b (a) c)) => #f (member (list 'a) '(b (a) c)) => ((a) c) (memq 101 '(100 101 102)) => <em id='emph4759' >unspecified</em> (memv 101 '(100 101 102)) => (101 102) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4766" class="mark"></a><a name="assq" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4770" align="left" colspan="1"><strong id='bold4768' >assq</strong><em id='it4769' > obj alist</em></td><td id="tc4771" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g4775" class="mark"></a><a name="assv" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4779" align="left" colspan="1"><strong id='bold4777' >assv</strong><em id='it4778' > obj alist</em></td><td id="tc4780" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g4784" class="mark"></a><a name="assoc" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4788" align="left" colspan="1"><strong id='bold4786' >assoc</strong><em id='it4787' > obj alist</em></td><td id="tc4789" align="right" colspan="1">library procedure</td></tr> </tbody></table> <code id='code4793' ><em id='it4792' >Alist</em></code> (for ``association list'') must be a list of pairs. These procedures find the first pair in <code id='code4795' ><em id='it4794' >alist</em></code> whose car field is <code id='code4797' ><em id='it4796' >obj</em></code>, and returns that pair. If no pair in <code id='code4799' ><em id='it4798' >alist</em></code> has <code id='code4801' ><em id='it4800' >obj</em></code> as its car, then <tt id='tt4802' >#f</tt> (not the empty list) is returned. <samp id='samp4803' >Assq</samp> uses <samp id='samp4804' >eq?</samp> to compare <code id='code4806' ><em id='it4805' >obj</em></code> with the car fields of the pairs in <code id='code4808' ><em id='it4807' >alist</em></code>, while <samp id='samp4809' >assv</samp> uses <samp id='samp4810' >eqv?</samp> and <samp id='samp4811' >assoc</samp> uses <samp id='samp4812' >equal?</samp>.<br/><br/><center id='center4818' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4816' >(<font color="#6959cf"><strong id='bold7993' >define</strong></font> <font color="#6959cf"><strong id='bold7995' >e</strong></font> '((a 1) (b 2) (c 3))) (assq 'a e) => (a 1) (assq 'b e) => (b 2) (assq 'd e) => #f (assq (list 'a) '(((a)) ((b)) ((c)))) => #f (assoc (list 'a) '(((a)) ((b)) ((c)))) => ((a)) (assq 5 '((2 3) (5 7) (11 13))) => <em id='emph4814' >unspecified</em> (assv 5 '((2 3) (5 7) (11 13))) => (5 7) </pre> </td></tr> </tbody></table></center> <br/><em id='emph4820' >Rationale:</em> Although they are ordinarily used as predicates, <samp id='samp4821' >memq</samp>, <samp id='samp4822' >memv</samp>, <samp id='samp4823' >member</samp>, <samp id='samp4824' >assq</samp>, <samp id='samp4825' >assv</samp>, and <samp id='samp4826' >assoc</samp> do not have question marks in their names because they return useful values rather than just <tt id='tt4827' >#t</tt> or <tt id='tt4828' >#f</tt>.<br/><br/></td></tr> </tbody></table><br/> </div> <!-- Symbols --> <a name="Symbols"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.3.3 Symbols</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Symbols" class="mark"></a>Symbols are objects whose usefulness rests on the fact that two symbols are identical (in the sense of <samp id='samp4832' >eqv?</samp>) if and only if their names are spelled the same way. This is exactly the property needed to represent identifiers in programs, and so most <a name="g4834" class="mark"></a>implementations of Scheme use them internally for that purpose. Symbols are useful for many other applications; for instance, they may be used the way enumerated values are used in Pascal.<br/><br/>The rules for writing a symbol are exactly the same as the rules for writing an identifier; see sections <a href="r5rs-5.html#Identifiers" class="inbound">Identifiers</a> and <a href="r5rs-10.html#Lexical-structure" class="inbound">Lexical structure</a>.<br/><br/>It is guaranteed that any symbol that has been returned as part of a literal expression, or read using the <samp id='samp4838' >read</samp> procedure, and subsequently written out using the <samp id='samp4839' >write</samp> procedure, will read back in as the identical symbol (in the sense of <samp id='samp4840' >eqv?</samp>). The <samp id='samp4841' >string->symbol</samp> procedure, however, can create symbols for which this write/read invariance may not hold because their names contain special characters or letters in the non-standard case.<br/><br/><font size="-1"><em id='emph4843' >Note:</em> Some implementations of Scheme have a feature known as ``slashification'' in order to guarantee write/read invariance for all symbols, but historically the most important use of this feature has been to compensate for the lack of a string data type.<br/><br/>Some implementations also have ``uninterned symbols'', which defeat write/read invariance even in implementations with slashification, and also generate exceptions to the rule that two symbols are the same if and only if their names are spelled the same. </font> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4847" class="mark"></a><a name="symbol?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4851" align="left" colspan="1"><strong id='bold4849' >symbol?</strong><em id='it4850' > obj</em></td><td id="tc4852" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt4855' >#t</tt> if <code id='code4857' ><em id='it4856' >obj</em></code> is a symbol, otherwise returns <tt id='tt4858' >#f</tt>.<br/><br/><center id='center4863' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4861' >(symbol? 'foo) => #t (symbol? (car '(a b))) => #t (symbol? <font color="red">"bar"</font>) => #f (symbol? 'nil) => #t (symbol? '()) => #f (symbol? #f) => #f </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4867" class="mark"></a><a name="symbol->string" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4871" align="left" colspan="1"><strong id='bold4869' >symbol->string</strong><em id='it4870' > symbol</em></td><td id="tc4872" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the name of <code id='code4876' ><em id='it4875' >symbol</em></code> as a string. If the symbol was part of an object returned as the value of a literal expression (section <a href="r5rs-7.html#Literal-expressions" class="inbound">Literal expressions</a>) or by a call to the <samp id='samp4877' >read</samp> procedure, and its name contains alphabetic characters, then the string returned will contain characters in the implementation's preferred standard case---some implementations will prefer upper case, others lower case. If the symbol was returned by <samp id='samp4878' >string->symbol</samp>, the case of characters in the string returned will be the same as the case in the string that was passed to <samp id='samp4879' >string->symbol</samp>. It is an error to apply mutation procedures like <code id='code4880' >string-set!</code> to strings returned <a name="g4882" class="mark"></a>by this procedure.<br/><br/>The following examples assume that the implementation's standard case is lower case:<br/><br/><center id='center4892' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4890' >(symbol->string 'flying-fish) => <font color="red">"flying-fish"</font> (symbol->string 'Martin) => <font color="red">"martin"</font> (symbol->string (string->symbol <font color="red">"Malvina"</font>)) => <font color="red">"Malvina"</font> </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4896" class="mark"></a><a name="string->symbol" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4900" align="left" colspan="1"><strong id='bold4898' >string->symbol</strong><em id='it4899' > string</em></td><td id="tc4901" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the symbol whose name is <code id='code4905' ><em id='it4904' >string</em></code>. This procedure can create symbols with names containing special characters or letters in the non-standard case, but it is usually a bad idea to create such symbols because in some implementations of Scheme they cannot be read as themselves. See <samp id='samp4906' >symbol->string</samp>.<br/><br/>The following examples assume that the implementation's standard case is lower case:<br/><br/><center id='center4916' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog4914' >(eq? 'mISSISSIppi 'mississippi) => #t (string->symbol <font color="red">"mISSISSIppi"</font>) => the symbol with name <font color="red">"mISSISSIppi"</font> (eq? 'bitBlt (string->symbol <font color="red">"bitBlt"</font>)) => #f (eq? 'JollyWog (string->symbol (symbol->string 'JollyWog))) => #t (string=? <font color="red">"K. Harper, M.D."</font> (symbol->string (string->symbol <font color="red">"K. Harper, M.D."</font>))) => #t </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> </div> <!-- Characters --> <a name="Characters"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.3.4 Characters</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Characters" class="mark"></a> Characters are objects that represent printed characters such as letters and digits. Characters are written using the notation #\<character> or #\<character name>. For example:<br/><br/><font size="-1"><dl class="description" id='description4932' > <dt><tt id='tt4920' >#\a</tt></dt> <dt> </dt><dd>; lower case letter </dd> <dt><tt id='tt4922' >#\A</tt></dt> <dt> </dt><dd>; upper case letter </dd> <dt><tt id='tt4924' >#\(</tt></dt> <dt> </dt><dd>; left parenthesis </dd> <dt><tt id='tt4926' >#\ </tt></dt> <dt> </dt><dd>; the space character </dd> <dt><tt id='tt4928' >#\space</tt></dt> <dt> </dt><dd>; the preferred way to write a space </dd> <dt><tt id='tt4930' >#\newline</tt></dt> <dt> </dt><dd>; the newline character </dd> </dl></font> Case is significant in #\<character>, but not in #\<character name>. If <character> in #\<character> is alphabetic, then the character following <character> must be a delimiter character such as a space or parenthesis. This rule resolves the ambiguous case where, for example, the sequence of characters ``<tt id='tt4934' >#\ space</tt>'' could be taken to be either a representation of the space character or a representation of the character ``<tt id='tt4935' >#\ s</tt>'' followed by a representation of the symbol ``<tt id='tt4936' >pace</tt>.''<br/><br/> Characters written in the #\ notation are self-evaluating. That is, they do not have to be quoted in programs. Some of the procedures that operate on characters ignore the difference between upper case and lower case. The procedures that ignore case have <strong id='bold4939' >``<tt id='tt4938' >-ci</tt>''</strong> (for ``case insensitive'') embedded in their names.<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4942" class="mark"></a><a name="char?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4946" align="left" colspan="1"><strong id='bold4944' >char?</strong><em id='it4945' > obj</em></td><td id="tc4947" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt4950' >#t</tt> if <code id='code4952' ><em id='it4951' >obj</em></code> is a character, otherwise returns <tt id='tt4953' >#f</tt>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g4957" class="mark"></a><a name="char=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4961" align="left" colspan="1"><strong id='bold4959' >char=?</strong><em id='it4960' > char1 char2</em></td><td id="tc4962" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g4966" class="mark"></a><a name="char<?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4970" align="left" colspan="1"><strong id='bold4968' >char<?</strong><em id='it4969' > char1 char2</em></td><td id="tc4971" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g4975" class="mark"></a><a name="char>?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4979" align="left" colspan="1"><strong id='bold4977' >char>?</strong><em id='it4978' > char1 char2</em></td><td id="tc4980" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g4984" class="mark"></a><a name="char<=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4988" align="left" colspan="1"><strong id='bold4986' >char<=?</strong><em id='it4987' > char1 char2</em></td><td id="tc4989" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g4993" class="mark"></a><a name="char>=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc4997" align="left" colspan="1"><strong id='bold4995' >char>=?</strong><em id='it4996' > char1 char2</em></td><td id="tc4998" align="right" colspan="1">procedure</td></tr> </tbody></table> These procedures impose a total ordering on the set of characters. It is guaranteed that under this ordering:<br/><br/><ul class="itemize" id='itemize5014' ><li>The upper case characters are in order. For example, <samp id='samp5002' >(char<? #\A #\B)</samp> returns <tt id='tt5003' >#t</tt>. </li> <li>The lower case characters are in order. For example, <samp id='samp5005' >(char<? #\a #\b)</samp> returns <tt id='tt5006' >#t</tt>. </li> <li>The digits are in order. For example, <samp id='samp5008' >(char<? #\0 #\9)</samp> returns <tt id='tt5009' >#t</tt>. </li> <li>Either all the digits precede all the upper case letters, or vice versa. </li> <li>Either all the digits precede all the lower case letters, or vice versa.<br/><br/></li> </ul> Some implementations may generalize these procedures to take more than two arguments, as with the corresponding numerical predicates.<br/><br/></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5019" class="mark"></a><a name="char-ci=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5023" align="left" colspan="1"><strong id='bold5021' >char-ci=?</strong><em id='it5022' > char1 char2</em></td><td id="tc5024" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5028" class="mark"></a><a name="char-ci<?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5032" align="left" colspan="1"><strong id='bold5030' >char-ci<?</strong><em id='it5031' > char1 char2</em></td><td id="tc5033" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5037" class="mark"></a><a name="char-ci>?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5041" align="left" colspan="1"><strong id='bold5039' >char-ci>?</strong><em id='it5040' > char1 char2</em></td><td id="tc5042" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5046" class="mark"></a><a name="char-ci<=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5050" align="left" colspan="1"><strong id='bold5048' >char-ci<=?</strong><em id='it5049' > char1 char2</em></td><td id="tc5051" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5055" class="mark"></a><a name="char-ci>=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5059" align="left" colspan="1"><strong id='bold5057' >char-ci>=?</strong><em id='it5058' > char1 char2</em></td><td id="tc5060" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures are similar to <samp id='samp5063' >char=?</samp> et cetera, but they treat upper case and lower case letters as the same. For example, <samp id='samp5064' >(char-ci=? #\A #\a)</samp> returns <tt id='tt5065' >#t</tt>. Some implementations may generalize these procedures to take more than two arguments, as with the corresponding numerical predicates.<br/><br/></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5070" class="mark"></a><a name="char-alphabetic?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5074" align="left" colspan="1"><strong id='bold5072' >char-alphabetic?</strong><em id='it5073' > char</em></td><td id="tc5075" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5079" class="mark"></a><a name="char-numeric?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5083" align="left" colspan="1"><strong id='bold5081' >char-numeric?</strong><em id='it5082' > char</em></td><td id="tc5084" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5088" class="mark"></a><a name="char-whitespace?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5092" align="left" colspan="1"><strong id='bold5090' >char-whitespace?</strong><em id='it5091' > char</em></td><td id="tc5093" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5097" class="mark"></a><a name="char-upper-case?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5101" align="left" colspan="1"><strong id='bold5099' >char-upper-case?</strong><em id='it5100' > letter</em></td><td id="tc5102" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5106" class="mark"></a><a name="char-lower-case?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5110" align="left" colspan="1"><strong id='bold5108' >char-lower-case?</strong><em id='it5109' > letter</em></td><td id="tc5111" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures return <tt id='tt5114' >#t</tt> if their arguments are alphabetic, numeric, whitespace, upper case, or lower case characters, respectively, otherwise they return <tt id='tt5115' >#f</tt>. The following remarks, which are specific to the ASCII character set, are intended only as a guide: The alphabetic characters are the 52 upper and lower case letters. The numeric characters are the ten decimal digits. The whitespace characters are space, tab, line feed, form feed, and carriage return. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5119" class="mark"></a><a name="char->integer" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5123" align="left" colspan="1"><strong id='bold5121' >char->integer</strong><em id='it5122' > char</em></td><td id="tc5124" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g5128" class="mark"></a><a name="integer->char" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5134" align="left" colspan="1"><strong id='bold5130' >integer->char</strong><em id='it5133' > <code id='code5132' ><em id='it5131' >n</em></code></em></td><td id="tc5135" align="right" colspan="1">procedure</td></tr> </tbody></table> Given a character, <samp id='samp5138' >char->integer</samp> returns an exact integer representation of the character. Given an exact integer that is the image of a character under <samp id='samp5139' >char->integer</samp>, <samp id='samp5140' >integer->char</samp> returns that character. These procedures implement order-preserving isomorphisms between the set of characters under the <code id='code5141' >char<=?</code> ordering and some <a name="g5143" class="mark"></a>subset of the integers under the <samp id='samp5145' ><=</samp> ordering. That is, if<br/><br/><center id='center5157' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5155' >(char<=? <code id='code5148' ><em id='it5147' >a</em></code> <code id='code5150' ><em id='it5149' >b</em></code>) => #t and (<= <code id='code5152' ><em id='it5151' >x</em></code> <code id='code5154' ><em id='it5153' >y</em></code>) => #t </pre> </td></tr> </tbody></table></center> and <code id='code5159' ><em id='it5158' >x</em></code> and <code id='code5161' ><em id='it5160' >y</em></code> are in the domain of <samp id='samp5162' >integer->char</samp>, then<br/><br/><center id='center5175' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5173' >(<= (char->integer <code id='code5165' ><em id='it5164' >a</em></code>) (char->integer <code id='code5167' ><em id='it5166' >b</em></code>)) => #t<br/><br/>(char<=? (integer->char <code id='code5170' ><em id='it5169' >x</em></code>) (integer->char <code id='code5172' ><em id='it5171' >y</em></code>)) => #t </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5179" class="mark"></a><a name="char-upcase" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5183" align="left" colspan="1"><strong id='bold5181' >char-upcase</strong><em id='it5182' > char</em></td><td id="tc5184" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5188" class="mark"></a><a name="char-downcase" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5192" align="left" colspan="1"><strong id='bold5190' >char-downcase</strong><em id='it5191' > char</em></td><td id="tc5193" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures return a character <code id='code5197' ><em id='it5196' >char2</em></code> such that <samp id='samp5202' >(char-ci=? <code id='code5199' ><em id='it5198' >char</em></code> <code id='code5201' ><em id='it5200' >char2</em></code>)</samp>. In addition, if <code id='code5204' ><em id='it5203' >char</em></code> is alphabetic, then the result of <samp id='samp5205' >char-upcase</samp> is upper case and the result of <samp id='samp5206' >char-downcase</samp> is lower case.<br/><br/></td></tr> </tbody></table><br/> </div> <!-- Strings --> <a name="Strings"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.3.5 Strings</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Strings" class="mark"></a> Strings are sequences of characters. Strings are written as sequences of characters enclosed within doublequotes (<samp id='samp5210' >"</samp>). A doublequote can be written inside a string only by escaping it with a backslash (\), as in <center id='center5214' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5212' ><font color="red">"The word \"recursion\" has many meanings."</font> </pre> </td></tr> </tbody></table></center> A backslash can be written inside a string only by escaping it with another backslash. Scheme does not specify the effect of a backslash within a string that is not followed by a doublequote or backslash.<br/><br/>A string constant may continue from one line to the next, but the exact contents of such a string are unspecified. The <em id='emph5216' >length</em> of a string is the number of characters that it contains. This number is an exact, non-negative integer that is fixed when the string is created. The "valid indexes" of a string are the <a name="g5218" class="mark"></a>exact non-negative integers less than the length of the string. The first character of a string has index 0, the second has index 1, and so on.<br/><br/>In phrases such as ``the characters of <code id='code5222' ><em id='it5221' >string</em></code> beginning with index <code id='code5224' ><em id='it5223' >start</em></code> and ending with index <code id='code5226' ><em id='it5225' >end</em></code>,'' it is understood that the index <code id='code5228' ><em id='it5227' >start</em></code> is inclusive and the index <code id='code5230' ><em id='it5229' >end</em></code> is exclusive. Thus if <code id='code5232' ><em id='it5231' >start</em></code> and <code id='code5234' ><em id='it5233' >end</em></code> are the same index, a null substring is referred to, and if <code id='code5236' ><em id='it5235' >start</em></code> is zero and <code id='code5238' ><em id='it5237' >end</em></code> is the length of <code id='code5240' ><em id='it5239' >string</em></code>, then the entire string is referred to.<br/><br/>Some of the procedures that operate on strings ignore the difference between upper and lower case. The versions that ignore case have <strong id='bold5243' >``<samp id='samp5242' >-ci</samp>''</strong> (for ``case insensitive'') embedded in their names.<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5246" class="mark"></a><a name="string?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5250" align="left" colspan="1"><strong id='bold5248' >string?</strong><em id='it5249' > obj</em></td><td id="tc5251" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt5254' >#t</tt> if <code id='code5256' ><em id='it5255' >obj</em></code> is a string, otherwise returns <tt id='tt5257' >#f</tt>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5261" class="mark"></a><a name="make-string" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5267" align="left" colspan="1"><strong id='bold5263' >make-string</strong><em id='it5266' > <code id='code5265' ><em id='it5264' >k</em></code></em></td><td id="tc5268" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5275" align="left" colspan="1"><strong id='bold5271' >make-string</strong><em id='it5274' > <code id='code5273' ><em id='it5272' >k</em></code> char</em></td><td id="tc5276" align="right" colspan="1">procedure</td></tr> </tbody></table> <samp id='samp5279' >Make-string</samp> returns a newly allocated string of length <code id='code5281' ><em id='it5280' >k</em></code>. If <code id='code5283' ><em id='it5282' >char</em></code> is given, then all elements of the string are initialized to <code id='code5285' ><em id='it5284' >char</em></code>, otherwise the contents of the <code id='code5287' ><em id='it5286' >string</em></code> are unspecified. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5291" class="mark"></a><a name="string" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5295" align="left" colspan="1"><strong id='bold5293' >string</strong><em id='it5294' > char ...,</em></td><td id="tc5296" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns a newly allocated string composed of the arguments. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5302" class="mark"></a><a name="string-length" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5306" align="left" colspan="1"><strong id='bold5304' >string-length</strong><em id='it5305' > string</em></td><td id="tc5307" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the number of characters in the given <code id='code5311' ><em id='it5310' >string</em></code>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5315" class="mark"></a><a name="string-ref" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5321" align="left" colspan="1"><strong id='bold5317' >string-ref</strong><em id='it5320' > string <code id='code5319' ><em id='it5318' >k</em></code></em></td><td id="tc5322" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code5326' ><em id='it5325' >k</em></code> must be a valid index of <code id='code5328' ><em id='it5327' >string</em></code>. <samp id='samp5329' >String-ref</samp> returns character <code id='code5331' ><em id='it5330' >k</em></code> of <code id='code5333' ><em id='it5332' >string</em></code> using zero-origin indexing. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5337" class="mark"></a><a name="string-set!" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5341" align="left" colspan="1"><strong id='bold5339' >string-set!</strong><em id='it5340' > string k char</em></td><td id="tc5342" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code5346' ><em id='it5345' >k</em></code> must be a valid index of <code id='code5348' ><em id='it5347' >string</em></code> . <samp id='samp5349' >String-set!</samp> stores <code id='code5351' ><em id='it5350' >char</em></code> in element <code id='code5353' ><em id='it5352' >k</em></code> of <code id='code5355' ><em id='it5354' >string</em></code> and returns an unspecified value. <center id='center5364' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5362' >(<font color="#6959cf"><strong id='bold8008' >define</strong></font> (<font color="#6959cf"><strong id='bold8010' >f</strong></font>) (make-string 3 #\*)) (<font color="#6959cf"><strong id='bold8012' >define</strong></font> (<font color="#6959cf"><strong id='bold8014' >g</strong></font>) <font color="red">"***"</font>) (string-set! (f) 0 #\?) => <em id='emph5356' >unspecified</em> (string-set! (g) 0 #\?) => <em id='emph5357' >error</em> (string-set! (symbol->string 'immutable) 0 #\?) => <em id='emph5358' >error</em> </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5368" class="mark"></a><a name="string=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5372" align="left" colspan="1"><strong id='bold5370' >string=?</strong><em id='it5371' > string1 string2</em></td><td id="tc5373" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5377" class="mark"></a><a name="string-ci=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5381" align="left" colspan="1"><strong id='bold5379' >string-ci=?</strong><em id='it5380' > string1 string2</em></td><td id="tc5382" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns <tt id='tt5385' >#t</tt> if the two strings are the same length and contain the same characters in the same positions, otherwise returns <tt id='tt5386' >#f</tt>. <samp id='samp5387' >String-ci=?</samp> treats upper and lower case letters as though they were the same character, but <samp id='samp5388' >string=?</samp> treats upper and lower case as distinct characters.<br/><br/></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5393" class="mark"></a><a name="string<?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5397" align="left" colspan="1"><strong id='bold5395' >string<?</strong><em id='it5396' > string1 string2</em></td><td id="tc5398" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5402" class="mark"></a><a name="string>?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5406" align="left" colspan="1"><strong id='bold5404' >string>?</strong><em id='it5405' > string1 string2</em></td><td id="tc5407" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5411" class="mark"></a><a name="string<=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5415" align="left" colspan="1"><strong id='bold5413' >string<=?</strong><em id='it5414' > string1 string2</em></td><td id="tc5416" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5420" class="mark"></a><a name="string>=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5424" align="left" colspan="1"><strong id='bold5422' >string>=?</strong><em id='it5423' > string1 string2</em></td><td id="tc5425" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5429" class="mark"></a><a name="string-ci<?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5433" align="left" colspan="1"><strong id='bold5431' >string-ci<?</strong><em id='it5432' > string1 string2</em></td><td id="tc5434" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5438" class="mark"></a><a name="string-ci>?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5442" align="left" colspan="1"><strong id='bold5440' >string-ci>?</strong><em id='it5441' > string1 string2</em></td><td id="tc5443" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5447" class="mark"></a><a name="string-ci<=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5451" align="left" colspan="1"><strong id='bold5449' >string-ci<=?</strong><em id='it5450' > string1 string2</em></td><td id="tc5452" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5456" class="mark"></a><a name="string-ci>=?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5460" align="left" colspan="1"><strong id='bold5458' >string-ci>=?</strong><em id='it5459' > string1 string2</em></td><td id="tc5461" align="right" colspan="1">library procedure</td></tr> </tbody></table> These procedures are the lexicographic extensions to strings of the corresponding orderings on characters. For example, <samp id='samp5464' >string<?</samp> is the lexicographic ordering on strings induced by the ordering <samp id='samp5465' >char<?</samp> on characters. If two strings differ in length but are the same up to the length of the shorter string, the shorter string is considered to be lexicographically less than the longer string.<br/><br/>Implementations may generalize these and the <samp id='samp5467' >string=?</samp> and <samp id='samp5468' >string-ci=?</samp> procedures to take more than two arguments, as with the corresponding numerical predicates.<br/><br/></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5473" class="mark"></a><a name="substring" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5477" align="left" colspan="1"><strong id='bold5475' >substring</strong><em id='it5476' > string start end</em></td><td id="tc5478" align="right" colspan="1">library procedure</td></tr> </tbody></table> <code id='code5482' ><em id='it5481' >String</em></code> must be a string, and <code id='code5484' ><em id='it5483' >start</em></code> and <code id='code5486' ><em id='it5485' >end</em></code> must be exact integers satisfying<br/><br/><center id='center5496' >0 <= <code id='code5489' ><em id='it5488' >start</em></code> <= <code id='code5491' ><em id='it5490' >end</em></code> <= <strong id='bold5495' ><tt id='tt5494' >(string-length <code id='code5493' ><em id='it5492' >string</em></code>).</tt></strong></center> <samp id='samp5497' >Substring</samp> returns a newly allocated string formed from the characters of <code id='code5499' ><em id='it5498' >string</em></code> beginning with index <code id='code5501' ><em id='it5500' >start</em></code> (inclusive) and ending with index <code id='code5503' ><em id='it5502' >end</em></code> (exclusive). </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5507" class="mark"></a><a name="string-append" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5513" align="left" colspan="1"><strong id='bold5509' >string-append</strong><em id='it5512' > <code id='code5511' ><em id='it5510' >string</em></code> ...,</em></td><td id="tc5514" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns a newly allocated string whose characters form the concatenation of the given strings. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5520" class="mark"></a><a name="string->list" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5524" align="left" colspan="1"><strong id='bold5522' >string->list</strong><em id='it5523' > string</em></td><td id="tc5525" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5529" class="mark"></a><a name="list->string" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5533" align="left" colspan="1"><strong id='bold5531' >list->string</strong><em id='it5532' > list</em></td><td id="tc5534" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp5537' >String->list</samp> returns a newly allocated list of the characters that make up the given string. <samp id='samp5538' >List->string</samp> returns a newly allocated string formed from the characters in the list <code id='code5540' ><em id='it5539' >list</em></code>, which must be a list of characters. <samp id='samp5541' >String->list</samp> and <samp id='samp5542' >list->string</samp> are inverses so far as <samp id='samp5543' >equal?</samp> is concerned. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5547" class="mark"></a><a name="string-copy" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5551" align="left" colspan="1"><strong id='bold5549' >string-copy</strong><em id='it5550' > string</em></td><td id="tc5552" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns a newly allocated copy of the given <code id='code5556' ><em id='it5555' >string</em></code>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5560" class="mark"></a><a name="string-fill!" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5564" align="left" colspan="1"><strong id='bold5562' >string-fill!</strong><em id='it5563' > string char</em></td><td id="tc5565" align="right" colspan="1">library procedure</td></tr> </tbody></table> Stores <code id='code5569' ><em id='it5568' >char</em></code> in every element of the given <code id='code5571' ><em id='it5570' >string</em></code> and returns an unspecified value. </td></tr> </tbody></table><br/> </div> <!-- Vectors --> <a name="Vectors"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.3.6 Vectors</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Vectors" class="mark"></a> Vectors are heterogenous structures whose elements are indexed by integers. A vector typically occupies less space than a list of the same length, and the average time required to access a randomly chosen element is typically less for the vector than for the list.<br/><br/>The <em id='emph5575' >length</em> of a vector is the number of elements that it contains. This number is a non-negative integer that is fixed when the vector is created. The <em id='emph5576' >valid indexes</em> of a <a name="g5578" class="mark"></a>vector are the exact non-negative integers less than the length of the vector. The first element in a vector is indexed by zero, and the last element is indexed by one less than the length of the vector.<br/><br/>Vectors are written using the notation <tt id='tt5583' >#(<code id='code5582' ><em id='it5581' >obj</em></code> ...,)</tt>. For example, a vector of length 3 containing the number zero in element 0, the list <samp id='samp5584' >(2 2 2 2)</samp> in element 1, and the string <samp id='samp5585' >"Anna"</samp> in element 2 can be written as following:<br/><br/><br/><center id='center5590' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5588' >#(0 (2 2 2 2) <font color="red">"Anna"</font>) </pre> </td></tr> </tbody></table></center> Note that this is the external representation of a vector, not an expression evaluating to a vector. Like list constants, vector constants must be quoted:<br/><br/><center id='center5596' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5594' >'#(0 (2 2 2 2) <font color="red">"Anna"</font>) => #(0 (2 2 2 2) <font color="red">"Anna"</font>) </pre> </td></tr> </tbody></table></center> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5598" class="mark"></a><a name="vector?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5602" align="left" colspan="1"><strong id='bold5600' >vector?</strong><em id='it5601' > obj</em></td><td id="tc5603" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt5606' >#t</tt> if <code id='code5608' ><em id='it5607' >obj</em></code> is a vector, otherwise returns <tt id='tt5609' >#f</tt>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5614" align="left" colspan="1"><strong id='bold5612' >make-vector</strong><em id='it5613' > k</em></td><td id="tc5615" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5620" align="left" colspan="1"><strong id='bold5618' >make-vector</strong><em id='it5619' > k fill</em></td><td id="tc5621" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns a newly allocated vector of <code id='code5625' ><em id='it5624' >k</em></code> elements. If a second argument is given, then each element is initialized to <code id='code5627' ><em id='it5626' >fill</em></code>. Otherwise the initial contents of each element is unspecified. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5631" class="mark"></a><a name="vector" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5635" align="left" colspan="1"><strong id='bold5633' >vector</strong><em id='it5634' > obj ...,</em></td><td id="tc5636" align="right" colspan="1">library procedure</td></tr> </tbody></table> Returns a newly allocated vector whose elements contain the given arguments. Analogous to <samp id='samp5639' >list</samp>.<br/><br/><center id='center5643' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5641' >(vector 'a 'b 'c) => #(a b c) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5647" class="mark"></a><a name="vector-length" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5651" align="left" colspan="1"><strong id='bold5649' >vector-length</strong><em id='it5650' > vector</em></td><td id="tc5652" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the number of elements in <code id='code5656' ><em id='it5655' >vector</em></code> as an exact integer. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5661" align="left" colspan="1"><strong id='bold5659' >vector-ref</strong><em id='it5660' > vector k</em></td><td id="tc5662" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code5666' ><em id='it5665' >k</em></code> must be a valid index of <code id='code5668' ><em id='it5667' >vector</em></code>. <samp id='samp5669' >Vector-ref</samp> returns the contents of element <code id='code5671' ><em id='it5670' >k</em></code> of <code id='code5673' ><em id='it5672' >vector</em></code>.<br/><br/><center id='center5679' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5677' >(vector-ref '#(1 1 2 3 5 8 13 21) 5) => 8 (vector-ref '#(1 1 2 3 5 8 13 21) (<strong id='bold8020' >let</strong> ((i (round (* 2 (acos -1))))) (<strong id='bold8021' >if</strong> (inexact? i) (inexact->exact i) i))) => 13 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5683" class="mark"></a><a name="vector-set!" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5687" align="left" colspan="1"><strong id='bold5685' >vector-set!</strong><em id='it5686' > vector k obj</em></td><td id="tc5688" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code5692' ><em id='it5691' >k</em></code> must be a valid index of <code id='code5694' ><em id='it5693' >vector</em></code>. <samp id='samp5695' >Vector-set!</samp> stores <code id='code5697' ><em id='it5696' >obj</em></code> in element <code id='code5699' ><em id='it5698' >k</em></code> of <code id='code5701' ><em id='it5700' >vector</em></code>. The value returned by <samp id='samp5702' >vector-set!</samp> is unspecified. <center id='center5715' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5713' >(<strong id='bold8022' >let</strong> ((vec (vector 0 '(2 2 2 2) <font color="red">"Anna"</font>))) (vector-set! vec 1 '(<font color="red">"Sue"</font> <font color="red">"Sue"</font>)) vec) => #(0 (<font color="red">"Sue"</font> <font color="red">"Sue"</font>) <font color="red">"Anna"</font>)<br/><br/>(vector-set! '#(0 1 2) 1 <font color="red">"doe"</font>) => <em id='emph5704' >error</em> ; constant vector </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5719" class="mark"></a><a name="vector->list" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5723" align="left" colspan="1"><strong id='bold5721' >vector->list</strong><em id='it5722' > vector</em></td><td id="tc5724" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g5728" class="mark"></a><a name="list->vector" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5732" align="left" colspan="1"><strong id='bold5730' >list->vector</strong><em id='it5731' > list</em></td><td id="tc5733" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp5736' >Vector->list</samp> returns a newly allocated list of the objects contained in the elements of <code id='code5738' ><em id='it5737' >vector</em></code>. <samp id='samp5739' >List->vector</samp> returns a newly created vector initialized to the elements of the list <code id='code5741' ><em id='it5740' >list</em></code>.<br/><br/><center id='center5745' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5743' >(vector->list '#(dah dah didah)) => (dah dah didah) (list->vector '(dididit dah)) => #(dididit dah) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5749" class="mark"></a><a name="vector-fill!" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5753" align="left" colspan="1"><strong id='bold5751' >vector-fill!</strong><em id='it5752' > vector fill</em></td><td id="tc5754" align="right" colspan="1">library procedure</td></tr> </tbody></table> Stores <code id='code5758' ><em id='it5757' >fill</em></code> in every element of <code id='code5760' ><em id='it5759' >vector</em></code>. The value returned by <samp id='samp5761' >vector-fill!</samp> is unspecified. </td></tr> </tbody></table><br/> </div> </div><br> <!-- Control features --> <a name="Control-features"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">6.4 Control features</font> </h3></td></tr></table> </div><div class="section"> <a name="Control-features" class="mark"></a> This chapter describes various primitive procedures which control the flow of program execution in special ways. The <samp id='samp5764' >procedure?</samp> predicate is also described here.<br/><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5767" class="mark"></a><a name="procedure?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5771" align="left" colspan="1"><strong id='bold5769' >procedure?</strong><em id='it5770' > obj</em></td><td id="tc5772" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt5775' >#t</tt> if <code id='code5777' ><em id='it5776' >obj</em></code> is a procedure, otherwise returns <tt id='tt5778' >#f</tt>.<br/><br/><center id='center5784' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5782' >(procedure? car) => #t (procedure? 'car) => #f (procedure? (<strong id='bold8030' >lambda</strong> (x) (* x x))) => #t (procedure? '(<strong id='bold8031' >lambda</strong> (x) (* x x))) => #f (call-with-current-continuation procedure?) => #t </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5788" class="mark"></a><a name="apply" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5792" align="left" colspan="1"><strong id='bold5790' >apply</strong><em id='it5791' > proc arg1 ... args</em></td><td id="tc5793" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code5797' ><em id='it5796' >Proc</em></code> must be a procedure and <code id='code5799' ><em id='it5798' >args</em></code> must be a list. Calls <code id='code5801' ><em id='it5800' >proc</em></code> with the elements of the list <samp id='samp5806' >(append (list <code id='code5803' ><em id='it5802' >arg1</em></code> ...,) <code id='code5805' ><em id='it5804' >args</em></code>)</samp> as the actual arguments.<br/><br/><center id='center5817' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5815' >(<strong id='bold8032' >apply</strong> + (list 3 4)) => 7<br/><br/>(<font color="#6959cf"><strong id='bold8033' >define</strong></font> <font color="#6959cf"><strong id='bold8035' >compose</strong></font> (<strong id='bold8037' >lambda</strong> (f g) (<strong id='bold8038' >lambda</strong> args (f (<strong id='bold8039' >apply</strong> g args)))))<br/><br/>((compose sqrt *) 12 75) => 30 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5821" class="mark"></a><a name="map" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5825" align="left" colspan="1"><strong id='bold5823' >map</strong><em id='it5824' > proc list1 list2 ...,</em></td><td id="tc5826" align="right" colspan="1">library procedure</td></tr> </tbody></table> The <code id='code5830' ><em id='it5829' >list</em></code>s must be lists, and <code id='code5832' ><em id='it5831' >proc</em></code> must be a procedure taking as many arguments as there are <em id='it5833' >list</em>s and returning a single value. If more than one <code id='code5835' ><em id='it5834' >list</em></code> is given, then they must all be the same length. <samp id='samp5836' >Map</samp> applies <code id='code5838' ><em id='it5837' >proc</em></code> element-wise to the elements of the <code id='code5840' ><em id='it5839' >list</em></code>s and returns a list of the results, in order. The dynamic order in which <code id='code5842' ><em id='it5841' >proc</em></code> is applied to the elements of the <code id='code5844' ><em id='it5843' >list</em></code>s is unspecified.<br/><br/><center id='center5857' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5855' >(map cadr '((a b) (d e) (g h))) => (b e h)<br/><br/>(map (<strong id='bold8040' >lambda</strong> (n) (expt n n)) '(1 2 3 4 5)) => (1 4 27 256 3125)<br/><br/>(map + '(1 2 3) '(4 5 6)) => (5 7 9)<br/><br/>(<strong id='bold8041' >let</strong> ((count 0)) (map (<strong id='bold8042' >lambda</strong> (ignored) (<strong id='bold8043' >set!</strong> count (+ count 1)) count) '(a b))) => (1 2) <code id='code5850' ><em id='it5849' >or</em></code> (2 1) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5861" class="mark"></a><a name="for-each" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5865" align="left" colspan="1"><strong id='bold5863' >for-each</strong><em id='it5864' > proc list1 list2 ...,</em></td><td id="tc5866" align="right" colspan="1">library procedure</td></tr> </tbody></table> The arguments to <samp id='samp5869' >for-each</samp> are like the arguments to <samp id='samp5870' >map</samp>, but <samp id='samp5871' >for-each</samp> calls <code id='code5873' ><em id='it5872' >proc</em></code> for its side effects rather than for its values. Unlike <samp id='samp5874' >map</samp>, <samp id='samp5875' >for-each</samp> is guaranteed to call <code id='code5877' ><em id='it5876' >proc</em></code> on the elements of the <code id='code5879' ><em id='it5878' >list</em></code>s in order from the first element(s) to the last, and the value returned by <samp id='samp5880' >for-each</samp> is unspecified.<br/><br/><center id='center5886' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5884' >(<strong id='bold8044' >let</strong> ((v (make-vector 5))) (for-each (<strong id='bold8045' >lambda</strong> (i) (vector-set! v i (* i i))) '(0 1 2 3 4)) v) => #(0 1 4 9 16) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g5890" class="mark"></a><a name="force" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc5894" align="left" colspan="1"><strong id='bold5892' >force</strong><em id='it5893' > promise</em></td><td id="tc5895" align="right" colspan="1">library procedure</td></tr> </tbody></table> Forces the value of <code id='code5899' ><em id='it5898' >promise</em></code> (see <code id='code5900' >delay</code>, <a name="g5902" class="mark"></a>section <a href="r5rs-7.html#Delayed-evaluation" class="inbound">Delayed evaluation</a>). If no value has been computed for <a name="g5905" class="mark"></a>the promise, then a value is computed and returned. The value of the promise is cached (or ``memoized'') so that if it is forced a second time, the previously computed value is returned. <br/><br/><center id='center5919' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5917' >(force (delay (+ 1 2))) => 3 (<strong id='bold8046' >let</strong> ((p (delay (+ 1 2)))) (list (force p) (force p))) => (3 3)<br/><br/>(<font color="#6959cf"><strong id='bold8047' >define</strong></font> <font color="#6959cf"><strong id='bold8049' >a-stream</strong></font> (<strong id='bold8051' >letrec</strong> ((next (<strong id='bold8052' >lambda</strong> (n) (cons n (delay (next (+ n 1))))))) (next 0))) (<font color="#6959cf"><strong id='bold8053' >define</strong></font> <font color="#6959cf"><strong id='bold8055' >head</strong></font> car) (<font color="#6959cf"><strong id='bold8057' >define</strong></font> <font color="#6959cf"><strong id='bold8059' >tail</strong></font> (<strong id='bold8061' >lambda</strong> (stream) (force (cdr stream))))<br/><br/>(head (tail (tail a-stream))) => 2 </pre> </td></tr> </tbody></table></center> <samp id='samp5920' >Force</samp> and <samp id='samp5921' >delay</samp> are mainly intended for programs written in functional style. The following examples should not be considered to illustrate good programming style, but they illustrate the property that only one value is computed for a promise, no matter how many times it is forced. <center id='center5932' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5930' >(<font color="#6959cf"><strong id='bold8062' >define</strong></font> <font color="#6959cf"><strong id='bold8064' >count</strong></font> 0) (<font color="#6959cf"><strong id='bold8066' >define</strong></font> <font color="#6959cf"><strong id='bold8068' >p</strong></font> (delay (<strong id='bold8070' >begin</strong> (<strong id='bold8071' >set!</strong> count (+ count 1)) (<strong id='bold8072' >if</strong> (> count x) count (force p))))) (<font color="#6959cf"><strong id='bold8073' >define</strong></font> <font color="#6959cf"><strong id='bold8075' >x</strong></font> 5) p => a promise (force p) => 6 p => a promise, still (<strong id='bold8077' >begin</strong> (<strong id='bold8078' >set!</strong> x 10) (force p)) => 6 </pre> </td></tr> </tbody></table></center> Here is a possible implementation of <samp id='samp5933' >delay</samp> and <samp id='samp5934' >force</samp>. Promises are implemented here as procedures of no arguments, and <samp id='samp5935' >force</samp> simply calls its argument:<br/><br/><center id='center5941' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5939' >(<font color="#6959cf"><strong id='bold8079' >define</strong></font> <font color="#6959cf"><strong id='bold8081' >force</strong></font> (<strong id='bold8083' >lambda</strong> (object) (object))) </pre> </td></tr> </tbody></table></center> We define the expression<br/><br/><center id='center5945' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog5943' >(delay <expression>) </pre> </td></tr> </tbody></table></center> to have the same meaning as the procedure call<br/><br/><center id='center5949' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ccccff"><pre class="prog" id='prog5947' >(make-promise (lambda () <expression>)) </pre> </td></tr> </tbody></table></center> as follows<br/><br/><center id='center5956' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5954' >(<font color="#6959cf"><strong id='bold8084' >define-syntax</strong></font> <font color="#6959cf"><strong id='bold8086' >delay</strong></font> (<strong id='bold8088' >syntax-rules</strong> () ((delay expression) (make-promise (<strong id='bold8089' >lambda</strong> () expression))))), </pre> </td></tr> </tbody></table></center> where <samp id='samp5957' >make-promise</samp> is defined as follows:<br/><br/> <center id='center5971' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5969' >(<font color="#6959cf"><strong id='bold8090' >define</strong></font> <font color="#6959cf"><strong id='bold8092' >make-promise</strong></font> (<strong id='bold8094' >lambda</strong> (proc) (<strong id='bold8095' >let</strong> ((result-ready? #f) (result #f)) (<strong id='bold8096' >lambda</strong> () (<strong id='bold8097' >if</strong> result-ready? result (<strong id='bold8098' >let</strong> ((x (proc))) (<strong id='bold8099' >if</strong> result-ready? result (<strong id='bold8100' >begin</strong> (<strong id='bold8101' >set!</strong> result-ready? #t) (<strong id='bold8102' >set!</strong> result x) result)))))))) </pre> </td></tr> </tbody></table></center> <br/><em id='emph5973' >Rationale:</em> A promise may refer to its own value, as in the last example above. Forcing such a promise may cause the promise to be forced a second time before the value of the first force has been computed. This complicates the definition of <samp id='samp5974' >make-promise</samp>.<br/><br/>Various extensions to this semantics of <samp id='samp5976' >delay</samp> and <samp id='samp5977' >force</samp> are supported in some implementations:<br/><br/><ul class="itemize" id='itemize5998' ><li>Calling <samp id='samp5979' >force</samp> on an object that is not a promise may simply return the object.<br/><br/></li> <li>It may be the case that there is no means by which a promise can be operationally distinguished from its forced value. That is, expressions like the following may evaluate to either <tt id='tt5982' >#t</tt> or to <tt id='tt5983' >#f</tt>, depending on the implementation:<br/><br/><center id='center5989' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5987' >(eqv? (delay 1) 1) => <em id='emph5985' >unspecified</em> (pair? (delay (cons 1 2))) => <em id='emph5986' >unspecified</em> </pre> </td></tr> </tbody></table></center> </li> <li>Some implementations may implement ``implicit forcing,'' where the value of a promise is forced by primitive procedures like <samp id='samp5991' >cdr</samp> and <samp id='samp5992' >+</samp>:<br/><br/><center id='center5996' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog5994' >(+ (delay (* 3 7)) 13) => 34 </pre> </td></tr> </tbody></table></center> </li> </ul></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6002" class="mark"></a><a name="call-with-current-continuation" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6006" align="left" colspan="1"><strong id='bold6004' >call-with-current-continuation</strong><em id='it6005' > proc</em></td><td id="tc6007" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code6011' ><em id='it6010' >Proc</em></code> must be a procedure of one argument. The procedure <samp id='samp6012' >call-with-current-continuation</samp> packages up the current continuation (see the rationale below) as an ``escape procedure'' and passes it as an argument to <a name="g6014" class="mark"></a><code id='code6017' ><em id='it6016' >proc</em></code>. The escape procedure is a Scheme procedure that, if it is later called, will abandon whatever continuation is in effect at that later time and will instead use the continuation that was in effect when the escape procedure was created. Calling the escape procedure may cause the invocation of <code id='code6019' ><em id='it6018' >before</em></code> and <code id='code6021' ><em id='it6020' >after</em></code> thunks installed using <code id='code6022' >dynamic-wind</code>. <a name="g6024" class="mark"></a> The escape procedure accepts the same number of arguments as the continuation to the original call to <tt id='tt6026' >call-with-current-continuation</tt>. Except for continuations created by the <samp id='samp6027' >call-with-values</samp> procedure, all continuations take exactly one value. The effect of passing no value or more than one value to continuations that were not created by <tt id='tt6028' >call-with-values</tt> is unspecified.<br/><br/>The escape procedure that is passed to <code id='code6031' ><em id='it6030' >proc</em></code> has unlimited extent just like any other procedure in Scheme. It may be stored in variables or data structures and may be called as many times as desired.<br/><br/>The following examples show only the most common ways in which <samp id='samp6033' >call-with-current-continuation</samp> is used. If all real uses were as simple as these examples, there would be no need for a procedure with the power of <samp id='samp6034' >call-with-current-continuation</samp>.<br/><br/><center id='center6050' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6048' >(call-with-current-continuation (<strong id='bold8103' >lambda</strong> (exit) (for-each (<strong id='bold8104' >lambda</strong> (x) (<strong id='bold8105' >if</strong> (negative? x) (exit x))) '(54 0 37 -3 245 19)) #t)) => -3<br/><br/>(<font color="#6959cf"><strong id='bold8106' >define</strong></font> <font color="#6959cf"><strong id='bold8108' >list-length</strong></font> (<strong id='bold8110' >lambda</strong> (obj) (call-with-current-continuation (<strong id='bold8111' >lambda</strong> (return) (<strong id='bold8112' >letrec</strong> ((r (<strong id='bold8113' >lambda</strong> (obj) (<strong id='bold8114' >cond</strong> ((null? obj) 0) ((pair? obj) (+ (r (cdr obj)) 1)) (else (return #f)))))) (r obj))))))<br/><br/>(list-length '(1 2 3 4)) => 4<br/><br/>(list-length '(a b . c)) => #f </pre> </td></tr> </tbody></table></center> <br/><em id='emph6052' >Rationale:</em><br/><br/>A common use of <samp id='samp6054' >call-with-current-continuation</samp> is for structured, non-local exits from loops or procedure bodies, but in fact <samp id='samp6055' >call-with-current-continuation</samp> is extremely useful for implementing a wide variety of advanced control structures.<br/><br/>Whenever a Scheme expression is evaluated there is a "continuation" wanting the result of the expression. The continuation <a name="g6058" class="mark"></a>represents an entire (default) future for the computation. If the expression is evaluated at top level, for example, then the continuation might take the result, print it on the screen, prompt for the next input, evaluate it, and so on forever. Most of the time the continuation includes actions specified by user code, as in a continuation that will take the result, multiply it by the value stored in a local variable, add seven, and give the answer to the top level continuation to be printed. Normally these ubiquitous continuations are hidden behind the scenes and programmers do not think much about them. On rare occasions, however, a programmer may need to deal with continuations explicitly. <samp id='samp6060' >Call-with-current-continuation</samp> allows Scheme programmers to do that by creating a procedure that acts just like the current continuation.<br/><br/>Most programming languages incorporate one or more special-purpose escape constructs with names like <tt id='tt6062' >exit</tt>, <strong id='bold6064' ><samp id='samp6063' >return</samp></strong>, or even <tt id='tt6065' >goto</tt>. In 1965, however, Peter Landin [Landin65] invented a general purpose escape operator called the J-operator. John Reynolds [Reynolds72] described a simpler but equally powerful construct in 1972. The <samp id='samp6066' >catch</samp> special form described by Sussman and Steele in the 1975 report on Scheme is exactly the same as Reynolds's construct, though its name came from a less general construct in MacLisp. Several Scheme implementors noticed that the full power of the <code id='code6067' >catch</code> construct could be provided by a procedure instead of by a <a name="g6069" class="mark"></a>special syntactic construct, and the name <samp id='samp6071' >call-with-current-continuation</samp> was coined in 1982. This name is descriptive, but opinions differ on the merits of such a long name, and some people use the name <code id='code6072' >call/cc</code> instead. <a name="g6074" class="mark"></a></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6079" class="mark"></a><a name="values" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6083" align="left" colspan="1"><strong id='bold6081' >values</strong><em id='it6082' > obj ...</em></td><td id="tc6084" align="right" colspan="1">procedure</td></tr> </tbody></table> Delivers all of its arguments to its continuation. Except for continuations created by the <code id='code6087' >call-with-values</code> <a name="g6089" class="mark"></a>procedure, all continuations take exactly one value. <tt id='tt6091' >Values</tt> might be defined as follows:<br/><br/><center id='center6098' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6096' >(<font color="#6959cf"><strong id='bold8115' >define</strong></font> (<font color="#6959cf"><strong id='bold8117' >values</strong></font> . things) (call-with-current-continuation (<strong id='bold8119' >lambda</strong> (cont) (<strong id='bold8120' >apply</strong> cont things)))) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6102" class="mark"></a><a name="call-with-values" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6106" align="left" colspan="1"><strong id='bold6104' >call-with-values</strong><em id='it6105' > producer consumer</em></td><td id="tc6107" align="right" colspan="1">procedure</td></tr> </tbody></table> Calls its <code id='code6111' ><em id='it6110' >producer</em></code> argument with no values and a continuation that, when passed some values, calls the <code id='code6113' ><em id='it6112' >consumer</em></code> procedure with those values as arguments. The continuation for the call to <code id='code6115' ><em id='it6114' >consumer</em></code> is the continuation of the call to <tt id='tt6116' >call-with-values</tt>.<br/><br/><br/><center id='center6123' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6121' >(call-with-values (<strong id='bold8121' >lambda</strong> () (values 4 5)) (<strong id='bold8122' >lambda</strong> (a b) b)) => 5<br/><br/>(call-with-values * -) => -1 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6127" class="mark"></a><a name="dynamic-wind" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6131" align="left" colspan="1"><strong id='bold6129' >dynamic-wind</strong><em id='it6130' > before thunk after</em></td><td id="tc6132" align="right" colspan="1">procedure</td></tr> </tbody></table> Calls <code id='code6136' ><em id='it6135' >thunk</em></code> without arguments, returning the result(s) of this call. <code id='code6138' ><em id='it6137' >Before</em></code> and <code id='code6140' ><em id='it6139' >after</em></code> are called, also without arguments, as required by the following rules (note that in the absence of calls to continuations captured using <code id='code6141' >call-with-current-continuation</code> the three arguments are <a name="g6143" class="mark"></a>called once each, in order). <code id='code6146' ><em id='it6145' >Before</em></code> is called whenever execution enters the dynamic extent of the call to <code id='code6148' ><em id='it6147' >thunk</em></code> and <code id='code6150' ><em id='it6149' >after</em></code> is called whenever it exits that dynamic extent. The dynamic extent of a procedure call is the period between when the call is initiated and when it returns. In Scheme, because of <samp id='samp6151' >call-with-current-continuation</samp>, the dynamic extent of a call may not be a single, connected time period. It is defined as follows:<br/><br/><ul class="itemize" id='itemize6162' ><li>The dynamic extent is entered when execution of the body of the called procedure begins.<br/><br/></li> <li>The dynamic extent is also entered when execution is not within the dynamic extent and a continuation is invoked that was captured (using <samp id='samp6155' >call-with-current-continuation</samp>) during the dynamic extent.<br/><br/></li> <li>It is exited when the called procedure returns.<br/><br/></li> <li>It is also exited when execution is within the dynamic extent and a continuation is invoked that was captured while not within the dynamic extent.<br/><br/></li> </ul> If a second call to <samp id='samp6163' >dynamic-wind</samp> occurs within the dynamic extent of the call to <code id='code6165' ><em id='it6164' >thunk</em></code> and then a continuation is invoked in such a way that the <code id='code6167' ><em id='it6166' >after</em></code>s from these two invocations of <samp id='samp6168' >dynamic-wind</samp> are both to be called, then the <code id='code6170' ><em id='it6169' >after</em></code> associated with the second (inner) call to <samp id='samp6171' >dynamic-wind</samp> is called first.<br/><br/>If a second call to <samp id='samp6173' >dynamic-wind</samp> occurs within the dynamic extent of the call to <code id='code6175' ><em id='it6174' >thunk</em></code> and then a continuation is invoked in such a way that the <code id='code6177' ><em id='it6176' >before</em></code>s from these two invocations of <samp id='samp6178' >dynamic-wind</samp> are both to be called, then the <code id='code6180' ><em id='it6179' >before</em></code> associated with the first (outer) call to <samp id='samp6181' >dynamic-wind</samp> is called first.<br/><br/>If invoking a continuation requires calling the <code id='code6184' ><em id='it6183' >before</em></code> from one call to <samp id='samp6185' >dynamic-wind</samp> and the <code id='code6187' ><em id='it6186' >after</em></code> from another, then the <code id='code6189' ><em id='it6188' >after</em></code> is called first.<br/><br/>The effect of using a captured continuation to enter or exit the dynamic extent of a call to <code id='code6192' ><em id='it6191' >before</em></code> or <code id='code6194' ><em id='it6193' >after</em></code> is undefined.<br/><br/><center id='center6208' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6206' >(<strong id='bold8123' >let</strong> ((path '()) (c #f)) (<strong id='bold8124' >let</strong> ((add (<strong id='bold8125' >lambda</strong> (s) (<strong id='bold8126' >set!</strong> path (cons s path))))) (dynamic-wind (<strong id='bold8127' >lambda</strong> () (add 'connect)) (<strong id='bold8128' >lambda</strong> () (add (call-with-current-continuation (<strong id='bold8129' >lambda</strong> (c0) (<strong id='bold8130' >set!</strong> c c0) 'talk1)))) (<strong id='bold8131' >lambda</strong> () (add 'disconnect))) (<strong id='bold8132' >if</strong> (< (length path) 4) (c 'talk2) (reverse path)))) => (connect talk1 disconnect connect talk2 disconnect) </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> </div><br> <!-- Eval --> <a name="Eval"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">6.5 Eval</font> </h3></td></tr></table> </div><div class="section"> <a name="Eval" class="mark"></a> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6212" class="mark"></a><a name="eval" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6216" align="left" colspan="1"><strong id='bold6214' >eval</strong><em id='it6215' > expression environment-specifier</em></td><td id="tc6217" align="right" colspan="1">procedure</td></tr> </tbody></table> Evaluates <code id='code6221' ><em id='it6220' >expression</em></code> in the specified environment and returns its value. <code id='code6223' ><em id='it6222' >Expression</em></code> must be a valid Scheme expression represented as data, and <code id='code6225' ><em id='it6224' >environment-specifier</em></code> must be a value returned by one of the three procedures described below. Implementations may extend <samp id='samp6226' >eval</samp> to allow non-expression programs (definitions) as the first argument and to allow other values as environments, with the restriction that <samp id='samp6227' >eval</samp> is not allowed to create new bindings in the environments associated with <samp id='samp6228' >null-environment</samp> or <samp id='samp6229' >scheme-report-environment</samp>.<br/><br/><center id='center6236' ><table cellspacing="0" class="color" cellpadding="0" width="95%"><tbody> <tr><td bgcolor="#ffffcc"><pre class="prog" id='prog6234' >(eval '(* 7 3) (scheme-report-environment 5)) => 21<br/><br/>(<strong id='bold8133' >let</strong> ((f (eval '(<strong id='bold8134' >lambda</strong> (f x) (f x x)) (null-environment 5)))) (f + 10)) => 20 </pre> </td></tr> </tbody></table></center> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6240" class="mark"></a><a name="scheme-report-environment" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6244" align="left" colspan="1"><strong id='bold6242' >scheme-report-environment</strong><em id='it6243' > version</em></td><td id="tc6245" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g6249" class="mark"></a><a name="null-environment" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6253" align="left" colspan="1"><strong id='bold6251' >null-environment</strong><em id='it6252' > version</em></td><td id="tc6254" align="right" colspan="1">procedure</td></tr> </tbody></table> <code id='code6258' ><em id='it6257' >Version</em></code> must be the exact integer <samp id='samp6259' >5</samp>, corresponding to this revision of the Scheme report (the Revised^5 Report on Scheme). <samp id='samp6260' >Scheme-report-environment</samp> returns a specifier for an environment that is empty except for all bindings defined in this report that are either required or both optional and supported by the implementation. <samp id='samp6261' >Null-environment</samp> returns a specifier for an environment that is empty except for the (syntactic) bindings for all syntactic keywords defined in this report that are either required or both optional and supported by the implementation.<br/><br/>Other values of <code id='code6264' ><em id='it6263' >version</em></code> can be used to specify environments matching past revisions of this report, but their support is not required. An implementation will signal an error if <code id='code6266' ><em id='it6265' >version</em></code> is neither <samp id='samp6267' >5</samp> nor another value supported by the implementation.<br/><br/>The effect of assigning (through the use of <samp id='samp6269' >eval</samp>) a variable bound in a <samp id='samp6270' >scheme-report-environment</samp> (for example <samp id='samp6271' >car</samp>) is unspecified. Thus the environments specified by <samp id='samp6272' >scheme-report-environment</samp> may be immutable. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6276" class="mark"></a><a name="interaction-environment" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6280" align="left" colspan="1"><strong id='bold6278' >interaction-environment</strong><em id='it6279' > </em></td><td id="tc6281" align="right" colspan="1">optional procedure</td></tr> </tbody></table> This procedure returns a specifier for the environment that contains implementation-defined bindings, typically a superset of those listed in the report. The intent is that this procedure will return the environment in which the implementation would evaluate expressions dynamically typed by the user. </td></tr> </tbody></table><br/> </div><br> <!-- Input and output --> <a name="Input-and-output"></a> <div class="section-atitle"><table width="100%"><tr><td bgcolor="#dedeff"><h3><font color="black">6.6 Input and output</font> </h3></td></tr></table> </div><div class="section"> <a name="Input-and-output" class="mark"></a> <!-- Ports --> <a name="Ports"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.6.1 Ports</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Ports" class="mark"></a> Ports represent input and output devices. To Scheme, an input port is a Scheme object that can deliver characters upon command, while an output port is a Scheme object that can accept characters. <a name="g6287" class="mark"></a> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6290" class="mark"></a><a name="call-with-input-file" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6294" align="left" colspan="1"><strong id='bold6292' >call-with-input-file</strong><em id='it6293' > string proc</em></td><td id="tc6295" align="right" colspan="1">library procedure</td></tr> </tbody></table> <a name="g6299" class="mark"></a><a name="call-with-output-file" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6303" align="left" colspan="1"><strong id='bold6301' >call-with-output-file</strong><em id='it6302' > string proc</em></td><td id="tc6304" align="right" colspan="1">library procedure</td></tr> </tbody></table> <code id='code6308' ><em id='it6307' >String</em></code> should be a string naming a file, and <code id='code6310' ><em id='it6309' >proc</em></code> should be a procedure that accepts one argument. For <samp id='samp6311' >call-with-input-file</samp>, the file should already exist; for <samp id='samp6312' >call-with-output-file</samp>, the effect is unspecified if the file already exists. These procedures call <code id='code6314' ><em id='it6313' >proc</em></code> with one argument: the port obtained by opening the named file for input or output. If the file cannot be opened, an error is signalled. If <code id='code6316' ><em id='it6315' >proc</em></code> returns, then the port is closed automatically and the value(s) yielded by the <code id='code6318' ><em id='it6317' >proc</em></code> is(are) returned. If <code id='code6320' ><em id='it6319' >proc</em></code> does not return, then the port will not be closed automatically unless it is possible to prove that the port will never again be used for a read or write operation. <br/><em id='emph6322' >Rationale:</em> Because Scheme's escape procedures have unlimited extent, it is possible to escape from the current continuation but later to escape back in. If implementations were permitted to close the port on any escape from the current continuation, then it would be impossible to write portable code using both <samp id='samp6323' >call-with-current-continuation</samp> and <samp id='samp6324' >call-with-input-file</samp> or <samp id='samp6325' >call-with-output-file</samp>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6329" class="mark"></a><a name="input-port?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6333" align="left" colspan="1"><strong id='bold6331' >input-port?</strong><em id='it6332' > obj</em></td><td id="tc6334" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g6338" class="mark"></a><a name="output-port?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6342" align="left" colspan="1"><strong id='bold6340' >output-port?</strong><em id='it6341' > obj</em></td><td id="tc6343" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt6346' >#t</tt> if <code id='code6348' ><em id='it6347' >obj</em></code> is an input port or output port respectively, otherwise returns <tt id='tt6349' >#f</tt>.<br/><br/></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6354" class="mark"></a><a name="current-input-port" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6358" align="left" colspan="1"><strong id='bold6356' >current-input-port</strong><em id='it6357' > </em></td><td id="tc6359" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g6363" class="mark"></a><a name="current-output-port" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6367" align="left" colspan="1"><strong id='bold6365' >current-output-port</strong><em id='it6366' > </em></td><td id="tc6368" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the current default input or output port. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6374" class="mark"></a><a name="with-input-from-file" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6378" align="left" colspan="1"><strong id='bold6376' >with-input-from-file</strong><em id='it6377' > string thunk</em></td><td id="tc6379" align="right" colspan="1">optional procedure</td></tr> </tbody></table> <a name="g6383" class="mark"></a><a name="with-output-to-file" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6387" align="left" colspan="1"><strong id='bold6385' >with-output-to-file</strong><em id='it6386' > string thunk</em></td><td id="tc6388" align="right" colspan="1">optional procedure</td></tr> </tbody></table> <code id='code6392' ><em id='it6391' >String</em></code> should be a string naming a file, and <code id='code6394' ><em id='it6393' >proc</em></code> should be a procedure of no arguments. For <samp id='samp6395' >with-input-from-file</samp>, the file should already exist; for <samp id='samp6396' >with-output-to-file</samp>, the effect is unspecified if the file already exists. The file is opened for input or output, an input or output port connected to it is made the default value returned by <samp id='samp6397' >current-input-port</samp> or <samp id='samp6398' >current-output-port</samp> (and is used by <tt id='tt6399' >(read)</tt>, <tt id='tt6402' >(write <code id='code6401' ><em id='it6400' >obj</em></code>)</tt>, and so forth), and the <code id='code6404' ><em id='it6403' >thunk</em></code> is called with no arguments. When the <code id='code6406' ><em id='it6405' >thunk</em></code> returns, the port is closed and the previous default is restored. <samp id='samp6407' >With-input-from-file</samp> and <samp id='samp6408' >with-output-to-file</samp> return(s) the value(s) yielded by <code id='code6410' ><em id='it6409' >thunk</em></code>. If an escape procedure is used to escape from the continuation of these procedures, their behavior is implementation dependent.<br/><br/> </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6415" class="mark"></a><a name="open-input-file" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6419" align="left" colspan="1"><strong id='bold6417' >open-input-file</strong><em id='it6418' > filename</em></td><td id="tc6420" align="right" colspan="1">procedure</td></tr> </tbody></table> Takes a string naming an existing file and returns an input port capable of delivering characters from the file. If the file cannot be opened, an error is signalled. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6426" class="mark"></a><a name="open-output-file" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6430" align="left" colspan="1"><strong id='bold6428' >open-output-file</strong><em id='it6429' > filename</em></td><td id="tc6431" align="right" colspan="1">procedure</td></tr> </tbody></table> Takes a string naming an output file to be created and returns an output port capable of writing characters to a new file by that name. If the file cannot be opened, an error is signalled. If a file with the given name already exists, the effect is unspecified. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6437" class="mark"></a><a name="close-input-port" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6441" align="left" colspan="1"><strong id='bold6439' >close-input-port</strong><em id='it6440' > port</em></td><td id="tc6442" align="right" colspan="1">procedure</td></tr> </tbody></table> <a name="g6446" class="mark"></a><a name="close-output-port" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6450" align="left" colspan="1"><strong id='bold6448' >close-output-port</strong><em id='it6449' > port</em></td><td id="tc6451" align="right" colspan="1">procedure</td></tr> </tbody></table> Closes the file associated with <code id='code6455' ><em id='it6454' >port</em></code>, rendering the <code id='code6457' ><em id='it6456' >port</em></code> incapable of delivering or accepting characters. These routines have no effect if the file has already been closed. The value returned is unspecified.<br/><br/><br/><br/></td></tr> </tbody></table><br/> </div> <!-- Input --> <a name="Input"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.6.2 Input</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Input" class="mark"></a> <strong id='bold6462' > </strong> <br/><br/><br/><br/><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6465" class="mark"></a><a name="read" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6469" align="left" colspan="1"><strong id='bold6467' >read</strong><em id='it6468' > </em></td><td id="tc6470" align="right" colspan="1">library procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6475" align="left" colspan="1"><strong id='bold6473' >read</strong><em id='it6474' > port</em></td><td id="tc6476" align="right" colspan="1">library procedure</td></tr> </tbody></table> <samp id='samp6479' >Read</samp> converts external representations of Scheme objects into the objects themselves. That is, it is a parser for the nonterminal <datum> (see sections <a href="r5rs-10.html#External-representation" class="inbound">External representation</a> and <a href="r5rs-9.html#Pairs-and-lists" class="inbound">Pairs and lists</a>). <samp id='samp6480' >Read</samp> returns the next object parsable from the given input <code id='code6482' ><em id='it6481' >port</em></code>, updating <code id='code6484' ><em id='it6483' >port</em></code> to point to the first character past the end of the external representation of the object.<br/><br/>If an end of file is encountered in the input before any characters are found that can begin an object, then an end of file object is returned. The port remains open, and further attempts to read will also return an end of file object. If an end of file is encountered after the beginning of an object's external representation, but the external representation is incomplete and therefore not parsable, an error is signalled.<br/><br/>The <code id='code6488' ><em id='it6487' >port</em></code> argument may be omitted, in which case it defaults to the value returned by <samp id='samp6489' >current-input-port</samp>. It is an error to read from a closed port. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6493" class="mark"></a><a name="read-char" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6497" align="left" colspan="1"><strong id='bold6495' >read-char</strong><em id='it6496' > </em></td><td id="tc6498" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6503" align="left" colspan="1"><strong id='bold6501' >read-char</strong><em id='it6502' > port</em></td><td id="tc6504" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the next character available from the input <code id='code6508' ><em id='it6507' >port</em></code>, updating the <code id='code6510' ><em id='it6509' >port</em></code> to point to the following character. If no more characters are available, an end of file object is returned. <code id='code6512' ><em id='it6511' >Port</em></code> may be omitted, in which case it defaults to the value returned by <samp id='samp6513' >current-input-port</samp>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6517" class="mark"></a><a name="peek-char" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6521" align="left" colspan="1"><strong id='bold6519' >peek-char</strong><em id='it6520' > </em></td><td id="tc6522" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6527" align="left" colspan="1"><strong id='bold6525' >peek-char</strong><em id='it6526' > port</em></td><td id="tc6528" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns the next character available from the input <code id='code6532' ><em id='it6531' >port</em></code>, <em id='emph6533' >without</em> updating the <code id='code6535' ><em id='it6534' >port</em></code> to point to the following character. If no more characters are available, an end of file object is returned. <code id='code6537' ><em id='it6536' >Port</em></code> may be omitted, in which case it defaults to the value returned by <samp id='samp6538' >current-input-port</samp>.<br/><br/><font size="-1"><em id='emph6540' >Note:</em> The value returned by a call to <samp id='samp6541' >peek-char</samp> is the same as the value that would have been returned by a call to <samp id='samp6542' >read-char</samp> with the same <code id='code6544' ><em id='it6543' >port</em></code>. The only difference is that the very next call to <samp id='samp6545' >read-char</samp> or <samp id='samp6546' >peek-char</samp> on that <code id='code6548' ><em id='it6547' >port</em></code> will return the value returned by the preceding call to <samp id='samp6549' >peek-char</samp>. In particular, a call to <samp id='samp6550' >peek-char</samp> on an interactive port will hang waiting for input whenever a call to <samp id='samp6551' >read-char</samp> would have hung. </font></td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6556" class="mark"></a><a name="eof-object?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6560" align="left" colspan="1"><strong id='bold6558' >eof-object?</strong><em id='it6559' > obj</em></td><td id="tc6561" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt6564' >#t</tt> if <code id='code6566' ><em id='it6565' >obj</em></code> is an end of file object, otherwise returns <tt id='tt6567' >#f</tt>. The precise set of end of file objects will vary among implementations, but in any case no end of file object will ever be an object that can be read in using <samp id='samp6568' >read</samp>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6572" class="mark"></a><a name="char-ready?" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6576" align="left" colspan="1"><strong id='bold6574' >char-ready?</strong><em id='it6575' > </em></td><td id="tc6577" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6582" align="left" colspan="1"><strong id='bold6580' >char-ready?</strong><em id='it6581' > port</em></td><td id="tc6583" align="right" colspan="1">procedure</td></tr> </tbody></table> Returns <tt id='tt6586' >#t</tt> if a character is ready on the input <code id='code6588' ><em id='it6587' >port</em></code> and returns <tt id='tt6589' >#f</tt> otherwise. If <samp id='samp6590' >char-ready</samp> returns <tt id='tt6591' >#t</tt> then the next <samp id='samp6592' >read-char</samp> operation on the given <code id='code6594' ><em id='it6593' >port</em></code> is guaranteed not to hang. If the <code id='code6596' ><em id='it6595' >port</em></code> is at end of file then <samp id='samp6597' >char-ready?</samp> returns <tt id='tt6598' >#t</tt>. <code id='code6600' ><em id='it6599' >Port</em></code> may be omitted, in which case it defaults to the value returned by <samp id='samp6601' >current-input-port</samp>.<br/><br/><br/><em id='emph6604' >Rationale:</em> <samp id='samp6605' >Char-ready?</samp> exists to make it possible for a program to accept characters from interactive ports without getting stuck waiting for input. Any input editors associated with such ports must ensure that characters whose existence has been asserted by <samp id='samp6606' >char-ready?</samp> cannot be rubbed out. If <samp id='samp6607' >char-ready?</samp> were to return <tt id='tt6608' >#f</tt> at end of file, a port at end of file would be indistinguishable from an interactive port that has no ready characters. </td></tr> </tbody></table><br/> </div> <!-- Output --> <a name="Output"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.6.3 Output</font> </h3></td></tr></table> </div><div class="subsection"> <a name="Output" class="mark"></a> <br/><br/><br/><br/><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6613" class="mark"></a><a name="write" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6617" align="left" colspan="1"><strong id='bold6615' >write</strong><em id='it6616' > obj</em></td><td id="tc6618" align="right" colspan="1">library procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6623" align="left" colspan="1"><strong id='bold6621' >write</strong><em id='it6622' > obj port</em></td><td id="tc6624" align="right" colspan="1">library procedure</td></tr> </tbody></table> Writes a written representation of <code id='code6628' ><em id='it6627' >obj</em></code> to the given <code id='code6630' ><em id='it6629' >port</em></code>. Strings that appear in the written representation are enclosed in doublequotes, and within those strings backslash and doublequote characters are escaped by backslashes. Character objects are written using the <samp id='samp6631' >#\</samp> notation. <samp id='samp6632' >Write</samp> returns an unspecified value. The <code id='code6634' ><em id='it6633' >port</em></code> argument may be omitted, in which case it defaults to the value returned by <samp id='samp6635' >current-output-port</samp>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6639" class="mark"></a><a name="display" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6643" align="left" colspan="1"><strong id='bold6641' >display</strong><em id='it6642' > obj</em></td><td id="tc6644" align="right" colspan="1">library procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6649" align="left" colspan="1"><strong id='bold6647' >display</strong><em id='it6648' > obj port</em></td><td id="tc6650" align="right" colspan="1">library procedure</td></tr> </tbody></table> Writes a representation of <code id='code6654' ><em id='it6653' >obj</em></code> to the given <code id='code6656' ><em id='it6655' >port</em></code>. Strings that appear in the written representation are not enclosed in doublequotes, and no characters are escaped within those strings. Character objects appear in the representation as if written by <samp id='samp6657' >write-char</samp> instead of by <samp id='samp6658' >write</samp>. <samp id='samp6659' >Display</samp> returns an unspecified value. The <code id='code6661' ><em id='it6660' >port</em></code> argument may be omitted, in which case it defaults to the value returned by <samp id='samp6662' >current-output-port</samp>.<br/><br/><br/><em id='emph6665' >Rationale:</em> <samp id='samp6666' >Write</samp> is intended for producing machine-readable output and <samp id='samp6667' >display</samp> is for producing human-readable output. Implementations that allow ``slashification'' within symbols will probably want <samp id='samp6668' >write</samp> but not <samp id='samp6669' >display</samp> to slashify funny characters in symbols. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6673" class="mark"></a><a name="newline" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6677" align="left" colspan="1"><strong id='bold6675' >newline</strong><em id='it6676' > </em></td><td id="tc6678" align="right" colspan="1">library procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6683" align="left" colspan="1"><strong id='bold6681' >newline</strong><em id='it6682' > port</em></td><td id="tc6684" align="right" colspan="1">library procedure</td></tr> </tbody></table> Writes an end of line to <code id='code6688' ><em id='it6687' >port</em></code>. Exactly how this is done differs from one operating system to another. Returns an unspecified value. The <code id='code6690' ><em id='it6689' >port</em></code> argument may be omitted, in which case it defaults to the value returned by <samp id='samp6691' >current-output-port</samp>. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6695" class="mark"></a><a name="write-char" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6699" align="left" colspan="1"><strong id='bold6697' >write-char</strong><em id='it6698' > char</em></td><td id="tc6700" align="right" colspan="1">procedure</td></tr> </tbody></table> <table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6705" align="left" colspan="1"><strong id='bold6703' >write-char</strong><em id='it6704' > char port</em></td><td id="tc6706" align="right" colspan="1">procedure</td></tr> </tbody></table> Writes the character <code id='code6710' ><em id='it6709' >char</em></code> (not an external representation of the character) to the given <code id='code6712' ><em id='it6711' >port</em></code> and returns an unspecified value. The <code id='code6714' ><em id='it6713' >port</em></code> argument may be omitted, in which case it defaults to the value returned by <samp id='samp6715' >current-output-port</samp>. </td></tr> </tbody></table><br/> </div> <!-- System interface --> <a name="System-interface"></a> <div class="subsection-atitle"><table width="100%"><tr><td bgcolor="#ffffff"><h3><font color="#8381de">6.6.4 System interface</font> </h3></td></tr></table> </div><div class="subsection"> <a name="System-interface" class="mark"></a>Questions of system interface generally fall outside of the domain of this report. However, the following operations are important enough to deserve description here.<br/><br/><table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6720" class="mark"></a><a name="load" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6724" align="left" colspan="1"><strong id='bold6722' >load</strong><em id='it6723' > filename</em></td><td id="tc6725" align="right" colspan="1">optional procedure</td></tr> </tbody></table> <code id='code6729' ><em id='it6728' >Filename</em></code> should be a string naming an existing file containing Scheme source code. The <samp id='samp6730' >load</samp> procedure reads expressions and definitions from the file and evaluates them sequentially. It is unspecified whether the results of the expressions are printed. The <samp id='samp6731' >load</samp> procedure does not affect the values returned by <samp id='samp6732' >current-input-port</samp> and <samp id='samp6733' >current-output-port</samp>. <samp id='samp6734' >Load</samp> returns an unspecified value.<br/><br/><br/><em id='emph6737' >Rationale:</em> For portability, <samp id='samp6738' >load</samp> must operate on source files. Its operation on other kinds of files necessarily varies among implementations. </td></tr> </tbody></table><br/> <table cellspacing="0" class="frame" cellpadding="10" border="1" width="100%"><tbody> <tr><td><a name="g6742" class="mark"></a><a name="transcript-on" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6746" align="left" colspan="1"><strong id='bold6744' >transcript-on</strong><em id='it6745' > filename</em></td><td id="tc6747" align="right" colspan="1">optional procedure</td></tr> </tbody></table> <a name="g6751" class="mark"></a><a name="transcript-off" class="mark"></a><table width="100%" style="border-collapse: collapse;" frame="void" rules="none"><tbody> <tr><td id="tc6755" align="left" colspan="1"><strong id='bold6753' >transcript-off</strong><em id='it6754' > </em></td><td id="tc6756" align="right" colspan="1">optional procedure</td></tr> </tbody></table> <code id='code6760' ><em id='it6759' >Filename</em></code> must be a string naming an output file to be created. The effect of <samp id='samp6761' >transcript-on</samp> is to open the named file for output, and to cause a transcript of subsequent interaction between the user and the Scheme system to be written to the file. The transcript is ended by a call to <samp id='samp6762' >transcript-off</samp>, which closes the transcript file. Only one transcript may be in progress at any time, though some implementations may relax this restriction. The values returned by these procedures are unspecified. </td></tr> </tbody></table><br/> </div> </div><br> </div></td> </tr></table><div class="skribe-ending"> <hr> <p class="ending" id='paragraph8140' ><font size="-1"> This <span class="sc">Html</span> page has been produced by <a href="http://www.inria.fr/mimosa/fp/Skribe" class="http">Skribe</a>. <br/> Last update <em id='it8138' >Tue Jun 2 11:43:24 2009</em>.</font></p></div> </body> </html>