manual-Z-H-7.html 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750
  1. <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!--
  4. Generated from manual.tex by tex2page, v 2005-03-30
  5. (running on MzScheme 299.101, unix),
  6. (c) Dorai Sitaram,
  7. http://www.ccs.neu.edu/~dorai/tex2page/tex2page-doc.html
  8. -->
  9. <head>
  10. <title>
  11. The Incomplete Scheme 48 Reference Manual for release 1.3
  12. </title>
  13. <link rel="stylesheet" type="text/css" href="manual-Z-S.css" title=default>
  14. <meta name=robots content="noindex,follow">
  15. </head>
  16. <body>
  17. <div id=content>
  18. <div align=right class=navigation><i>[Go to <span><a href="manual.html">first</a>, <a href="manual-Z-H-6.html">previous</a></span><span>, <a href="manual-Z-H-8.html">next</a></span> page<span>; &nbsp;&nbsp;</span><span><a href="manual-Z-H-2.html#node_toc_start">contents</a></span><span><span>; &nbsp;&nbsp;</span><a href="manual-Z-H-13.html#node_index_start">index</a></span>]</i></div>
  19. <p></p>
  20. <a name="node_chap_5"></a>
  21. <h1 class=chapter>
  22. <div class=chapterheading><a href="manual-Z-H-2.html#node_toc_node_chap_5">Chapter 5</a></div><br>
  23. <a href="manual-Z-H-2.html#node_toc_node_chap_5">Libraries</a></h1>
  24. <p>Use the
  25. <tt>,open</tt> command (section&nbsp;<a href="manual-Z-H-5.html#node_sec_3.4">3.4</a>)
  26. or
  27. the module language (chapter&nbsp;<a href="manual-Z-H-4.html#node_sec_2.6">2.6</a>)
  28. to open the structures described below.</p>
  29. <p>
  30. </p>
  31. <a name="node_sec_5.1"></a>
  32. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.1">5.1&nbsp;&nbsp;General utilities</a></h2>
  33. <p></p>
  34. <p>
  35. </p>
  36. <p>
  37. These are in the <tt>big-util</tt> structure.</p>
  38. <p>
  39. </p>
  40. <ul>
  41. <li><p><tt>(atom?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_14"></a>
  42. </p>
  43. </ul><p>
  44. <tt>(atom? <i>x</i>)</tt> is the same as <tt>(not (pair? <i>x</i>))</tt>.</p>
  45. <p>
  46. </p>
  47. <ul>
  48. <li><p><tt>(null-list?<i> list</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_16"></a>
  49. </p>
  50. </ul><p>
  51. Returns true for the empty list, false for a pair, and signals an
  52. error otherwise.</p>
  53. <p>
  54. </p>
  55. <ul>
  56. <li><p><tt>(neq?<i> value value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_18"></a>
  57. </p>
  58. </ul><p>
  59. <tt>(neq? <i>x</i> <i>y</i>)</tt> is the same as <tt>(not (eq? <i>x</i>
  60. <i>y</i>))</tt>.</p>
  61. <p>
  62. </p>
  63. <ul>
  64. <li><p><tt>(n=<i> number number</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_20"></a>
  65. </p>
  66. </ul><p>
  67. <tt>(n= <i>x</i> <i>y</i>)</tt> is the same as <tt>(not (= <i>x</i>
  68. <i>y</i>))</tt>.</p>
  69. <p>
  70. </p>
  71. <ul>
  72. <li><p><tt>(identity<i> value</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_22"></a>
  73. </p>
  74. <li><p><tt>(no-op<i> value</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_24"></a>
  75. </p>
  76. </ul><p>
  77. These both just return their argument. <tt>No-op</tt> is guaranteed not to
  78. be compiled in-line, <tt>identity</tt> may be.</p>
  79. <p>
  80. </p>
  81. <ul>
  82. <li><p><tt>(memq?<i> value list</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_26"></a>
  83. </p>
  84. </ul><p>
  85. Returns true if <i>value</i> is in <i>list</i>, false otherwise.</p>
  86. <p>
  87. </p>
  88. <ul>
  89. <li><p><tt>(any?<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_28"></a>
  90. </p>
  91. </ul><p>
  92. Returns true if <i>predicate</i> is true for any element of <i>list</i>.</p>
  93. <p>
  94. </p>
  95. <ul>
  96. <li><p><tt>(every?<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_30"></a>
  97. </p>
  98. </ul><p>
  99. Returns true if <i>predicate</i> is true for every element of <i>list</i>.</p>
  100. <p>
  101. </p>
  102. <ul>
  103. <li><p><tt>(any<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_32"></a>
  104. </p>
  105. <li><p><tt>(first<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_34"></a>
  106. </p>
  107. </ul><p>
  108. <tt>Any</tt> returns some element of <i>list</i> for which <i>predicate</i> is true, or
  109. false if there are none. <tt>First</tt> does the same except that it returns
  110. the first element for which <i>predicate</i> is true.</p>
  111. <p>
  112. </p>
  113. <ul>
  114. <li><p><tt>(filter<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_36"></a>
  115. </p>
  116. <li><p><tt>(filter!<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_38"></a>
  117. </p>
  118. </ul><p>
  119. Returns a list containing all of the elements of <i>list</i> for which
  120. <i>predicate</i> is true. The order of the elements is preserved.
  121. <tt>Filter!</tt> may reuse the storage of <i>list</i>.</p>
  122. <p>
  123. </p>
  124. <ul>
  125. <li><p><tt>(filter-map<i> procedure list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_40"></a>
  126. </p>
  127. </ul><p>
  128. The same as <tt>filter</tt> except the returned list contains the results of
  129. applying <i>procedure</i> instead of elements of <i>list</i>. <tt>(filter-map <i>p</i>
  130. <i>l</i>)</tt> is the same as <tt>(filter identity (map <i>p</i> <i>l</i>))</tt>.</p>
  131. <p>
  132. </p>
  133. <ul>
  134. <li><p><tt>(partition-list<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>list list</i></tt><a name="node_idx_42"></a>
  135. </p>
  136. <li><p><tt>(partition-list!<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>list list</i></tt><a name="node_idx_44"></a>
  137. </p>
  138. </ul><p>
  139. The first return value contains those elements <i>list</i> for which
  140. <i>predicate</i> is true, the second contains the remaining elements.
  141. The order of the elements is preserved. <tt>Partition-list!</tt> may
  142. reuse the storage of the <i>list</i>.</p>
  143. <p>
  144. </p>
  145. <ul>
  146. <li><p><tt>(remove-duplicates<i> list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_46"></a>
  147. </p>
  148. </ul><p>
  149. Returns its argument with all duplicate elements removed. The first
  150. instance of each element is preserved.</p>
  151. <p>
  152. </p>
  153. <ul>
  154. <li><p><tt>(delq<i> value list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_48"></a>
  155. </p>
  156. <li><p><tt>(delq!<i> value list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_50"></a>
  157. </p>
  158. <li><p><tt>(delete<i> predicate list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_52"></a>
  159. </p>
  160. </ul><p>
  161. All three of these return <i>list</i> with some elements removed.
  162. <tt>Delq</tt> removes all elements <tt>eq?</tt> to <i>value</i>. <tt>Delq!</tt>
  163. does the same and may modify the list argument. <tt>Delete</tt> removes
  164. all elements for which <i>predicate</i> is true. Both <tt>delq</tt> and
  165. <tt>delete</tt> may reuse some of the storage in the list argument, but
  166. won't modify it.</p>
  167. <p>
  168. </p>
  169. <ul>
  170. <li><p><tt>(reverse!<i> list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_54"></a>
  171. </p>
  172. </ul><p>
  173. Destructively reverses <i>list</i>.</p>
  174. <p>
  175. </p>
  176. <ul>
  177. <li><p><tt>(concatenate-symbol<i> value <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>symbol</i></tt><a name="node_idx_56"></a>
  178. </p>
  179. </ul><p>
  180. Returns the symbol whose name is produced by concatenating the
  181. <tt>display</tt>ed
  182. representations of <i>value</i>&nbsp;<tt>...</tt>.</p>
  183. <p>
  184. </p>
  185. <pre class=verbatim>(concatenate-symbol 'abc &quot;-&quot; 4) ===&gt; 'abc-4
  186. </pre><p></p>
  187. <p>
  188. </p>
  189. <a name="node_sec_5.2"></a>
  190. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.2">5.2&nbsp;&nbsp;Pretty-printing</a></h2>
  191. <p>These are in the <tt>pp</tt> structure.</p>
  192. <p>
  193. </p>
  194. <ul>
  195. <li><p><tt>(p<i> value</i>)</tt><a name="node_idx_58"></a>
  196. </p>
  197. <li><p><tt>(p<i> value output-port</i>)</tt><a name="node_idx_60"></a>
  198. </p>
  199. <li><p><tt>(pretty-print<i> value output-port position</i>)</tt><a name="node_idx_62"></a>
  200. </p>
  201. </ul><p>
  202. Pretty-print <i>value</i> The current output port is used if no port is
  203. specified. <i>Position</i> is the starting offset. <i>Value</i> will be
  204. pretty-printed to the right of this column.</p>
  205. <p>
  206. </p>
  207. <a name="node_sec_5.3"></a>
  208. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.3">5.3&nbsp;&nbsp;ASCII character encoding</a></h2>
  209. <p></p>
  210. <p>
  211. These are in the structure <tt>ascii</tt>.</p>
  212. <p>
  213. </p>
  214. <ul>
  215. <li><p><tt>(char-&gt;ascii<i> char</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_64"></a>
  216. </p>
  217. <li><p><tt>(ascii-&gt;char<i> integer</i>)&nbsp;-&gt;&nbsp;<i>char</i></tt><a name="node_idx_66"></a>
  218. </p>
  219. </ul><p>
  220. These are identical to <tt>char-&gt;integer</tt> and <tt>integer-&gt;char</tt> except that
  221. they use the
  222. ASCII encoding (appendix&nbsp;<a href="manual-Z-H-11.html#node_chap_A">A</a>).</p>
  223. <p>
  224. </p>
  225. <ul>
  226. <li><p><tt>ascii-limit</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(integer)<a name="node_idx_68"></a>
  227. </p>
  228. <li><p><tt>ascii-whitespaces</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(list of integers)<a name="node_idx_70"></a>
  229. </p>
  230. </ul><p>
  231. <tt>Ascii-limit</tt> is one more than the largest value that <tt>char-&gt;ascii</tt>
  232. may return.
  233. <tt>Ascii-whitespaces</tt> is a list of the ASCII values of whitespace characters
  234. (space, horizontal tab, line feed (= newline), vertical tab, form feed, and
  235. carriage return).</p>
  236. <p>
  237. </p>
  238. <a name="node_sec_5.4"></a>
  239. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.4">5.4&nbsp;&nbsp;Bitwise integer operations</a></h2>
  240. <p>These functions use the two's-complement representation for integers.
  241. There is no limit to the number of bits in an integer.
  242. They are in the structures <tt>bitwise</tt> and <tt>big-scheme</tt>.</p>
  243. <p>
  244. </p>
  245. <ul>
  246. <li><p><tt>(bitwise-and<i> integer integer</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_72"></a>
  247. </p>
  248. <li><p><tt>(bitwise-ior<i> integer integer</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_74"></a>
  249. </p>
  250. <li><p><tt>(bitwise-xor<i> integer integer</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_76"></a>
  251. </p>
  252. <li><p><tt>(bitwise-not<i> integer</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_78"></a>
  253. </p>
  254. </ul><p>
  255. These perform various logical operations on integers on a bit-by-bit
  256. basis. `<tt>ior</tt>' is inclusive OR and `<tt>xor</tt>' is exclusive OR.</p>
  257. <p>
  258. </p>
  259. <ul>
  260. <li><p><tt>(arithmetic-shift<i> integer bit-count</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_80"></a>
  261. </p>
  262. </ul><p>
  263. Shifts the integer by the given bit count, which must be an integer,
  264. shifting left for positive counts and right for negative ones.
  265. Shifting preserves the integer's sign.</p>
  266. <p>
  267. </p>
  268. <ul>
  269. <li><p><tt>(bit-count<i> integer</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_82"></a>
  270. </p>
  271. </ul><p>
  272. Counts the number of bits set in the integer.
  273. If the argument is negative a bitwise NOT operation is performed
  274. before counting.</p>
  275. <p>
  276. </p>
  277. <a name="node_sec_5.5"></a>
  278. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.5">5.5&nbsp;&nbsp;Byte vectors</a></h2>
  279. <p>These are homogeneous vectors of small integers (0 <u>&lt;</u> <em>i</em> <u>&lt;</u> 255).
  280. The functions that operate on them are analogous to those for vectors.
  281. They are in the structure <tt>byte-vectors</tt>.</p>
  282. <p>
  283. </p>
  284. <ul>
  285. <li><p><tt>(byte-vector?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_84"></a>
  286. </p>
  287. <li><p><tt>(make-byte-vector<i> k fill</i>)&nbsp;-&gt;&nbsp;<i>byte-vector</i></tt><a name="node_idx_86"></a>
  288. </p>
  289. <li><p><tt>(byte-vector<i> b <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>byte-vector</i></tt><a name="node_idx_88"></a>
  290. </p>
  291. <li><p><tt>(byte-vector-length<i> byte-vector</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_90"></a>
  292. </p>
  293. <li><p><tt>(byte-vector-ref<i> byte-vector k</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_92"></a>
  294. </p>
  295. <li><p><tt>(byte-vector-set!<i> byte-vector k b</i>)</tt><a name="node_idx_94"></a>
  296. </p>
  297. </ul><p></p>
  298. <p>
  299. </p>
  300. <a name="node_sec_5.6"></a>
  301. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.6">5.6&nbsp;&nbsp;Sparse vectors</a></h2>
  302. <p>These are vectors that grow as large as they need to. That is, they
  303. can be indexed by arbitrarily large nonnegative integers. The
  304. implementation allows for arbitrarily large gaps by arranging the
  305. entries in a tree. They are in the structure <tt>sparse-vectors</tt>.</p>
  306. <p>
  307. </p>
  308. <ul>
  309. <li><p><tt>(make-sparse-vector<i></i>)&nbsp;-&gt;&nbsp;<i>sparse-vector</i></tt><a name="node_idx_96"></a>
  310. </p>
  311. <li><p><tt>(sparse-vector-ref<i> sparse-vector k</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_98"></a>
  312. </p>
  313. <li><p><tt>(sparse-vector-set!<i> sparse-vector k value</i>)</tt><a name="node_idx_100"></a>
  314. </p>
  315. <li><p><tt>(sparse-vector-&gt;list<i> sparse-vector</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_102"></a>
  316. </p>
  317. </ul><p>
  318. <tt>Make-sparse-vector</tt>, <tt>sparse-vector-ref</tt>, and
  319. <tt>sparse-vector-set!</tt> are analogous to <tt>make-vector</tt>,
  320. <tt>vector-ref</tt>, and <tt>vector-set!</tt>, except that the indices
  321. passed to <tt>sparse-vector-ref</tt> and <tt>sparse-vector-set!</tt> can
  322. be arbitrarily large. For indices whose elements have not been set in
  323. a sparse vector, <tt>sparse-vector-ref</tt> returns <tt>#f</tt>.</p>
  324. <p>
  325. <tt>Sparse-vector-&gt;list</tt> is for debugging: It returns a list of the
  326. consecutive elements in a sparse vector from 0 to the highest element
  327. that has been set. Note that the list will also include all the
  328. <tt>#f</tt> elements for the unset elements.</p>
  329. <p>
  330. </p>
  331. <a name="node_sec_5.7"></a>
  332. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.7">5.7&nbsp;&nbsp;Cells</a></h2>
  333. <p></p>
  334. <p>
  335. These hold a single value and are useful when a simple indirection is
  336. required.
  337. The system uses these to hold the values of lexical variables that
  338. may be <tt>set!</tt>.</p>
  339. <p>
  340. </p>
  341. <ul>
  342. <li><p><tt>(cell?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_104"></a>
  343. </p>
  344. <li><p><tt>(make-cell<i> value</i>)&nbsp;-&gt;&nbsp;<i>cell</i></tt><a name="node_idx_106"></a>
  345. </p>
  346. <li><p><tt>(cell-ref<i> cell</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_108"></a>
  347. </p>
  348. <li><p><tt>(cell-set!<i> cell value</i>)</tt><a name="node_idx_110"></a>
  349. </p>
  350. </ul><p></p>
  351. <p>
  352. </p>
  353. <a name="node_sec_5.8"></a>
  354. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.8">5.8&nbsp;&nbsp;Queues</a></h2>
  355. <p>These are ordinary first-in, first-out queues.
  356. The procedures are in structure <tt>queues</tt>.</p>
  357. <p>
  358. </p>
  359. <ul>
  360. <li><p><tt>(make-queue<i></i>)&nbsp;-&gt;&nbsp;<i>queue</i></tt><a name="node_idx_112"></a>
  361. </p>
  362. <li><p><tt>(queue?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_114"></a>
  363. </p>
  364. <li><p><tt>(queue-empty?<i> queue</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_116"></a>
  365. </p>
  366. <li><p><tt>(enqueue!<i> queue value</i>)</tt><a name="node_idx_118"></a>
  367. </p>
  368. <li><p><tt>(dequeue!<i> queue</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_120"></a>
  369. </p>
  370. </ul><p>
  371. <tt>Make-queue</tt> creates an empty queue, <tt>queue?</tt> is a predicate for
  372. identifying queues, <tt>queue-empty?</tt> tells you if a queue is empty,
  373. <tt>enqueue!</tt> and <tt>dequeue!</tt> add and remove values.</p>
  374. <p>
  375. </p>
  376. <ul>
  377. <li><p><tt>(queue-length<i> queue</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_122"></a>
  378. </p>
  379. <li><p><tt>(queue-&gt;list<i> queue</i>)&nbsp;-&gt;&nbsp;<i>values</i></tt><a name="node_idx_124"></a>
  380. </p>
  381. <li><p><tt>(list-&gt;queue<i> values</i>)&nbsp;-&gt;&nbsp;<i>queue</i></tt><a name="node_idx_126"></a>
  382. </p>
  383. <li><p><tt>(delete-from-queue!<i> queue value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_128"></a>
  384. </p>
  385. </ul><p>
  386. <tt>Queue-length</tt> returns the number of values in <i>queue</i>.
  387. <tt>Queue-&gt;list</tt> returns the values in <i>queue</i> as a list, in the
  388. order in which the values were added.
  389. <tt>List-&gt;queue</tt> returns a queue containing <i>values</i>, preserving
  390. their order.
  391. <tt>Delete-from-queue</tt> removes the first instance of <i>value</i> from
  392. <tt>queue</tt>, using <tt>eq?</tt> for comparisons.
  393. <tt>Delete-from-queue</tt> returns <tt>#t</tt> if <i>value</i> is found and
  394. <tt>#f</tt> if it is not.</p>
  395. <p>
  396. </p>
  397. <a name="node_sec_5.9"></a>
  398. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.9">5.9&nbsp;&nbsp;Arrays</a></h2>
  399. <p>These provide N-dimensional, zero-based arrays and
  400. are in the structure <tt>arrays</tt>.
  401. The array interface is derived from one invented by Alan Bawden.</p>
  402. <p>
  403. </p>
  404. <ul>
  405. <li><p><tt>(make-array<i> value dimension<sub>0</sub> <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>array</i></tt><a name="node_idx_130"></a>
  406. </p>
  407. <li><p><tt>(array<i> dimensions element<sub>0</sub> <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>array</i></tt><a name="node_idx_132"></a>
  408. </p>
  409. <li><p><tt>(copy-array<i> array</i>)&nbsp;-&gt;&nbsp;<i>array</i></tt><a name="node_idx_134"></a>
  410. </p>
  411. </ul><p>
  412. <tt>Make-array</tt> makes a new array with the given dimensions, each of which
  413. must be a non-negative integer.
  414. Every element is initially set to <i>value</i>.
  415. <tt>Array</tt> Returns a new array with the given dimensions and elements.
  416. <i>Dimensions</i> must be a list of non-negative integers,
  417. The number of elements should be the equal to the product of the
  418. dimensions.
  419. The elements are stored in row-major order.
  420. </p>
  421. <pre class=verbatim>(make-array 'a 2 3) $${Array 2 3}
  422. (array '(2 3) 'a 'b 'c 'd 'e 'f)
  423. $${Array 2 3}
  424. </pre><p></p>
  425. <p>
  426. <tt>Copy-array</tt> returns a copy of <i>array</i>.
  427. The copy is identical to the <i>array</i> but does not share storage with it.</p>
  428. <p>
  429. </p>
  430. <ul>
  431. <li><p><tt>(array?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_136"></a>
  432. </p>
  433. </ul><p>
  434. Returns <tt>#t</tt> if <i>value</i> is an array.</p>
  435. <p>
  436. </p>
  437. <ul>
  438. <li><p><tt>(array-ref<i> array index<sub>0</sub> <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_138"></a>
  439. </p>
  440. <li><p><tt>(array-set!<i> array value index<sub>0</sub> <tt>...</tt></i>)</tt><a name="node_idx_140"></a>
  441. </p>
  442. <li><p><tt>(array-&gt;vector<i> array</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_142"></a>
  443. </p>
  444. <li><p><tt>(array-dimensions<i> array</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_144"></a>
  445. </p>
  446. </ul><p>
  447. <tt>Array-ref</tt> returns the specified array element and <tt>array-set!</tt>
  448. replaces the element with <i>value</i>.
  449. </p>
  450. <pre class=verbatim>(let ((a (array '(2 3) 'a 'b 'c 'd 'e 'f)))
  451. (let ((x (array-ref a 0 1)))
  452. (array-set! a 'g 0 1)
  453. (list x (array-ref a 0 1))))
  454. $$'(b g)
  455. </pre><p></p>
  456. <p>
  457. <tt>Array-&gt;vector</tt> returns a vector containing the elements of <i>array</i>
  458. in row-major order.
  459. <tt>Array-dimensions</tt> returns the dimensions of
  460. the array as a list.</p>
  461. <p>
  462. </p>
  463. <ul>
  464. <li><p><tt>(make-shared-array<i> array linear-map dimension<sub>0</sub> <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>array</i></tt><a name="node_idx_146"></a>
  465. </p>
  466. </ul><p>
  467. <tt>Make-shared-array</tt> makes a new array that shares storage with <i>array</i>
  468. and uses <i>linear-map</i> to map indexes to elements.
  469. <i>Linear-map</i> must accept as many arguments as the number of
  470. <i>dimension</i>s given and must return a list of non-negative integers
  471. that are valid indexes into <i>array</i>.
  472. &lt;</p>
  473. <pre class=verbatim>(array-ref (make-shared-array a f i0 i1 ...)
  474. j0 j1 ...)
  475. </pre><p>
  476. is equivalent to
  477. </p>
  478. <pre class=verbatim>(apply array-ref a (f j0 j1 ...))
  479. </pre><p></p>
  480. <p>
  481. As an example, the following function makes the transpose of a two-dimensional
  482. array:
  483. </p>
  484. <pre class=verbatim>(define (transpose array)
  485. (let ((dimensions (array-dimensions array)))
  486. (make-shared-array array
  487. (lambda (x y)
  488. (list y x))
  489. (cadr dimensions)
  490. (car dimensions))))
  491. (array-&gt;vector
  492. (transpose
  493. (array '(2 3) 'a 'b 'c 'd 'e 'f)))
  494. $$'(a d b e c f)
  495. </pre><p></p>
  496. <p>
  497. </p>
  498. <a name="node_sec_5.10"></a>
  499. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.10">5.10&nbsp;&nbsp;Records</a></h2>
  500. <p></p>
  501. <p>
  502. New types can be constructed using the <tt>define-record-type</tt> macro
  503. from the <tt>define-record-types</tt> structure
  504. The general syntax is:
  505. </p>
  506. <pre class=verbatim>(define-record-type <i>tag</i> <i>type-name</i>
  507. (<i>constructor-name</i> <i>field-tag</i> <tt>...</tt>)
  508. <i>predicate-name</i>
  509. (<i>field-tag</i> <i>accessor-name</i> [<i>modifier-name</i>])
  510. <tt>...</tt>)
  511. </pre><p>
  512. This makes the following definitions:
  513. </p>
  514. <ul>
  515. <li><p><tt><i>type-name</i></tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(type)
  516. </p>
  517. <li><p><tt>(<i>constructor-name</i><i> field-init <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>type-name</i></tt>
  518. </p>
  519. <li><p><tt>(<i>predicate-name</i><i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt>
  520. </p>
  521. <li><p><tt>(<i>accessor-name</i><i> type-name</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt>
  522. </p>
  523. <li><p><tt>(<i>modifier-name</i><i> type-name value</i>)</tt>
  524. </p>
  525. </ul><p>
  526. <i>Type-name</i> is the record type itself, and can be used to
  527. specify a print method (see below).
  528. <i>Constructor-name</i> is a constructor that accepts values
  529. for the fields whose tags are specified.
  530. <i>Predicate-name</i> is a predicate that returns <tt>#t</tt> for
  531. elements of the type and <tt>#f</tt> for everything else.
  532. The <i>accessor-name</i>s retrieve the values of fields,
  533. and the <i>modifier-name</i>'s update them.
  534. <i>Tag</i> is used in printing instances of the record type and
  535. the <i>field-tag</i>s are used in the inspector and to match
  536. constructor arguments with fields.</p>
  537. <p>
  538. </p>
  539. <ul>
  540. <li><p><tt>(define-record-discloser<i> type discloser</i>)</tt><a name="node_idx_148"></a>
  541. </p>
  542. </ul><p>
  543. <tt>Define-record-discloser</tt> determines how
  544. records of type <i>type</i> are printed.
  545. <i>Discloser</i> should be procedure which takes a single
  546. record of type <i>type</i> and returns a list whose car is
  547. a symbol.
  548. The record will be printed as the value returned by <i>discloser</i>
  549. with curly braces used instead of the usual parenthesis.</p>
  550. <p>
  551. For example
  552. </p>
  553. <pre class=verbatim>(define-record-type pare :pare
  554. (kons x y)
  555. pare?
  556. (x kar set-kar!)
  557. (y kdr))
  558. </pre><p>
  559. defines <tt>kons</tt> to be a constructor, <tt>kar</tt> and <tt>kdr</tt> to be
  560. accessors, <tt>set-kar!</tt> to be a modifier, and <tt>pare?</tt> to be a predicate
  561. for a new type of object.
  562. The type itself is named <tt>:pare</tt>.
  563. <tt>Pare</tt> is a tag used in printing the new objects.</p>
  564. <p>
  565. By default, the new objects print as <tt>#{Pare}</tt>.
  566. The print method can be modified using <tt>define-record-discloser</tt>:
  567. </p>
  568. <pre class=verbatim>(define-record-discloser :pare
  569. (lambda (p) `(pare ,(kar p) ,(kdr p))))
  570. </pre><p>
  571. will cause the result of <tt>(kons 1 2)</tt> to print as
  572. <tt>#{Pare 1 2}</tt>.</p>
  573. <p>
  574. <tt>Define-record-resumer</tt> (section&nbsp;<a href="manual-Z-H-9.html#node_sec_7.8.3">7.8.3</a>)
  575. can be used to control how records are stored in heap images.</p>
  576. <p>
  577. </p>
  578. <a name="node_sec_5.10.1"></a>
  579. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.10.1">5.10.1&nbsp;&nbsp;Low-level access to records</a></h3>
  580. <p>Records are implemented using primitive objects exactly analogous
  581. to vectors.
  582. Every record has a record type (which is another record) in the first slot.
  583. Note that use of these procedures, especially <tt>record-set!</tt>, breaks
  584. the record abstraction described above; caution is advised.</p>
  585. <p>
  586. These procedures are in the structure <tt>records</tt>.</p>
  587. <p>
  588. </p>
  589. <ul>
  590. <li><p><tt>(make-record<i> n value</i>)&nbsp;-&gt;&nbsp;<i>record</i></tt><a name="node_idx_150"></a>
  591. </p>
  592. <li><p><tt>(record<i> value <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>record-vector</i></tt><a name="node_idx_152"></a>
  593. </p>
  594. <li><p><tt>(record?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_154"></a>
  595. </p>
  596. <li><p><tt>(record-length<i> record</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_156"></a>
  597. </p>
  598. <li><p><tt>(record-type<i> record</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_158"></a>
  599. </p>
  600. <li><p><tt>(record-ref<i> record i</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_160"></a>
  601. </p>
  602. <li><p><tt>(record-set!<i> record i value</i>)</tt><a name="node_idx_162"></a>
  603. </p>
  604. </ul><p>
  605. These the same as the standard <tt>vector-</tt> procedures except that they
  606. operate on records.
  607. The value returned by <tt>record-length</tt> includes the slot holding the
  608. record's type.
  609. <tt>(record-type <i>x</i>)</tt> is equivalent to <tt>(record-ref <i>x</i> 0)</tt>.</p>
  610. <p>
  611. </p>
  612. <a name="node_sec_5.10.2"></a>
  613. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.10.2">5.10.2&nbsp;&nbsp;Record types</a></h3>
  614. <p>Record types are themselves records of a particular type (the first slot
  615. of <tt>:record-type</tt> points to itself).
  616. A record type contains four values: the name of the record type, a list of
  617. the names its fields, and procedures for disclosing and resuming records
  618. of that type.
  619. Procedures for manipulating them are in the structure <tt>record-types</tt>.</p>
  620. <p>
  621. </p>
  622. <ul>
  623. <li><p><tt>(make-record-type<i> name field-names</i>)&nbsp;-&gt;&nbsp;<i>record-type</i></tt><a name="node_idx_164"></a>
  624. </p>
  625. <li><p><tt>(record-type?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_166"></a>
  626. </p>
  627. <li><p><tt>(record-type-name<i> record-type</i>)&nbsp;-&gt;&nbsp;<i>symbol</i></tt><a name="node_idx_168"></a>
  628. </p>
  629. <li><p><tt>(record-type-field-names<i> record-type</i>)&nbsp;-&gt;&nbsp;<i>symbols</i></tt><a name="node_idx_170"></a>
  630. </p>
  631. </ul><p>
  632. </p>
  633. <p>
  634. </p>
  635. <ul>
  636. <li><p><tt>(record-constructor<i> record-type field-names</i>)&nbsp;-&gt;&nbsp;<i>procedure</i></tt><a name="node_idx_172"></a>
  637. </p>
  638. <li><p><tt>(record-predicate<i> record-type</i>)&nbsp;-&gt;&nbsp;<i>procedure</i></tt><a name="node_idx_174"></a>
  639. </p>
  640. <li><p><tt>(record-accessor<i> record-type field-name</i>)&nbsp;-&gt;&nbsp;<i>procedure</i></tt><a name="node_idx_176"></a>
  641. </p>
  642. <li><p><tt>(record-modifier<i> record-type field-name</i>)&nbsp;-&gt;&nbsp;<i>procedure</i></tt><a name="node_idx_178"></a>
  643. </p>
  644. </ul><p>
  645. These procedures construct the usual record-manipulating procedures.
  646. <tt>Record-constructor</tt> returns a constructor that is passed the initial
  647. values for the fields specified and returns a new record.
  648. <tt>Record-predicate</tt> returns a predicate that return true when passed
  649. a record of type <i>record-type</i> and false otherwise.
  650. <tt>Record-accessor</tt> and <tt>record-modifier</tt> return procedures that
  651. reference and set the given field in records of the approriate type.</p>
  652. <p>
  653. </p>
  654. <ul>
  655. <li><p><tt>(define-record-discloser<i> record-type discloser</i>)</tt><a name="node_idx_180"></a>
  656. </p>
  657. <li><p><tt>(define-record-resumer<i> record-type resumer</i>)</tt><a name="node_idx_182"></a>
  658. </p>
  659. </ul><p>
  660. <tt>Record-types</tt> is the initial exporter of
  661. <tt>define-record-discloser</tt>
  662. (re-exported by <tt>define-record-types</tt> described above)
  663. and
  664. <tt>define-record-resumer</tt>
  665. (re-exported by
  666. <tt>external-calls</tt> (section&nbsp;<a href="manual-Z-H-9.html#node_sec_7.8.3">7.8.3</a>)).</p>
  667. <p>
  668. The procedures described in this section can be used to define new
  669. record-type-defining macros.
  670. </p>
  671. <pre class=verbatim>(define-record-type pare :pare
  672. (kons x y)
  673. pare?
  674. (x kar set-kar!)
  675. (y kdr))
  676. </pre><p>
  677. is (sematically) equivalent to
  678. </p>
  679. <pre class=verbatim>(define :pare (make-record-type 'pare '(x y)))
  680. (define kons (record-constructor :pare '(x y)))
  681. (define kar (record-accessor :pare 'x))
  682. (define set-kar! (record-modifier :pare 'x))
  683. (define kdr (record-accessor :pare 'y))
  684. </pre><p></p>
  685. <p>
  686. The ``(semantically)'' above is because <tt>define-record-type</tt> adds
  687. declarations, which allows the type checker to detect some misuses of records,
  688. and uses more efficient definitions for the constructor, accessors, and
  689. modifiers.
  690. Ignoring the declarations, which will have to wait for another edition of
  691. the manual, what the above example actually expands into is:
  692. </p>
  693. <pre class=verbatim>(define :pare (make-record-type 'pare '(x y)))
  694. (define (kons x y) (record :pare x y))
  695. (define (kar r) (checked-record-ref r :pare 1))
  696. (define (set-kar! r new)
  697. (checked-record-set! r :pare 1 new))
  698. (define (kdr r) (checked-record-ref r :pare 2))
  699. </pre><p>
  700. <tt>Checked-record-ref</tt> and <tt>Checked-record-set!</tt> are
  701. low-level procedures that check the type of the
  702. record and access or modify it using a single VM instruction.</p>
  703. <p>
  704. </p>
  705. <a name="node_sec_5.11"></a>
  706. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.11">5.11&nbsp;&nbsp;Finite record types</a></h2>
  707. <p>The structure <tt>finite-types</tt> has
  708. two macros for defining `finite' record types.
  709. These are record types for which there are a fixed number of instances,
  710. all of which are created at the same time as the record type itself.
  711. The syntax for defining an enumerated type is:
  712. </p>
  713. <pre class=verbatim>(define-enumerated-type <i>tag</i> <i>type-name</i>
  714. <i>predicate-name</i>
  715. <i>vector-of-instances-name</i>
  716. <i>name-accessor</i>
  717. <i>index-accessor</i>
  718. (<i>instance-name</i> <tt>...</tt>))
  719. </pre><p>
  720. This defines a new record type, bound to <i>type-name</i>, with as many
  721. instances as there are <i>instance-name</i>'s.
  722. <i>Vector-of-instances-name</i> is bound to a vector containing the instances
  723. of the type in the same order as the <i>instance-name</i> list.
  724. <i>Tag</i> is bound to a macro that when given an <i>instance-name</i> expands
  725. into an expression that returns corresponding instance.
  726. The name lookup is done at macro expansion time.
  727. <i>Predicate-name</i> is a predicate for the new type.
  728. <i>Name-accessor</i> and <i>index-accessor</i> are accessors for the
  729. name and index (in <i>vector-of-instances</i>) of instances of the type.</p>
  730. <p>
  731. </p>
  732. <pre class=verbatim>(define-enumerated-type color :color
  733. color?
  734. colors
  735. color-name
  736. color-index
  737. (black white purple maroon))
  738. (color-name (vector-ref colors 0)) $$black
  739. (color-name (color white)) $$white
  740. (color-index (color purple)) $$2
  741. </pre><p></p>
  742. <p>
  743. Finite types are enumerations that allow the user to add additional
  744. fields in the type.
  745. The syntax for defining a finite type is:
  746. </p>
  747. <pre class=verbatim>(define-finite-type <i>tag</i> <i>type-name</i>
  748. (<i>field-tag</i> <tt>...</tt>)
  749. <i>predicate-name</i>
  750. <i>vector-of-instances-name</i>
  751. <i>name-accessor</i>
  752. <i>index-accessor</i>
  753. (<i>field-tag</i> <i>accessor-name</i> [<i>modifier-name</i>])
  754. <tt>...</tt>((<i>instance-name</i> <i>field-value</i> <tt>...</tt>)
  755. <tt>...</tt>))
  756. </pre><p>
  757. The additional fields are specified exactly as with <tt>define-record-type</tt>.
  758. The field arguments to the constructor are listed after the <i>type-name</i>;
  759. these do not include the name and index fields.
  760. The form ends with the names and the initial field values for
  761. the instances of the type.
  762. The instances are constructed by applying the (unnamed) constructor to
  763. these initial field values.
  764. The name must be first and
  765. the remaining values must match the <i>field-tag</i>s in the constructor's
  766. argument list.</p>
  767. <p>
  768. </p>
  769. <p>
  770. </p>
  771. <pre class=verbatim>(define-finite-type color :color
  772. (red green blue)
  773. color?
  774. colors
  775. color-name
  776. color-index
  777. (red color-red)
  778. (green color-green)
  779. (blue color-blue)
  780. ((black 0 0 0)
  781. (white 255 255 255)
  782. (purple 160 32 240)
  783. (maroon 176 48 96)))
  784. (color-name (color black)) $$black
  785. (color-name (vector-ref colors 1)) $$white
  786. (color-index (color purple)) $$2
  787. (color-red (color maroon)) $$176
  788. </pre><p></p>
  789. <p>
  790. </p>
  791. <a name="node_sec_5.12"></a>
  792. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.12">5.12&nbsp;&nbsp;Sets over finite types</a></h2>
  793. <p></p>
  794. <p>
  795. The structure <tt>enum-sets</tt> has a macro for defining types for sets
  796. of elements of finite types. These work naturally with the finite
  797. types defined by the <tt>finite-types</tt> structure, but are not tied
  798. to them. The syntax for defining such a type is:</p>
  799. <p>
  800. </p>
  801. <pre class=verbatim>(define-enum-set-type <i>id</i> <i>type-name</i> <i>predicate</i> <i>constructor</i>
  802. <i>element-syntax</i> <i>element-predicate</i> <i>all-elements</i> <i>element-index-ref</i>)
  803. </pre><p>
  804. This defines <i>id</i> to be syntax for constructing sets,
  805. <i>type-name</i> to be a value representing the type,
  806. <i>predicate</i> to be a predicate for those sets, and
  807. <i>constructor</i> a procedure for constructing one from a list.</p>
  808. <p>
  809. <i>Element-syntax</i> must be the name of a macro for constructing set
  810. elements from names (akin to the <i>tag</i> argument to
  811. <tt>define-enumerated-type</tt>). <i>Element-predicate</i> must be a
  812. predicate for the element type, <i>all-elements</i> a vector of all
  813. values of the element type, and <i>element-index-ref</i> must return
  814. the index of an element within the <i>all-elements</i> vector.</p>
  815. <p>
  816. </p>
  817. <ul>
  818. <li><p><tt>(enum-set-&gt;list<i> enum-set</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_184"></a>
  819. </p>
  820. <li><p><tt>(enum-set-member?<i> enum-set enumerand</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_186"></a>
  821. </p>
  822. <li><p><tt>(enum-set=?<i> enum-set enum-set</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_188"></a>
  823. </p>
  824. <li><p><tt>(enum-set-union<i> enum-set enum-set</i>)&nbsp;-&gt;&nbsp;<i>enum-set</i></tt><a name="node_idx_190"></a>
  825. </p>
  826. <li><p><tt>(enum-set-intersection<i> enum-set enum-set</i>)&nbsp;-&gt;&nbsp;<i> enum-set</i></tt><a name="node_idx_192"></a>
  827. </p>
  828. <li><p><tt>(enum-set-negation<i> enum-set</i>)&nbsp;-&gt;&nbsp;<i>enum-set</i></tt><a name="node_idx_194"></a>
  829. </p>
  830. </ul><p>
  831. <tt>Enum-set-&gt;list</tt> converts a set into a list of its elements.
  832. <tt>Enum-set-member?</tt> tests for membership. <tt>Enum-set=?</tt> tests
  833. two sets of equal type for equality. (If its arguments are not of the
  834. same type, <tt>enum-set=?</tt> raises an exception.)
  835. <tt>Enum-set-union</tt> computes the union of two sets of equal type,
  836. <tt>enum-set-intersection</tt> computes the intersection, and
  837. <tt>enum-set-negation</tt> computes the complement of a set.</p>
  838. <p>
  839. Here is an example. Given an enumerated type:</p>
  840. <p>
  841. </p>
  842. <pre class=verbatim>(define-enumerated-type color :color
  843. color?
  844. colors
  845. color-name
  846. color-index
  847. (red blue green))
  848. </pre><p></p>
  849. <p>
  850. we can define sets of colors:</p>
  851. <p>
  852. </p>
  853. <pre class=verbatim>(define-enum-set-type color-set :color-set
  854. color-set?
  855. make-color-set
  856. color color? colors color-index)
  857. </pre><p></p>
  858. <p>
  859. </p>
  860. <pre class=verbatim>&gt; (enum-set-&gt;list (color-set red blue))
  861. (#Color red #Color blue)
  862. &gt; (enum-set-&gt;list (enum-set-negation (color-set red blue)))
  863. (#Color green)
  864. &gt; (enum-set-member? (color-set red blue) (color blue))
  865. #t
  866. </pre><p></p>
  867. <p>
  868. </p>
  869. <a name="node_sec_5.13"></a>
  870. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.13">5.13&nbsp;&nbsp;Hash tables</a></h2>
  871. <p>These are generic hash tables, and are in the structure <tt>tables</tt>.
  872. Strictly speaking they are more maps than tables, as every table has a
  873. value for every possible key (for that type of table).
  874. All but a finite number of those values are <tt>#f</tt>.</p>
  875. <p>
  876. </p>
  877. <ul>
  878. <li><p><tt>(make-table<i></i>)&nbsp;-&gt;&nbsp;<i>table</i></tt><a name="node_idx_196"></a>
  879. </p>
  880. <li><p><tt>(make-symbol-table<i></i>)&nbsp;-&gt;&nbsp;<i>symbol-table</i></tt><a name="node_idx_198"></a>
  881. </p>
  882. <li><p><tt>(make-string-table<i></i>)&nbsp;-&gt;&nbsp;<i>string-table</i></tt><a name="node_idx_200"></a>
  883. </p>
  884. <li><p><tt>(make-integer-table<i></i>)&nbsp;-&gt;&nbsp;<i>integer-table</i></tt><a name="node_idx_202"></a>
  885. </p>
  886. <li><p><tt>(make-table-maker<i> compare-proc hash-proc</i>)&nbsp;-&gt;&nbsp;<i>procedure</i></tt><a name="node_idx_204"></a>
  887. </p>
  888. <li><p><tt>(make-table-immutable!<i> table</i>)</tt><a name="node_idx_206"></a>
  889. </p>
  890. </ul><p>
  891. The first four functions listed make various kinds of tables.
  892. <tt>Make-table</tt> returns a table whose keys may be symbols, integer,
  893. characters, booleans, or the empty list (these are also the values
  894. that may be used in <tt>case</tt> expressions).
  895. As with <tt>case</tt>, comparison is done using <tt>eqv?</tt>.
  896. The comparison procedures used in symbol, string, and integer tables are
  897. <tt>eq?</tt>, <tt>string=?</tt>, and <tt>=</tt>.</p>
  898. <p>
  899. <tt>Make-table-maker</tt> takes two procedures as arguments and returns
  900. a nullary table-making procedure.
  901. <i>Compare-proc</i> should be a two-argument equality predicate.
  902. <i>Hash-proc</i> should be a one argument procedure that takes a key
  903. and returns a non-negative integer hash value.
  904. If <tt>(<i>compare-proc</i> <i>x</i> <i>y</i>)</tt> returns true,
  905. then <tt>(= (<i>hash-proc</i> <i>x</i>) (<i>hash-proc</i> <i>y</i>))</tt>
  906. must also return true.
  907. For example, <tt>make-integer-table</tt> could be defined
  908. as <tt>(make-table-maker = abs)</tt>.</p>
  909. <p>
  910. <tt>Make-table-immutable!</tt> prohibits future modification to its argument.</p>
  911. <p>
  912. </p>
  913. <ul>
  914. <li><p><tt>(table?<i> value</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_208"></a>
  915. </p>
  916. <li><p><tt>(table-ref<i> table key</i>)&nbsp;-&gt;&nbsp;<i>value or <tt>#f</tt></i></tt><a name="node_idx_210"></a>
  917. </p>
  918. <li><p><tt>(table-set!<i> table key value</i>)</tt><a name="node_idx_212"></a>
  919. </p>
  920. <li><p><tt>(table-walk<i> procedure table</i>)</tt><a name="node_idx_214"></a>
  921. </p>
  922. </ul><p>
  923. <tt>Table?</tt> is the predicate for tables.
  924. <tt>Table-ref</tt> and <tt>table-set!</tt> access and modify the value of <i>key</i>
  925. in <i>table</i>.
  926. <tt>Table-walk</tt> applies <i>procedure</i>, which must accept two arguments,
  927. to every associated key and non-<tt>#f</tt> value in <tt>table</tt>.</p>
  928. <p>
  929. </p>
  930. <ul>
  931. <li><p><tt>(default-hash-function<i> value</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_216"></a>
  932. </p>
  933. <li><p><tt>(string-hash<i> string</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_218"></a>
  934. </p>
  935. </ul><p>
  936. <tt>Default-hash-function</tt> is the hash function used in the tables
  937. returned by <tt>make-table</tt>, and <tt>string-hash</tt> it the one used
  938. by <tt>make-string-table</tt>.</p>
  939. <p>
  940. </p>
  941. <a name="node_sec_5.14"></a>
  942. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.14">5.14&nbsp;&nbsp;Port extensions</a></h2>
  943. <p>These procedures are in structure <tt>extended-ports</tt>.</p>
  944. <p>
  945. </p>
  946. <ul>
  947. <li><p><tt>(make-string-input-port<i> string</i>)&nbsp;-&gt;&nbsp;<i>input-port</i></tt><a name="node_idx_220"></a>
  948. </p>
  949. <li><p><tt>(make-string-output-port<i></i>)&nbsp;-&gt;&nbsp;<i>output-port</i></tt><a name="node_idx_222"></a>
  950. </p>
  951. <li><p><tt>(string-output-port-output<i> string-output-port</i>)&nbsp;-&gt;&nbsp;<i>string</i></tt><a name="node_idx_224"></a>
  952. </p>
  953. </ul><p>
  954. <tt>Make-string-input-port</tt> returns an input port that
  955. that reads characters from the supplied string. An end-of-file
  956. object is returned if the user reads past the end of the string.
  957. <tt>Make-string-output-port</tt> returns an output port that saves
  958. the characters written to it.
  959. These are then returned as a string by <tt>string-output-port-output</tt>.</p>
  960. <p>
  961. </p>
  962. <pre class=verbatim>(read (make-string-input-port &quot;(a b)&quot;))
  963. $$'(a b)
  964. (let ((p (make-string-output-port)))
  965. (write '(a b) p)
  966. (let ((s (string-output-port-output p)))
  967. (display &quot;c&quot; p)
  968. (list s (string-output-port-output p))))
  969. $$'(&quot;(a b)&quot; &quot;(a b)c&quot;)
  970. </pre><p></p>
  971. <p>
  972. </p>
  973. <ul>
  974. <li><p><tt>(limit-output<i> output-port n procedure</i>)</tt><a name="node_idx_226"></a>
  975. </p>
  976. </ul><p>
  977. <i>Procedure</i> is called on an output port.
  978. Output written to that port is copied to <i>output-port</i> until <i>n</i>
  979. characters have been written, at which point <tt>limit-output</tt> returns.
  980. If <i>procedure</i> returns before writing <i>n</i> characters, then
  981. <tt>limit-output</tt> also returns at that time, regardless of how many
  982. characters have been written.</p>
  983. <p>
  984. </p>
  985. <ul>
  986. <li><p><tt>(make-tracking-input-port<i> input-port</i>)&nbsp;-&gt;&nbsp;<i>input-port</i></tt><a name="node_idx_228"></a>
  987. </p>
  988. <li><p><tt>(make-tracking-output-port<i> output-port</i>)&nbsp;-&gt;&nbsp;<i>output-port</i></tt><a name="node_idx_230"></a>
  989. </p>
  990. <li><p><tt>(current-row<i> port</i>)&nbsp;-&gt;&nbsp;<i>integer or <tt>#f</tt></i></tt><a name="node_idx_232"></a>
  991. </p>
  992. <li><p><tt>(current-column<i> port</i>)&nbsp;-&gt;&nbsp;<i>integer or <tt>#f</tt></i></tt><a name="node_idx_234"></a>
  993. </p>
  994. <li><p><tt>(fresh-line<i> output-port</i>)</tt><a name="node_idx_236"></a>
  995. </p>
  996. </ul><p>
  997. <tt>Make-tracking-input-port</tt> and <tt>make-tracking-output-port</tt>
  998. return ports that keep track of the current row and column and
  999. are otherwise identical to their arguments.
  1000. Closing a tracking port does not close the underlying port.
  1001. <tt>Current-row</tt> and <tt>current-column</tt> return
  1002. <i>port</i>'s current read or write location.
  1003. They return <tt>#f</tt> if <i>port</i> does not keep track of its location.
  1004. <tt>Fresh-line</tt> writes a newline character to <i>output-port</i> if
  1005. <tt>(current-row <i>port</i>)</tt> is not 0.</p>
  1006. <p>
  1007. </p>
  1008. <pre class=verbatim>(define p (open-output-port &quot;/tmp/temp&quot;))
  1009. (list (current-row p) (current-column p))
  1010. $$'(0 0)
  1011. (display &quot;012&quot; p)
  1012. (list (current-row p) (current-column p))
  1013. $$'(0 3)
  1014. (fresh-line p)
  1015. (list (current-row p) (current-column p))
  1016. $$'(1 0)
  1017. (fresh-line p)
  1018. (list (current-row p) (current-column p))
  1019. $$'(1 0)
  1020. </pre><p></p>
  1021. <p>
  1022. </p>
  1023. <a name="node_sec_5.15"></a>
  1024. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.15">5.15&nbsp;&nbsp;Fluid bindings</a></h2>
  1025. <p>These procedures implement dynamic binding and are in structure <tt>fluids</tt>.
  1026. A <i>fluid</i> is a cell whose value can be bound dynamically.
  1027. Each fluid has a top-level value that is used when the fluid
  1028. is unbound in the current dynamic environment.</p>
  1029. <p>
  1030. </p>
  1031. <ul>
  1032. <li><p><tt>(make-fluid<i> value</i>)&nbsp;-&gt;&nbsp;<i>fluid</i></tt><a name="node_idx_238"></a>
  1033. </p>
  1034. <li><p><tt>(fluid<i> fluid</i>)&nbsp;-&gt;&nbsp;<i>value</i></tt><a name="node_idx_240"></a>
  1035. </p>
  1036. <li><p><tt>(let-fluid<i> fluid value thunk</i>)&nbsp;-&gt;&nbsp;<i>value(s)</i></tt><a name="node_idx_242"></a>
  1037. </p>
  1038. <li><p><tt>(let-fluids<i> fluid<sub>0</sub> value<sub>0</sub> fluid<sub>1</sub> value<sub>1</sub> <tt>...</tt>thunk</i>)&nbsp;-&gt;&nbsp;<i>value(s)</i></tt><a name="node_idx_244"></a>
  1039. </p>
  1040. </ul><p>
  1041. <tt>Make-fluid</tt> returns a new fluid with <i>value</i> as its initial
  1042. top-level value.
  1043. <tt>Fluid</tt> returns <tt>fluid</tt>'s current value.
  1044. <tt>Let-fluid</tt> calls <tt>thunk</tt>, with <i>fluid</i> bound to <i>value</i>
  1045. until <tt>thunk</tt> returns.
  1046. Using a continuation to throw out of the call to <tt>thunk</tt> causes
  1047. <i>fluid</i> to revert to its original value, while throwing back
  1048. in causes <i>fluid</i> to be rebound to <i>value</i>.
  1049. <tt>Let-fluid</tt> returns the value(s) returned by <i>thunk</i>.
  1050. <tt>Let-fluids</tt> is identical to <tt>let-fluid</tt> except that it binds
  1051. an arbitrary number of fluids to new values.</p>
  1052. <p>
  1053. </p>
  1054. <pre class=verbatim>(let* ((f (make-fluid 'a))
  1055. (v0 (fluid f))
  1056. (v1 (let-fluid f 'b
  1057. (lambda ()
  1058. (fluid f))))
  1059. (v2 (fluid f)))
  1060. (list v0 v1 v2))
  1061. $$'(a b a)
  1062. </pre><p></p>
  1063. <p>
  1064. </p>
  1065. <pre class=verbatim>(let ((f (make-fluid 'a))
  1066. (path '())
  1067. (c #f))
  1068. (let ((add (lambda ()
  1069. (set! path (cons (fluid f) path)))))
  1070. (add)
  1071. (let-fluid f 'b
  1072. (lambda ()
  1073. (call-with-current-continuation
  1074. (lambda (c0)
  1075. (set! c c0)))
  1076. (add)))
  1077. (add)
  1078. (if (&lt; (length path) 5)
  1079. (c)
  1080. (reverse path))))
  1081. $$'(a b a b a)
  1082. </pre><p></p>
  1083. <p>
  1084. </p>
  1085. <a name="node_sec_5.16"></a>
  1086. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.16">5.16&nbsp;&nbsp;Shell commands</a></h2>
  1087. <p>Structure <tt>c-system-function</tt> provides access to the C <tt>system()</tt>
  1088. function.</p>
  1089. <p>
  1090. </p>
  1091. <ul>
  1092. <li><p><tt>(have-system?<i></i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_246"></a>
  1093. </p>
  1094. <li><p><tt>(system<i> string</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_248"></a>
  1095. </p>
  1096. </ul><p>
  1097. <tt>Have-system?</tt> returns true if the underlying C implementation
  1098. has a command processor.
  1099. <tt>(System <i>string</i>)</tt> passes <i>string</i> to the C
  1100. <tt>system()</tt> function and returns the result.</p>
  1101. <p>
  1102. </p>
  1103. <pre class=verbatim>(begin
  1104. (system &quot;echo foo &gt; test-file&quot;)
  1105. (call-with-input-file &quot;test-file&quot; read))
  1106. $$'foo
  1107. </pre><p></p>
  1108. <p>
  1109. </p>
  1110. <a name="node_sec_5.17"></a>
  1111. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.17">5.17&nbsp;&nbsp;Sockets</a></h2>
  1112. <p></p>
  1113. <p>
  1114. Structure <tt>sockets</tt> provides access to TCP/IP sockets for interprocess
  1115. and network communication.</p>
  1116. <p>
  1117. </p>
  1118. <ul>
  1119. <li><p><tt>(open-socket<i></i>)&nbsp;-&gt;&nbsp;<i>socket</i></tt><a name="node_idx_250"></a>
  1120. </p>
  1121. <li><p><tt>(open-socket<i> port-number</i>)&nbsp;-&gt;&nbsp;<i>socket</i></tt><a name="node_idx_252"></a>
  1122. </p>
  1123. <li><p><tt>(socket-port-number<i> socket</i>)&nbsp;-&gt;&nbsp;<i>integer</i></tt><a name="node_idx_254"></a>
  1124. </p>
  1125. <li><p><tt>(close-socket<i> socket</i>)</tt><a name="node_idx_256"></a>
  1126. </p>
  1127. <li><p><tt>(socket-accept<i> socket</i>)&nbsp;-&gt;&nbsp;<i>input-port output-port</i></tt><a name="node_idx_258"></a>
  1128. </p>
  1129. <li><p><tt>(get-host-name<i></i>)&nbsp;-&gt;&nbsp;<i>string</i></tt><a name="node_idx_260"></a>
  1130. </p>
  1131. </ul><p>
  1132. <tt>Open-socket</tt> creates a new socket.
  1133. If no <i>port-number</i> is supplied the system picks one at random.
  1134. <tt>Socket-port-number</tt> returns a socket's port number.
  1135. <tt>Close-socket</tt> closes a socket, preventing any further connections.
  1136. <tt>Socket-accept</tt> accepts a single connection on <i>socket</i>, returning
  1137. an input port and an output port for communicating with the client.
  1138. If no client is waiting <tt>socket-accept</tt> blocks until one appears.
  1139. <tt>Get-host-name</tt> returns the network name of the machine.</p>
  1140. <p>
  1141. </p>
  1142. <ul>
  1143. <li><p><tt>(socket-client<i> host-name port-number</i>)&nbsp;-&gt;&nbsp;<i>input-port output-port</i></tt><a name="node_idx_262"></a>
  1144. </p>
  1145. </ul><p>
  1146. <tt>Socket-client</tt> connects to the server at <i>port-number</i> on
  1147. the machine named <i>host-name</i>.
  1148. <tt>Socket-client</tt> blocks until the server accepts the connection.</p>
  1149. <p>
  1150. The following simple example shows a server and client for a centralized UID
  1151. service.
  1152. </p>
  1153. <pre class=verbatim>(define (id-server)
  1154. (let ((socket (open-socket)))
  1155. (display &quot;Waiting on port &quot;)
  1156. (display (socket-port-number socket))
  1157. (newline)
  1158. (let loop ((next-id 0))
  1159. (call-with-values
  1160. (lambda ()
  1161. (socket-accept socket))
  1162. (lambda (in out)
  1163. (display next-id out)
  1164. (close-input-port in)
  1165. (close-output-port out)
  1166. (loop (+ next-id 1)))))))
  1167. (define (get-id machine port-number)
  1168. (call-with-values
  1169. (lambda ()
  1170. (socket-client machine port-number))
  1171. (lambda (in out)
  1172. (let ((id (read in)))
  1173. (close-input-port in)
  1174. (close-output-port out)
  1175. id))))
  1176. </pre><p></p>
  1177. <p>
  1178. </p>
  1179. <a name="node_sec_5.18"></a>
  1180. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.18">5.18&nbsp;&nbsp;Macros for writing loops</a></h2>
  1181. <p></p>
  1182. <p>
  1183. <tt>Iterate</tt> and <tt>reduce</tt> are extensions of named-<tt>let</tt> for
  1184. writing loops that walk down one or more sequences,
  1185. such as the elements of a list or vector, the
  1186. characters read from a port, or an arithmetic series.
  1187. Additional sequences can be defined by the user.
  1188. <tt>Iterate</tt> and <tt>reduce</tt> are in structure <tt>reduce</tt>.</p>
  1189. <p>
  1190. </p>
  1191. <a name="node_sec_5.18.1"></a>
  1192. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.18.1">5.18.1&nbsp;&nbsp;<tt>Iterate</tt></a></h3>
  1193. <p>The syntax of <tt>iterate</tt> is:
  1194. </p>
  1195. <pre class=verbatim> (iterate <i>loop-name</i>
  1196. ((<i>sequence-type</i> <i>element-variable</i> <i>sequence-data</i> <tt>...</tt>)
  1197. <tt>...</tt>)
  1198. ((<i>state-variable</i> <i>initial-value</i>)
  1199. <tt>...</tt>)
  1200. <i>body-expression</i>
  1201. [<i>final-expression</i>])
  1202. </pre><p></p>
  1203. <p>
  1204. <tt>Iterate</tt> steps the <i>element-variable</i>s in parallel through the
  1205. sequences, while each <i>state-variable</i> has the corresponding
  1206. <i>initial-value</i> for the first iteration and have later values
  1207. supplied by <i>body-expression</i>.
  1208. If any sequence has reached its limit the value of the <tt>iterate</tt>
  1209. expression is
  1210. the value of <i>final-expression</i>, if present, or the current values of
  1211. the <i>state-variable</i>s, returned as multiple values.
  1212. If no sequence has reached
  1213. its limit, <i>body-expression</i> is evaluated and either calls <i>loop-name</i> with
  1214. new values for the <i>state-variable</i>s, or returns some other value(s).</p>
  1215. <p>
  1216. The <i>loop-name</i> and the <i>state-variable</i>s and <i>initial-value</i>s behave
  1217. exactly as in named-<tt>let</tt>. The named-<tt>let</tt> expression
  1218. </p>
  1219. <pre class=verbatim> (let loop-name ((state-variable initial-value) ...)
  1220. body ...)
  1221. </pre><p>
  1222. is equivalent to an <tt>iterate</tt> expression with no sequences
  1223. (and with an explicit
  1224. <tt>let</tt> wrapped around the body expressions to take care of any
  1225. internal <tt>define</tt>s):
  1226. </p>
  1227. <pre class=verbatim> (iterate loop-name
  1228. ()
  1229. ((state-variable initial-value) ...)
  1230. (let () body ...))
  1231. </pre><p></p>
  1232. <p>
  1233. The <i>sequence-type</i>s are keywords (they are actually macros of a particular
  1234. form; it is easy to add additional types of sequences).
  1235. Examples are <tt>list*</tt> which walks down the elements of a list and
  1236. <tt>vector*</tt> which does the same for vectors.
  1237. For each iteration, each <i>element-variable</i> is bound to the next
  1238. element of the sequence.
  1239. The <i>sequence-data</i> gives the actual list or vector or whatever.</p>
  1240. <p>
  1241. If there is a <i>final-expression</i>, it is evaluated when the end of one or more
  1242. sequences is reached.
  1243. If the <i>body-expression</i> does not call <i>loop-name</i> the
  1244. <i>final-expression</i> is not evaluated.
  1245. The <i>state-variable</i>s are visible in
  1246. <i>final-expression</i> but the <i>sequence-variable</i>s are not. </p>
  1247. <p>
  1248. The <i>body-expression</i> and the <i>final-expression</i> are in tail-position within
  1249. the <tt>iterate</tt>.
  1250. Unlike named-<tt>let</tt>, the behavior of a non-tail-recursive call to
  1251. <i>loop-name</i> is unspecified (because iterating down a sequence may involve side
  1252. effects, such as reading characters from a port).</p>
  1253. <p>
  1254. </p>
  1255. <a name="node_sec_5.18.2"></a>
  1256. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.18.2">5.18.2&nbsp;&nbsp;<tt>Reduce</tt></a></h3>
  1257. <p>If an <tt>iterate</tt> expression is not meant to terminate before a sequence
  1258. has reached its end,
  1259. <i>body-expression</i> will always end with a tail call to <i>loop-name</i>.
  1260. <tt>Reduce</tt> is a macro that makes this common case explicit.
  1261. The syntax of <tt>reduce</tt> is
  1262. the same as that of <tt>iterate</tt>, except that there is no <i>loop-name</i>.
  1263. The <i>body-expression</i> returns new values of the <i>state-variable</i>s
  1264. instead of passing them to <i>loop-name</i>.
  1265. Thus <i>body-expression</i> must return as many values as there are state
  1266. variables.
  1267. By special dispensation, if there are
  1268. no state variables then <i>body-expression</i> may return any number of values,
  1269. all of which are ignored.</p>
  1270. <p>
  1271. The syntax of <tt>reduce</tt> is:
  1272. </p>
  1273. <pre class=verbatim> (reduce ((<i>sequence-type</i> <i>element-variable</i> <i>sequence-data</i> <tt>...</tt>)
  1274. <tt>...</tt>)
  1275. ((<i>state-variable</i> <i>initial-value</i>)
  1276. <tt>...</tt>)
  1277. <i>body-expression</i>
  1278. [<i>final-expression</i>])
  1279. </pre><p></p>
  1280. <p>
  1281. The value(s) returned by an instance of <tt>reduce</tt> is the value(s) returned
  1282. by the <i>final-expression</i>, if present, or the current value(s) of the state
  1283. variables when the end of one or more sequences is reached.</p>
  1284. <p>
  1285. A <tt>reduce</tt> expression can be rewritten as an equivalent <tt>iterate</tt>
  1286. expression by adding a <i>loop-var</i> and a wrapper for the
  1287. <i>body-expression</i> that calls the <i>loop-var</i>.
  1288. </p>
  1289. <pre class=verbatim>(iterate loop
  1290. ((<i>sequence-type</i> <i>element-variable</i> <i>sequence-data</i> <tt>...</tt>)
  1291. <tt>...</tt>)
  1292. ((<i>state-variable</i> <i>initial-value</i>)
  1293. <tt>...</tt>)
  1294. (call-with-values (lambda ()
  1295. <i>body-expression</i>)
  1296. loop)
  1297. [<i>final-expression</i>])
  1298. </pre><p></p>
  1299. <p>
  1300. </p>
  1301. <a name="node_sec_5.18.3"></a>
  1302. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.18.3">5.18.3&nbsp;&nbsp;Sequence types</a></h3>
  1303. <p>The predefined sequence types are:
  1304. </p>
  1305. <ul>
  1306. <li><p><tt>(list* <i>elt-var</i> <i>list</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1307. </p>
  1308. <li><p><tt>(vector* <i>elt-var</i> <i>vector</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1309. </p>
  1310. <li><p><tt>(string* <i>elt-var</i> <i>string</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1311. </p>
  1312. <li><p><tt>(count* <i>elt-var</i> <i>start</i> [<i>end</i> [<i>step</i>]])</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1313. </p>
  1314. <li><p><tt>(input* <i>elt-var</i> <i>input-port</i> <i>read-procedure</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1315. </p>
  1316. <li><p><tt>(stream* <i>elt-var</i> <i>procedure</i> <i>initial-data</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1317. </p>
  1318. </ul><p></p>
  1319. <p>
  1320. For lists, vectors, and strings the element variable is bound to the
  1321. successive elements of the list or vector, or the characters in the
  1322. string.</p>
  1323. <p>
  1324. For <tt>count*</tt> the element variable is bound to the elements of the sequence
  1325. </p>
  1326. <pre class=verbatim> <i>start</i>, <i>start</i> + <i>step</i>, <i>start</i> + 2<i>step</i>, <tt>...</tt>, <i>end</i>
  1327. </pre><p>
  1328. inclusive of <i>start</i> and exclusive of <i>end</i>.
  1329. The default <i>step</i> is 1.
  1330. The sequence does not terminate if no <i>end</i> is given or if there
  1331. is no <em>N</em> &gt; 0 such that <i>end</i> = <i>start</i> + N<i>step</i>
  1332. (<tt>=</tt> is used to test for termination).
  1333. For example, <tt>(count* i 0 -1)</tt> doesn't terminate
  1334. because it begins past the <i>end</i> value and <tt>(count* i 0 1 2)</tt> doesn't
  1335. terminate because it skips over the <i>end</i> value.</p>
  1336. <p>
  1337. For <tt>input*</tt> the elements are the results of successive applications
  1338. of <i>read-procedure</i> to <i>input-port</i>.
  1339. The sequence ends when <i>read-procedure</i> returns an end-of-file object.</p>
  1340. <p>
  1341. For a stream, the <i>procedure</i> takes the current data value as an argument
  1342. and returns two values, the next value of the sequence and a new data value.
  1343. If the new data is <tt>#f</tt> then the previous element was the last
  1344. one. For example,
  1345. </p>
  1346. <pre class=verbatim> (list* elt my-list)
  1347. </pre><p>
  1348. is the same as
  1349. </p>
  1350. <pre class=verbatim> (stream* elt list-&gt;stream my-list)
  1351. </pre><p>
  1352. where <tt>list-&gt;stream</tt> is
  1353. </p>
  1354. <pre class=verbatim> (lambda (list)
  1355. (if (null? list)
  1356. (values 'ignored #f)
  1357. (values (car list) (cdr list))))
  1358. </pre><p></p>
  1359. <p>
  1360. </p>
  1361. <a name="node_sec_5.18.4"></a>
  1362. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.18.4">5.18.4&nbsp;&nbsp;Synchronous sequences</a></h3>
  1363. <p>When using the sequence types described above, a loop terminates when any of
  1364. its sequences reaches its end. To help detect bugs it is useful to have
  1365. sequence types that check to see if two or more sequences end on the same
  1366. iteration. For this purpose there is second set of sequence types called
  1367. synchronous sequences. These are identical to the ones listed above except
  1368. that they cause an error to be signalled if a loop is terminated by a
  1369. synchronous sequence and some other synchronous sequence did not reach its
  1370. end on the same iteration.</p>
  1371. <p>
  1372. Sequences are checked for termination in order, from left to right, and
  1373. if a loop is terminated by a non-synchronous sequence no further checking
  1374. is done.</p>
  1375. <p>
  1376. The synchronous sequences are:</p>
  1377. <p>
  1378. </p>
  1379. <ul>
  1380. <li><p><tt>(list% <i>elt-var</i> <i>list</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1381. </p>
  1382. <li><p><tt>(vector% <i>elt-var</i> <i>vector</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1383. </p>
  1384. <li><p><tt>(string% <i>elt-var</i> <i>string</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1385. </p>
  1386. <li><p><tt>(count% <i>elt-var</i> <i>start</i> <i>end</i> [<i>step</i>])</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1387. </p>
  1388. <li><p><tt>(input% <i>elt-var</i> <i>input-port</i> <i>read-procedure</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1389. </p>
  1390. <li><p><tt>(stream% <i>elt-var</i> <i>procedure</i> <i>initial-data</i>)</tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(syntax)
  1391. </p>
  1392. </ul><p></p>
  1393. <p>
  1394. Note that the synchronous <tt>count%</tt> must have an <i>end</i>, unlike the
  1395. nonsynchronous <tt>count%</tt>.</p>
  1396. <p>
  1397. </p>
  1398. <a name="node_sec_5.18.5"></a>
  1399. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.18.5">5.18.5&nbsp;&nbsp;Examples</a></h3>
  1400. <p>Gathering the indexes of list elements that answer true to some
  1401. predicate.
  1402. </p>
  1403. <pre class=verbatim>(lambda (my-list predicate)
  1404. (reduce ((list* elt my-list)
  1405. (count* i 0))
  1406. ((hits '()))
  1407. (if (predicate elt)
  1408. (cons i hits)
  1409. hits)
  1410. (reverse hits))
  1411. </pre><p></p>
  1412. <p>
  1413. Looking for the index of an element of a list.
  1414. </p>
  1415. <pre class=verbatim>(lambda (my-list predicate)
  1416. (iterate loop
  1417. ((list* elt my-list)
  1418. (count* i 0))
  1419. () ; no state
  1420. (if (predicate elt)
  1421. i
  1422. (loop))))
  1423. </pre><p></p>
  1424. <p>
  1425. Reading one line.
  1426. </p>
  1427. <pre class=verbatim>(define (read-line port)
  1428. (iterate loop
  1429. ((input* c port read-char))
  1430. ((chars '()))
  1431. (if (char=? c #<code class=verbatim>\</code>newline)
  1432. (list-&gt;string (reverse chars))
  1433. (loop (cons c chars)))
  1434. (if (null? chars)
  1435. (eof-object)
  1436. ; no newline at end of file
  1437. (list-&gt;string (reverse chars)))))
  1438. </pre><p></p>
  1439. <p>
  1440. Counting the lines in a file. We can't use <tt>count*</tt> because we
  1441. need the value of the count after the loop has finished.
  1442. </p>
  1443. <pre class=verbatim>(define (line-count name)
  1444. (call-with-input-file name
  1445. (lambda (in)
  1446. (reduce ((input* l in read-line))
  1447. ((i 0))
  1448. (+ i 1)))))
  1449. </pre><p></p>
  1450. <p>
  1451. </p>
  1452. <a name="node_sec_5.18.6"></a>
  1453. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.18.6">5.18.6&nbsp;&nbsp;Defining sequence types</a></h3>
  1454. <p>The sequence types are object-oriented macros similar to enumerations.
  1455. A non-synchronous sequence macro needs to supply three values:
  1456. <tt>#f</tt> to indicate that it isn't synchronous, a list of state variables
  1457. and their initializers, and the code for one iteration.
  1458. The first
  1459. two methods are CPS'ed: they take another macro and argument to
  1460. which to pass their result.
  1461. The <tt>synchronized?</tt> method gets no additional arguments.
  1462. The <tt>state-vars</tt> method is passed a list of names which
  1463. will be bound to the arguments to the sequence.
  1464. The final method, for the step, is passed the list of names bound to
  1465. the arguments and the list of state variables.
  1466. In addition there is
  1467. a variable to be bound to the next element of the sequence, the
  1468. body expression for the loop, and an expression for terminating the
  1469. loop.</p>
  1470. <p>
  1471. The definition of <tt>list*</tt> is
  1472. </p>
  1473. <pre class=verbatim>(define-syntax list*
  1474. (syntax-rules (synchronized? state-vars step)
  1475. ((list* synchronized? (next more))
  1476. (next #f more))
  1477. ((list* state-vars (start-list) (next more))
  1478. (next ((list-var start-list)) more))
  1479. ((list* step (start-list) (list-var)
  1480. value-var loop-body final-exp)
  1481. (if (null? list-var)
  1482. final-exp
  1483. (let ((value-var (car list-var))
  1484. (list-var (cdr list-var)))
  1485. loop-body)))))
  1486. </pre><p></p>
  1487. <p>
  1488. Synchronized sequences are the same, except that they need to
  1489. provide a termination test to be used when some other synchronized
  1490. method terminates the loop.
  1491. </p>
  1492. <pre class=verbatim>(define-syntax list%
  1493. (syntax-rules (sync done)
  1494. ((list% sync (next more))
  1495. (next #t more))
  1496. ((list% done (start-list) (list-var))
  1497. (null? list-var))
  1498. ((list% stuff ...)
  1499. (list* stuff ...))))
  1500. </pre><p></p>
  1501. <p>
  1502. </p>
  1503. <a name="node_sec_5.18.7"></a>
  1504. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.18.7">5.18.7&nbsp;&nbsp;Expanded code</a></h3>
  1505. <p>The expansion of
  1506. </p>
  1507. <pre class=verbatim> (reduce ((list* x '(1 2 3)))
  1508. ((r '()))
  1509. (cons x r))
  1510. </pre><p>
  1511. is
  1512. </p>
  1513. <pre class=verbatim> (let ((final (lambda (r) (values r)))
  1514. (list '(1 2 3))
  1515. (r '()))
  1516. (let loop ((list list) (r r))
  1517. (if (null? list)
  1518. (final r)
  1519. (let ((x (car list))
  1520. (list (cdr list)))
  1521. (let ((continue (lambda (r)
  1522. (loop list r))))
  1523. (continue (cons x r)))))))
  1524. </pre><p></p>
  1525. <p>
  1526. The only inefficiencies in this code are the <tt>final</tt> and <tt>continue</tt>
  1527. procedures, both of which could be substituted in-line.
  1528. The macro expander could do the substitution for <tt>continue</tt> when there
  1529. is no explicit proceed variable, as in this case, but not in general.</p>
  1530. <p>
  1531. </p>
  1532. <a name="node_sec_5.19"></a>
  1533. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.19">5.19&nbsp;&nbsp;Sorting lists and vectors</a></h2>
  1534. <p></p>
  1535. <p>
  1536. (This section, as the libraries it describes, was written mostly by
  1537. Olin Shivers for the draft of SRFI&nbsp;32.)</p>
  1538. <p>
  1539. </p>
  1540. <p>
  1541. </p>
  1542. <p>
  1543. The sort libraries in Scheme&nbsp;48 include
  1544. </p>
  1545. <ul>
  1546. <li><p>vector insert sort (stable)
  1547. </p>
  1548. <li><p>vector heap sort
  1549. </p>
  1550. <li><p>vector merge sort (stable)
  1551. </p>
  1552. <li><p>pure and destructive list merge sort (stable)
  1553. </p>
  1554. <li><p>stable vector and list merge
  1555. </p>
  1556. <li><p>miscellaneous sort-related procedures: vector and list merging,
  1557. sorted predicates, vector binary search, vector and list
  1558. delete-equal-neighbor procedures.
  1559. </p>
  1560. <li><p>a general, non-algorithmic set of procedure names for general sorting
  1561. and merging
  1562. </p>
  1563. </ul><p></p>
  1564. <p>
  1565. </p>
  1566. <a name="node_sec_5.19.1"></a>
  1567. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.19.1">5.19.1&nbsp;&nbsp;Design rules</a></h3>
  1568. <p></p>
  1569. <a name="node_sec_Temp_4"></a>
  1570. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_4">What vs. how</a></h5>
  1571. <p>There are two different interfaces: ``what'' (simple) and ``how'' (detailed).</p>
  1572. <p>
  1573. </p>
  1574. <dl><dt></dt><dd>
  1575. </dd><dt><b>Simple</b></dt><dd> you specify semantics: datatype (list or vector),
  1576. mutability, and stability.<p>
  1577. </p>
  1578. </dd><dt><b>Detailed</b></dt><dd> you specify the actual algorithm (quick, heap,
  1579. insert, merge). Different algorithms have different properties,
  1580. both semantic and pragmatic, so these exports are necessary.<p>
  1581. It is necessarily the case that the specifications of these procedures
  1582. make statements about execution ``pragmatics.'' For example, the sole
  1583. distinction between heap sort and quick sort -- both of which are
  1584. provided by this library -- -is one of execution time, which is not a
  1585. ``semantic'' distinction. Similar resource-use statements are made about
  1586. ``iterative'' procedures, meaning that they can execute on input of
  1587. arbitrary size in a constant number of stack frames.
  1588. </p>
  1589. </dd></dl><p></p>
  1590. <p>
  1591. </p>
  1592. <a name="node_sec_Temp_5"></a>
  1593. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_5">Consistency across procedure signatures</a></h5>
  1594. <p>The two interfaces share common procedure signatures wherever
  1595. possible, to facilitate switching a given call from one procedure
  1596. to another.</p>
  1597. <p>
  1598. </p>
  1599. <a name="node_sec_Temp_6"></a>
  1600. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_6">Less-than parameter first, data parameter after</a></h5>
  1601. <p>These procedures uniformly observe the following parameter order:
  1602. the data to be sorted comes after the comparison procedure.
  1603. That is, we write</p>
  1604. <p>
  1605. </p>
  1606. <pre class=verbatim> (sort &lt; <i>list</i>)
  1607. </pre><p></p>
  1608. <p>
  1609. not</p>
  1610. <p>
  1611. </p>
  1612. <pre class=verbatim> (sort <i>list</i> &lt;)
  1613. </pre><p>
  1614. </p>
  1615. <p>
  1616. </p>
  1617. <a name="node_sec_Temp_7"></a>
  1618. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_7">Ordering, comparison procedures and stability</a></h5>
  1619. <p>These routines take a &lt; comparison procedure, not a <u>&lt;</u> comparison
  1620. procedure, and they sort into increasing order. The difference between
  1621. a &lt; spec and a <u>&lt;</u> spec comes up in two places: </p>
  1622. <p>
  1623. </p>
  1624. <ul>
  1625. <li><p>the definition of an ordered or sorted data set, and
  1626. </p>
  1627. <li><p>the definition of a stable sorting algorithm.
  1628. </p>
  1629. </ul><p>
  1630. </p>
  1631. <p>
  1632. We say that a data set (a list or vector) is <i>sorted</i> or
  1633. <i>ordered</i> if it contains no adjacent pair of values <tt>...</tt> <em>x</em>,
  1634. <em>y</em> <tt>...</tt> such that <em>y</em> &lt; <em>x</em>.</p>
  1635. <p>
  1636. In other words, scanning across the data never takes a ``downwards'' step.</p>
  1637. <p>
  1638. If you use a <u>&lt;</u> procedure where these algorithms expect a &lt;
  1639. procedure, you may not get the answers you expect. For example,
  1640. the <tt>list-sorted?</tt> procedure will return false if you pass it a <u>&lt;</u> comparison
  1641. procedure and an ordered list containing adjacent equal elements.</p>
  1642. <p>
  1643. A ``stable'' sort is one that preserves the pre-existing order of equal
  1644. elements. Suppose, for example, that we sort a list of numbers by
  1645. comparing their absolute values, i.e., using comparison procedure
  1646. </p>
  1647. <pre class=verbatim>(lambda (x y) (&lt; (abs x) (abs y)))
  1648. </pre><p>
  1649. If we sort a list that contains both 3 and -3: </p>
  1650. <div align=center><table><tr><td><tt>...</tt> 3, <tt>...</tt>, <tt>-</tt> 3 <tt>...</tt></td></tr></table></div><p>
  1651. then a stable sort is an algorithm that will not swap the order
  1652. of these two elements, that is, the answer is guaranteed to to look like
  1653. </p>
  1654. <div align=center><table><tr><td><tt>...</tt> 3, <tt>-</tt> 3 <tt>...</tt></td></tr></table></div><p>
  1655. not
  1656. </p>
  1657. <div align=center><table><tr><td><tt>...</tt> <tt>-</tt> 3, 3 <tt>...</tt></td></tr></table></div><p>
  1658. Choosing &lt; for the comparison procedure instead of <u>&lt;</u> affects
  1659. how stability is coded. Given an adjacent pair <em>x</em>, <em>y</em>, <tt>(&lt;
  1660. <em>y</em> <em>x</em>)</tt> means ``<em>x</em> should be moved in front of <em>x</em>'' -- otherwise,
  1661. leave things as they are. So using a <u>&lt;</u> procedure where a &lt;
  1662. procedure is expected will <em>invert</em> stability.</p>
  1663. <p>
  1664. This is due to the definition of equality, given a &lt; comparator:
  1665. </p>
  1666. <pre class=verbatim> (and (not (&lt; x y))
  1667. (not (&lt; y x)))
  1668. </pre><p>
  1669. The definition is rather different, given a <u>&lt;</u> comparator:
  1670. </p>
  1671. <pre class=verbatim> (and (&lt;= x y)
  1672. (&lt;= y x))
  1673. </pre><p>
  1674. A ``stable'' merge is one that reliably favors one of its data sets
  1675. when equal items appear in both data sets. <em>All merge operations in
  1676. this library are stable</em>, breaking ties between data sets in favor
  1677. of the first data set -- elements of the first list come before equal
  1678. elements in the second list.</p>
  1679. <p>
  1680. So, if we are merging two lists of numbers ordered by absolute value,
  1681. the stable merge operation <tt>list-merge</tt>
  1682. </p>
  1683. <pre class=verbatim> (list-merge (lambda (x y) (&lt; (abs x) (abs y)))
  1684. '(0 -2 4 8 -10) '(-1 3 -4 7))
  1685. </pre><p>
  1686. reliably places the 4 of the first list before the equal-comparing -4
  1687. of the second list:
  1688. </p>
  1689. <pre class=verbatim> (0 -1 -2 4 -4 7 8 -10)
  1690. </pre><p>
  1691. Some sort algorithms will <em>not work correctly</em> if given a <u>&lt;</u>
  1692. when they expect a &lt; comparison (or vice-versa).</p>
  1693. <p>
  1694. </p>
  1695. <p>
  1696. In short, if your comparison procedure <em>f</em> answers true to <tt>(<em>f</em> x x)</tt>, then
  1697. </p>
  1698. <ul>
  1699. <li><p>using a stable sorting or merging algorithm will not give you a
  1700. stable sort or merge,
  1701. </p>
  1702. <li><p><tt>list-sorted?</tt> may surprise you.
  1703. </p>
  1704. </ul><p>
  1705. Note that you can synthesize a &lt; procedure from a <u>&lt;</u> procedure with
  1706. </p>
  1707. <pre class=verbatim> (lambda (x y) (not (&lt;= y x)))
  1708. </pre><p>
  1709. if need be. </p>
  1710. <p>
  1711. Precise definitions give sharp edges to tools, but require care in use.
  1712. ``Measure twice, cut once.''</p>
  1713. <p>
  1714. </p>
  1715. <p>
  1716. </p>
  1717. <a name="node_sec_Temp_8"></a>
  1718. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_8">All vector operations accept optional subrange parameters</a></h5>
  1719. <p>The vector operations specified below all take optional
  1720. <tt>start</tt>/<tt>end</tt> arguments indicating a selected subrange
  1721. of a vector's elements. If a <tt>start</tt> parameter or
  1722. <tt>start</tt>/<tt>end</tt> parameter pair is given to such a
  1723. procedure, they must be exact, non-negative integers, such that
  1724. </p>
  1725. <div align=center><table><tr><td>
  1726. 0 <u>&lt;</u> <i>start</i> <u>&lt;</u> <i>end</i> <u>&lt;</u> <tt>(vector-length <i>vector</i>)</tt>
  1727. </td></tr></table></div><p>
  1728. where <i>vector</i> is the related vector parameter. If not specified,
  1729. they default to 0 and the length of the vector, respectively. They are
  1730. interpreted to select the range [<i>start</i>,<i>end</i>), that
  1731. is, all elements from index <i>start</i> (inclusive) up to, but not
  1732. including, index <i>end</i>.</p>
  1733. <p>
  1734. </p>
  1735. <a name="node_sec_Temp_9"></a>
  1736. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_9">Required vs. allowed side-effects</a></h5>
  1737. <p><tt>List-sort!</tt> and <tt>List-stable-sort!</tt> are allowed, but
  1738. not required, to alter their arguments' cons cells to construct the
  1739. result list. This is consistent with the what-not-how character of the
  1740. group of procedures to which they belong (the <tt>sorting</tt> structure).</p>
  1741. <p>
  1742. The <tt>list-delete-neighbor-dups!</tt>, <tt>list-merge!</tt> and
  1743. <tt>list-merge-sort!</tt> procedures, on the other hand, provide
  1744. specific algorithms, and, as such, explicitly commit to the use of
  1745. side-effects on their input lists in order to guarantee their key
  1746. algorithmic properties (e.g., linear-time operation).</p>
  1747. <p>
  1748. </p>
  1749. <a name="node_sec_5.19.2"></a>
  1750. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.19.2">5.19.2&nbsp;&nbsp;Procedure specification</a></h3>
  1751. <p></p>
  1752. <div align=center><table><tr><td>
  1753. <table border=0><tr><td valign=top >Structure name </td><td valign=top >Functionality</td></tr>
  1754. <tr><td valign=top ><tt>sorting</tt> </td><td valign=top >General sorting for lists and vectors</td></tr>
  1755. <tr><td valign=top ><tt>sorted</tt> </td><td valign=top >Sorted predicates for lists and vectors</td></tr>
  1756. <tr><td valign=top ><tt>list-merge-sort</tt></td><td valign=top >List merge sort</td></tr>
  1757. <tr><td valign=top ><tt>vector-merge-sort</tt> </td><td valign=top >Vector merge sort</td></tr>
  1758. <tr><td valign=top ><tt>vector-heap-sort</tt> </td><td valign=top >Vector heap sort</td></tr>
  1759. <tr><td valign=top ><tt>vector-insert-sort</tt> </td><td valign=top >Vector insertion sort</td></tr>
  1760. <tr><td valign=top ><tt>delete-neighbor-duplicates</tt> </td><td valign=top >List and vector delete neighbor duplicates</td></tr>
  1761. <tr><td valign=top ><tt>binary-searches</tt> </td><td valign=top >Vector binary search
  1762. </td></tr></table>
  1763. </td></tr></table></div>
  1764. Note that there is no ``list insert sort'' package, as you might as well always
  1765. use list merge sort. The reference implementation's destructive list merge
  1766. sort will do fewer <tt>set-cdr!</tt>s than a destructive insert sort.<p>
  1767. </p>
  1768. <a name="node_sec_Temp_10"></a>
  1769. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_10">Procedure naming and functionality</a></h5>
  1770. <p>Almost all of the procedures described below are variants of two basic
  1771. operations: sorting and merging. These procedures are consistently named
  1772. by composing a set of basic lexemes to indicate what they do.
  1773. </p>
  1774. <div align=center><table><tr><td>
  1775. </td></tr><tr><td>
  1776. <p>
  1777. </p>
  1778. <table border=0><tr><td valign=top >Lexeme </td><td valign=top >Meaning</td></tr>
  1779. <tr><td valign=top ><tt>sort</tt></td><td valign=top >The procedure sorts its input data set by some &lt; comparison procedure.
  1780. </td></tr>
  1781. <tr><td valign=top ><tt>merge</tt></td><td valign=top >The procedure merges two ordered data sets into a single ordered
  1782. result.
  1783. </td></tr>
  1784. <tr><td valign=top ><tt>stable</tt> </td><td valign=top >This lexeme indicates that the sort is a stable one.
  1785. </td></tr>
  1786. <tr><td valign=top ><tt>vector</tt></td><td valign=top >The procedure operates upon vectors.
  1787. </td></tr>
  1788. <tr><td valign=top ><tt>list</tt> </td><td valign=top >The procedure operates upon lists.
  1789. </td></tr>
  1790. <tr><td valign=top ><tt>!</tt> </td><td valign=top >Procedures that end in <tt>!</tt> are allowed, and sometimes required,
  1791. to reuse their input storage to construct their answer.
  1792. </td></tr></table>
  1793. </td></tr></table></div>
  1794. <p>
  1795. </p>
  1796. <a name="node_sec_Temp_11"></a>
  1797. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_11">Types of parameters and return values</a></h5>
  1798. <p>In the procedures specified below,
  1799. </p>
  1800. <ul>
  1801. <li><p>A <tt>&lt;</tt> or <tt>=</tt> parameter is a procedure accepting
  1802. two arguments taken from the specified procedure's data set(s), and
  1803. returning a boolean;
  1804. </p>
  1805. <li><p><tt>Start</tt> and <tt>end</tt> parameters are exact, non-negative integers that
  1806. serve as vector indices selecting a subrange of some associated vector.
  1807. When specified, they must satisfy the relation
  1808. </p>
  1809. <div align=center><table><tr><td>
  1810. 0 <u>&lt;</u> <i>start</i> <u>&lt;</u> <i>end</i> <u>&lt;</u> <tt>(vector-length <i>vector</i>)</tt>
  1811. </td></tr></table></div><p>
  1812. where <i>vector</i> is the associated vector.
  1813. </p>
  1814. </ul><p>
  1815. Passing values to procedures with these parameters that do not satisfy
  1816. these types is an error.</p>
  1817. <p>
  1818. If a procedure is said to return ``unspecified,'' this means that
  1819. nothing at all is said about what the procedure returns, not even the
  1820. number of return values. Such a procedure is not even required to be
  1821. consistent from call to call in the nature or number of its return
  1822. values. It is simply required to return a value (or values) that may
  1823. be passed to a command continuation, e.g. as the value of an
  1824. expression appearing as a non-terminal subform of a <tt>begin</tt>
  1825. expression. Note that in R<sup>5</sup>RS, this restricts such a procedure to
  1826. returning a single value; non-R<sup>5</sup>RS systems may not even provide this
  1827. restriction.</p>
  1828. <p>
  1829. </p>
  1830. <a name="node_sec_5.19.2.1"></a>
  1831. <h4><a href="manual-Z-H-2.html#node_toc_node_sec_5.19.2.1">5.19.2.1&nbsp;&nbsp;<tt>sorting</tt> -- general sorting package</a></h4>
  1832. <p>This library provides basic sorting and merging functionality suitable for
  1833. general programming. The procedures are named by their semantic properties,
  1834. i.e., what they do to the data (sort, stable sort, merge, and so forth).</p>
  1835. <p>
  1836. </p>
  1837. <ul>
  1838. <li><p><tt>(list-sorted?<i> &lt; list</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_264"></a>
  1839. </p>
  1840. <li><p><tt>(list-merge<i> &lt; list<sub>1</sub> list<sub>2</sub></i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_266"></a>
  1841. </p>
  1842. <li><p><tt>(list-merge!<i> &lt; list<sub>1</sub> list<sub>2</sub></i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_268"></a>
  1843. </p>
  1844. <li><p><tt>(list-sort<i> &lt; lis</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_270"></a>
  1845. </p>
  1846. <li><p><tt>(list-sort!<i> &lt; lis</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_272"></a>
  1847. </p>
  1848. <li><p><tt>(list-stable-sort<i> &lt; list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_274"></a>
  1849. </p>
  1850. <li><p><tt>(list-stable-sort!<i> &lt; list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_276"></a>
  1851. </p>
  1852. <li><p><tt>(list-delete-neighbor-dups<i> = list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_278"></a>
  1853. </p>
  1854. <li><p><tt>(vector-sorted?<i> &lt; v [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_280"></a>
  1855. </p>
  1856. <li><p><tt>(vector-merge<i> &lt; v<sub>1</sub> v<sub>2</sub> [start1 [end1 [start2 [end2]]]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_282"></a>
  1857. </p>
  1858. <li><p><tt>(vector-merge!<i> &lt; v v<sub>1</sub> v<sub>2</sub> [start [start1 [end1 [start2 [end2]]]]]</i>)</tt><a name="node_idx_284"></a>
  1859. </p>
  1860. <li><p><tt>(vector-sort<i> &lt; v [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_286"></a>
  1861. </p>
  1862. <li><p><tt>(vector-sort!<i> &lt; v [start [end]]</i>)</tt><a name="node_idx_288"></a>
  1863. </p>
  1864. <li><p><tt>(vector-stable-sort<i> &lt; v [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_290"></a>
  1865. </p>
  1866. <li><p><tt>(vector-stable-sort!<i> &lt; v [start [end]]</i>)</tt><a name="node_idx_292"></a>
  1867. </p>
  1868. <li><p><tt>(vector-delete-neighbor-dups<i> = v [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_294"></a>
  1869. </p>
  1870. </ul><p></p>
  1871. <p>
  1872. </p>
  1873. <div align=center><table><tr><td>
  1874. <table border=0><tr><td valign=top >Procedure </td><td valign=top >Suggested algorithm
  1875. </td></tr>
  1876. <tr><td valign=top ><tt>list-sort</tt> </td><td valign=top >vector heap or quick</td></tr>
  1877. <tr><td valign=top ><tt>list-sort!</tt> </td><td valign=top >list merge sort</td></tr>
  1878. <tr><td valign=top ><tt>list-stable-sort</tt> </td><td valign=top >vector merge sort</td></tr>
  1879. <tr><td valign=top ><tt>list-stable-sort!</tt> </td><td valign=top >list merge sort</td></tr>
  1880. <tr><td valign=top ><tt>vector-sort</tt> </td><td valign=top >heap or quick sort</td></tr>
  1881. <tr><td valign=top ><tt>vector-sort!</tt> or quick sort</td></tr>
  1882. <tr><td valign=top ><tt>vector-stable-sort</tt> </td><td valign=top >vector merge sort</td></tr>
  1883. <tr><td valign=top ><tt>vector-stable-sort!</tt> merge sort
  1884. </td></tr></table>
  1885. </td></tr></table></div>
  1886. <tt>List-Sorted?</tt> and <tt>vector-sorted?</tt> return true if their
  1887. input list or vector is in sorted order, as determined by their <i>&lt;</i>
  1888. comparison parameter.<p>
  1889. All four merge operations are stable: an element of the initial list
  1890. <i>list<sub>1</sub></i> or vector <i>vector<sub>1</sub></i> will come before an
  1891. equal-comparing element in the second list <i>list<sub>2</sub></i> or vector
  1892. <i>vector<sub>2</sub></i> in the result.</p>
  1893. <p>
  1894. The procedures
  1895. </p>
  1896. <ul>
  1897. <li><p><tt>list-merge</tt>
  1898. </p>
  1899. <li><p><tt>list-sort</tt>
  1900. </p>
  1901. <li><p><tt>list-stable-sort</tt>
  1902. </p>
  1903. <li><p><tt>list-delete-neighbor-dups</tt>
  1904. </p>
  1905. </ul><p>
  1906. do not alter their inputs and are allowed to return a value that shares
  1907. a common tail with a list argument.</p>
  1908. <p>
  1909. The procedure
  1910. </p>
  1911. <ul>
  1912. <li><p><tt>list-sort!</tt>
  1913. </p>
  1914. <li><p><tt>list-stable-sort!</tt>
  1915. </p>
  1916. </ul><p>
  1917. are ``linear update'' operators -- they are allowed, but not required, to
  1918. alter the cons cells of their arguments to produce their results. </p>
  1919. <p>
  1920. On the other hand, the <tt>list-merge!</tt> procedure
  1921. make only a single, iterative, linear-time pass over its argument
  1922. list, using <tt>set-cdr!</tt>s to rearrange the cells of the list
  1923. into the final result -- it works ``in place.'' Hence, any cons cell
  1924. appearing in the result must have originally appeared in an input. The
  1925. intent of this iterative-algorithm commitment is to allow the
  1926. programmer to be sure that if, for example, <tt>list-merge!</tt> is asked to
  1927. merge two ten-million-element lists, the operation will complete
  1928. without performing some extremely (possibly twenty-million) deep
  1929. recursion.</p>
  1930. <p>
  1931. The vector procedures
  1932. </p>
  1933. <ul>
  1934. <li><p><tt>vector-sort</tt>
  1935. </p>
  1936. <li><p><tt>vector-stable-sort</tt>
  1937. </p>
  1938. <li><p><tt>vector-delete-neighbor-dups</tt>
  1939. </p>
  1940. </ul><p>
  1941. do not alter their inputs, but allocate a fresh vector for their result,
  1942. of length <i>end</i> <tt>-</tt> <i>start</i>. </p>
  1943. <p>
  1944. The vector procedures
  1945. </p>
  1946. <ul>
  1947. <li><p><tt>vector-sort!</tt>
  1948. </p>
  1949. <li><p><tt>vector-stable-sort!</tt>
  1950. </p>
  1951. </ul><p>
  1952. sort their data in-place. (But note that <tt>vector-stable-sort!</tt>
  1953. may allocate temporary storage proportional to the size of the
  1954. input
  1955. .)</p>
  1956. <p>
  1957. <tt>Vector-merge</tt> returns a vector of length (<i>end<sub>1</sub></i> <tt>-</tt> <i>start<sub>1</sub></i> + (<i>end<sub>2</sub></i> <tt>-</tt> <i>start<sub>2</sub></i>).</p>
  1958. <p>
  1959. <tt>Vector-merge!</tt> writes its result into vector <i>v</i>,
  1960. beginning at index <i>start</i>, for indices less than <i>end</i> =
  1961. <i>start</i> + (<i>end<sub>1</sub></i> <tt>-</tt> <i>start<sub>1</sub></i>) +
  1962. (<i>end<sub>2</sub></i> <tt>-</tt> <i>start<sub>2</sub></i>). The target subvector
  1963. <i>v</i>[<i>start</i>,<i>end</i>) may not overlap either source
  1964. subvector <i>vector<sub>1</sub></i>[<i>start<sub>1</sub></i>,<i>end<sub>1</sub></i>) <i>vector<sub>2</sub></i>[<i>start<sub>2</sub></i>,<i>end<sub>2</sub></i>).</p>
  1965. <p>
  1966. The <tt><tt>...</tt>-delete-neighbor-dups-<tt>...</tt></tt> procedures:
  1967. These procedures delete adjacent duplicate elements from a list or a
  1968. vector, using a given element-equality procedure. The first/leftmost
  1969. element of a run of equal elements is the one that survives. The list or
  1970. vector is not otherwise disordered.</p>
  1971. <p>
  1972. These procedures are linear time -- much faster than the <em>O</em>(<em>n</em><sup>2</sup>) general
  1973. duplicate-element deletors that do not assume any ``bunching'' of elements
  1974. (such as the ones provided by SRFI&nbsp;1). If you want to delete duplicate
  1975. elements from a large list or vector, you can sort the elements to bring
  1976. equal items together, then use one of these procedures, for a total time
  1977. of <em>O</em>(<em>n</em>log(<em>n</em>)).</p>
  1978. <p>
  1979. The comparison procedure = passed to these procedures is always
  1980. applied
  1981. <tt>( = <em>x</em> <em>y</em>)</tt>
  1982. where <em>x</em> comes before <em>y</em> in the containing list or vector.</p>
  1983. <p>
  1984. </p>
  1985. <ul>
  1986. <li><p><tt>List-delete-neighbor-dups</tt> does not alter its input list; its answer
  1987. may share storage with the input list.
  1988. </p>
  1989. <li><p><tt>Vector-delete-neighbor-dups</tt> does not alter its input vector, but
  1990. rather allocates a fresh vector to hold the result.
  1991. </p>
  1992. </ul><p>
  1993. Examples:</p>
  1994. <p>
  1995. </p>
  1996. <pre class=verbatim>(list-delete-neighbor-dups = '(1 1 2 7 7 7 0 -2 -2))
  1997. ===&gt; (1 2 7 0 -2)
  1998. (vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2))
  1999. ===&gt; #(1 2 7 0 -2)
  2000. (vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2) 3 7)
  2001. ===&gt; #(7 0 -2)
  2002. </pre><p></p>
  2003. <p>
  2004. </p>
  2005. <a name="node_sec_5.19.2.2"></a>
  2006. <h4><a href="manual-Z-H-2.html#node_toc_node_sec_5.19.2.2">5.19.2.2&nbsp;&nbsp;Algorithm-specific sorting packages</a></h4>
  2007. <p>These packages provide more specific sorting functionality, that is,
  2008. specific committment to particular algorithms that have particular
  2009. pragmatic consequences (such as memory locality, asymptotic running time)
  2010. beyond their semantic behaviour (sorting, stable sorting, merging, etc.).
  2011. Programmers that need a particular algorithm can use one of these packages.</p>
  2012. <p>
  2013. </p>
  2014. <a name="node_sec_Temp_12"></a>
  2015. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_12"><tt>sorted</tt> -- sorted predicates</a></h5>
  2016. <p></p>
  2017. <ul>
  2018. <li><p><tt>(list-sorted?<i> &lt; list</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_296"></a>
  2019. </p>
  2020. <li><p><tt>(vector-sorted?<i> &lt; vector</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_298"></a>
  2021. </p>
  2022. <li><p><tt>(vector-sorted?<i> &lt; vector start</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_300"></a>
  2023. </p>
  2024. <li><p><tt>(vector-sorted?<i> &lt; vector start end</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_302"></a>
  2025. </p>
  2026. </ul><p></p>
  2027. <p>
  2028. Return <tt>#f</tt> iff there is an adjacent pair <tt>...</tt> <em>x</em>, <em>y</em> <tt>...</tt> in the input
  2029. list or vector such that <em>y</em> &lt; <em>x</em>. The optional <i>start</i>/<i>end</i> range
  2030. arguments restrict <tt>vector-sorted?</tt> to the indicated subvector.</p>
  2031. <p>
  2032. </p>
  2033. <a name="node_sec_Temp_13"></a>
  2034. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_13"><tt>list-merge-sort</tt> -- list merge sort</a></h5>
  2035. <p></p>
  2036. <ul>
  2037. <li><p><tt>(list-merge-sort<i> &lt; list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_304"></a>
  2038. </p>
  2039. <li><p><tt>(list-merge-sort!<i> &lt; list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_306"></a>
  2040. </p>
  2041. <li><p><tt>(list-merge<i> list<sub>1</sub> &lt; list<sub>2</sub></i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_308"></a>
  2042. </p>
  2043. <li><p><tt>(list-merge!<i> list<sub>1</sub> &lt; list<sub>2</sub></i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_310"></a>
  2044. </p>
  2045. </ul><p>
  2046. The sort procedures sort their data using a list merge sort, which is
  2047. stable. (The reference implementation is, additionally, a ``natural'' sort.
  2048. See below for the properties of this algorithm.)</p>
  2049. <p>
  2050. The <tt>!</tt> procedures are destructive -- they use <tt>set-cdr!</tt>s to
  2051. rearrange the cells of the lists into the proper order. As such, they
  2052. do not allocate any extra cons cells -- they are ``in place'' sorts.
  2053. </p>
  2054. <p>
  2055. The merge operations are stable: an element of <i>list<sub>1</sub></i> will
  2056. come before an equal-comparing element in <i>list<sub>2</sub></i> in the result
  2057. list.</p>
  2058. <p>
  2059. </p>
  2060. <a name="node_sec_Temp_14"></a>
  2061. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_14"><tt>vector-merge-sort</tt> -- vector merge sort</a></h5>
  2062. <p></p>
  2063. <ul>
  2064. <li><p><tt>(vector-merge-sort<i> &lt; vector [start [end [temp]]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_312"></a>
  2065. </p>
  2066. <li><p><tt>(vector-merge-sort!<i> &lt; vector [start [end [temp]]]</i>)</tt><a name="node_idx_314"></a>
  2067. </p>
  2068. <li><p><tt>(vector-merge<i> &lt; vector<sub>1</sub> vector<sub>2</sub> [start<sub>1</sub> [end<sub>1</sub> [start<sub>2</sub> [end<sub>2</sub>]]]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_316"></a>
  2069. </p>
  2070. <li><p><tt>(vector-merge!<i> &lt; vector vector<sub>1</sub> vector<sub>2</sub> [start [start<sub>1</sub> [end<sub>1</sub> [start<sub>2</sub> [end<sub>2</sub>]]]]]</i>)</tt><a name="node_idx_318"></a>
  2071. </p>
  2072. </ul><p>
  2073. The sort procedures sort their data using vector merge sort, which is
  2074. stable. (The reference implementation is, additionally, a ``natural'' sort.
  2075. See below for the properties of this algorithm.)</p>
  2076. <p>
  2077. The optional <i>start</i>/<i>end</i> arguments provide for sorting of subranges, and
  2078. default to 0 and the length of the corresponding vector.</p>
  2079. <p>
  2080. Merge-sorting a vector requires the allocation of a temporary
  2081. ``scratch'' work vector for the duration of the sort. This scratch
  2082. vector can be passed in by the client as the optional <i>temp</i>
  2083. argument; if so, the supplied vector must be of size <u>&lt;</u> <i>end</i>,
  2084. and will not be altered outside the range [start,end). If not
  2085. supplied, the sort routines allocate one themselves.</p>
  2086. <p>
  2087. The merge operations are stable: an element of <i>vector<sub>1</sub></i> will
  2088. come before an equal-comparing element in <i>vector<sub>2</sub></i> in the
  2089. result vector.</p>
  2090. <p>
  2091. </p>
  2092. <ul>
  2093. <li><p><tt>Vector-merge-sort!</tt> leaves its result in
  2094. <i>vector</i>[<i>start</i>,<i>end</i>).
  2095. </p>
  2096. <li><p><tt>Vector-merge-sort</tt> returns a vector of length
  2097. <i>end</i> <tt>-</tt> <i>start</i>.
  2098. </p>
  2099. <li><p><tt>Vector-merge</tt> returns a vector of length
  2100. (<i>end<sub>1</sub></i> <tt>-</tt> <i>start<sub>1</sub></i>) + (<i>end<sub>2</sub></i> <tt>-</tt> <i>start<sub>2</sub></i>).
  2101. </p>
  2102. <li><p><tt>Vector-merge!</tt> writes its result into <i>vector</i>, beginning
  2103. at index <i>start</i>,
  2104. for indices less than <i>end</i> = <i>start</i> +
  2105. (<i>end<sub>1</sub></i> <tt>-</tt> <i>start<sub>1</sub></i>) + (<i>end<sub>2</sub></i> <tt>-</tt> <i>start<sub>2</sub></i>).
  2106. The target subvector
  2107. </p>
  2108. <div align=center><table><tr><td><i>vector</i>[<i>start</i>,<i>end</i>)</td></tr></table></div><p>
  2109. may not overlap either source subvector
  2110. </p>
  2111. <div align=center><table><tr><td><i>vector<sub>1</sub></i>[<i>start<sub>1</sub></i>,<i>end<sub>1</sub></i>), or
  2112. <i>vector<sub>2</sub></i>[<i>start<sub>2</sub></i>,<i>end<sub>2</sub></i>).</td></tr></table></div><p>
  2113. </p>
  2114. </ul><p></p>
  2115. <p>
  2116. </p>
  2117. <a name="node_sec_Temp_15"></a>
  2118. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_15"><tt>vector-heap-sort</tt> -- vector heap sort</a></h5>
  2119. <p></p>
  2120. <ul>
  2121. <li><p><tt>(vector-heap-sort<i> &lt; vector [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_320"></a>
  2122. </p>
  2123. <li><p><tt>(vector-heap-sort!<i> &lt; vector [start [end]]</i>)</tt><a name="node_idx_322"></a>
  2124. </p>
  2125. </ul><p>
  2126. These procedures sort their data using heap sort,
  2127. which is not a stable sorting algorithm.</p>
  2128. <p>
  2129. <tt>Vector-heap-sort</tt> returns a vector of length <i>end</i> <tt>-</tt> <i>start</i>.
  2130. <tt>Vector-heap-sort!</tt> is in-place, leaving its result in
  2131. <i>vector</i>[<i>start</i>,<i>end</i>).</p>
  2132. <p>
  2133. </p>
  2134. <p>
  2135. </p>
  2136. <p>
  2137. </p>
  2138. <p>
  2139. </p>
  2140. <p>
  2141. </p>
  2142. <p>
  2143. </p>
  2144. <p>
  2145. </p>
  2146. <p>
  2147. </p>
  2148. <p>
  2149. </p>
  2150. <a name="node_sec_Temp_16"></a>
  2151. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_16"><tt>vector-insert-sort</tt> -- vector insertion sort</a></h5>
  2152. <p></p>
  2153. <ul>
  2154. <li><p><tt>(vector-insert-sort<i> &lt; vector [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_324"></a>
  2155. </p>
  2156. <li><p><tt>(vector-insert-sort!<i> &lt; vector [start [end]]</i>)</tt><a name="node_idx_326"></a>
  2157. </p>
  2158. </ul><p>
  2159. These procedures stably sort their data using insertion sort.
  2160. </p>
  2161. <ul>
  2162. <li><p><tt>Vector-insert-sort</tt> returns a vector of length <i>end</i> <tt>-</tt> <i>start</i>.
  2163. </p>
  2164. <li><p><tt>Vector-insert-sort!</tt> is in-place, leaving its result in
  2165. <i>vector</i>[<i>start</i>,<i>end</i>).
  2166. </p>
  2167. </ul><p></p>
  2168. <p>
  2169. </p>
  2170. <a name="node_sec_Temp_17"></a>
  2171. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_17"><tt>delete-neighbor-duplicates</tt> -- list and vector
  2172. delete neighbor duplicates</a></h5>
  2173. <p></p>
  2174. <ul>
  2175. <li><p><tt>(list-delete-neighbor-dups<i> = list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_328"></a>
  2176. </p>
  2177. <li><p><tt>(list-delete-neighbor-dups!<i> = list</i>)&nbsp;-&gt;&nbsp;<i>list</i></tt><a name="node_idx_330"></a>
  2178. </p>
  2179. <li><p><tt>(vector-delete-neighbor-dups<i> = vector [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>vector</i></tt><a name="node_idx_332"></a>
  2180. </p>
  2181. <li><p><tt>(vector-delete-neighbor-dups!<i> = vector [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>end'</i></tt><a name="node_idx_334"></a>
  2182. </p>
  2183. </ul><p>
  2184. These procedures delete adjacent duplicate elements from a list or
  2185. a vector, using a given element-equality procedure = . The first/leftmost
  2186. element of a run of equal elements is the one that survives. The list
  2187. or vector is not otherwise disordered.</p>
  2188. <p>
  2189. These procedures are linear time -- much faster than the <em>O</em>(<em>n</em><sup>2</sup>) general
  2190. duplicate-element deletors that do not assume any ``bunching'' of elements
  2191. (such as the ones provided by SRFI&nbsp;1). If you want to delete duplicate
  2192. elements from a large list or vector, you can sort the elements to bring
  2193. equal items together, then use one of these procedures, for a total time
  2194. of <em>O</em>(<em>n</em>log(<em>n</em>)).</p>
  2195. <p>
  2196. The comparison procedure = passed to these procedures is always
  2197. applied</p>
  2198. <p>
  2199. </p>
  2200. <pre class=verbatim>( = <em>x</em> <em>y</em>)
  2201. </pre><p></p>
  2202. <p>
  2203. where <em>x</em> comes before <em>y</em> in the containing list or vector.
  2204. </p>
  2205. <ul>
  2206. <li><p><tt>List-delete-neighbor-dups</tt> does not alter its input list; its
  2207. answer may share storage with the input list.
  2208. </p>
  2209. <li><p><tt>Vector-delete-neighbor-dups</tt> does not alter its input vector, but
  2210. rather allocates a fresh vector to hold the result.
  2211. </p>
  2212. <li><p><tt>List-delete-neighbor-dups!</tt> is permitted, but not required, to
  2213. mutate its input list in order to construct its answer.
  2214. </p>
  2215. <li><p><tt>Vector-delete-neighbor-dups!</tt> reuses its input vector to hold the
  2216. answer, packing its answer into the index range
  2217. [<i>start</i>,<i>end'</i>), where
  2218. <i>end'</i> is the non-negative exact integer returned as its value. It
  2219. returns <i>end'</i> as its result. The vector is not altered outside the range
  2220. [<i>start</i>,<i>end'</i>).
  2221. </p>
  2222. </ul><p>
  2223. Examples:</p>
  2224. <p>
  2225. </p>
  2226. <pre class=verbatim>(list-delete-neighbor-dups = '(1 1 2 7 7 7 0 -2 -2))
  2227. ===&gt; (1 2 7 0 -2)
  2228. (vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2))
  2229. ===&gt; #(1 2 7 0 -2)
  2230. (vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2) 3 7)
  2231. ===&gt; #(7 0 -2)
  2232. ;; Result left in v[3,9):
  2233. (let ((v (vector 0 0 0 1 1 2 2 3 3 4 4 5 5 6 6)))
  2234. (cons (vector-delete-neighbor-dups! = v 3)
  2235. v))
  2236. ===&gt; (9 . #(0 0 0 1 2 3 4 5 6 4 4 5 5 6 6))
  2237. </pre><p></p>
  2238. <p>
  2239. </p>
  2240. <a name="node_sec_Temp_18"></a>
  2241. <h5><a href="manual-Z-H-2.html#node_toc_node_sec_Temp_18"><tt>binary-searches</tt> -- vector binary search</a></h5>
  2242. <p></p>
  2243. <ul>
  2244. <li><p><tt>(vector-binary-search<i> &lt; elt-&gt;key key vector [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>integer or <tt>#f</tt></i></tt><a name="node_idx_336"></a>
  2245. </p>
  2246. <li><p><tt>(vector-binary-search3<i> compare-proc vector [start [end]]</i>)&nbsp;-&gt;&nbsp;<i>integer or <tt>#f</tt></i></tt><a name="node_idx_338"></a>
  2247. </p>
  2248. </ul><p></p>
  2249. <p>
  2250. <tt>vector-binary-search</tt> searches <i>vector</i> in range
  2251. [<i>start</i>,<i>end</i>) (which default to 0 and the length of
  2252. <i>vector</i>, respectively) for an element whose
  2253. associated key is equal to <i>key</i>. The procedure <i>elt-&gt;key</i> is used to map
  2254. an element to its associated key. The elements of the vector are assumed
  2255. to be ordered by the &lt; relation on these keys. That is, </p>
  2256. <p>
  2257. </p>
  2258. <pre class=verbatim>(vector-sorted? (lambda (x y) (&lt; (<i>elt-&gt;key</i> x) (<i>elt-&gt;key</i> y)))
  2259. <i>vector</i> <i>start</i> <i>end</i>) ===&gt; true
  2260. </pre><p></p>
  2261. <p>
  2262. An element <i>e</i> of <i>vector</i> is a match for <i>key</i> if it's
  2263. neither less nor greater than the key:</p>
  2264. <p>
  2265. </p>
  2266. <pre class=verbatim>(and (not (&lt; (<i>elt-&gt;key</i> <i>e</i>) <i>key</i>))
  2267. (not (&lt; <i>key</i> (<i>elt-&gt;key</i> <i>e</i>))))
  2268. </pre><p></p>
  2269. <p>
  2270. If there is such an element, the procedure returns its index in the
  2271. vector as an exact integer. If there is no such element in the searched
  2272. range, the procedure returns false.</p>
  2273. <p>
  2274. </p>
  2275. <pre class=verbatim>(vector-binary-search &lt; car 4 '#((1 . one) (3 . three)
  2276. (4 . four) (25 . twenty-five)))
  2277. ===&gt; 2
  2278. (vector-binary-search &lt; car 7 '#((1 . one) (3 . three)
  2279. (4 . four) (25 . twenty-five)))
  2280. ===&gt; #f
  2281. </pre><p> </p>
  2282. <p>
  2283. <tt>Vector-binary-search3</tt> is a variant that uses a three-way comparison
  2284. procedure <i>compare-proc</i>. <i>Compare-proc</i> compares its
  2285. parameter to the search key, and returns an
  2286. exact integer whose sign indicates its relationship to the search key.
  2287. </p>
  2288. <div align=center><table><tr><td>
  2289. array<em>r</em><em>c</em><em>l</em><em>c</em><em>r</em><em>c</em><em>l</em>
  2290. (<i>compare-proc</i>&nbsp;<em>x</em>) &amp;&lt;&amp; 0&amp; ==&gt;&amp; <em>x</em> &amp;&lt;&amp; <i>search-key</i><br>
  2291. (<i>compare-proc</i>&nbsp;<em>x</em>) &amp; = &amp; 0&amp; ==&gt;&amp; <em>x</em> &amp; = &amp; <i>search-key</i><br>
  2292. (<i>compare-proc</i>&nbsp;<em>x</em>) &amp;&gt;&amp; 0&amp; ==&gt;&amp; <em>x</em> &amp;&gt;&amp; <i>search-key</i>
  2293. endarray
  2294. </td></tr></table></div><p></p>
  2295. <p>
  2296. </p>
  2297. <pre class=verbatim>(vector-binary-search3 (lambda (elt) (- (car elt) 4))
  2298. '#((1 . one) (3 . three)
  2299. (4 . four) (25 . twenty-five)))
  2300. ===&gt; 2
  2301. </pre><p></p>
  2302. <p>
  2303. </p>
  2304. <p>
  2305. </p>
  2306. <a name="node_sec_5.19.3"></a>
  2307. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.19.3">5.19.3&nbsp;&nbsp;Algorithmic properties</a></h3>
  2308. <p>Different sort and merge algorithms have different properties.
  2309. Choose the algorithm that matches your needs:</p>
  2310. <p>
  2311. </p>
  2312. <dl><dt></dt><dd>
  2313. </dd><dt><b>Vector insert sort</b></dt><dd>
  2314. Stable, but only suitable for small vectors -- <em>O</em>(<em>n</em><sup>2</sup>).
  2315. </dd><dt><b>Vector heap sort</b></dt><dd>
  2316. Not stable. Guaranteed fast -- <em>O</em>(<em>n</em>log(<em>n</em>)) <em>worst</em> case. Poor
  2317. locality on large vectors. A very reliable workhorse.
  2318. </dd><dt><b>Vector merge sort</b></dt><dd>
  2319. Stable. Not in-place -- requires a temporary buffer of equal size.
  2320. Fast -- <em>O</em>(<em>n</em>log(<em>n</em>)) -- and has good memory locality for large vectors.<p>
  2321. The implementation of vector merge sort provided by this
  2322. implementation is, additionally, a ``natural'' sort, meaning that it
  2323. exploits existing order in the input data, providing <em>O</em>(<em>n</em>) best case.
  2324. </p>
  2325. </dd><dt><b>Destructive list merge sort</b></dt><dd>
  2326. Stable, fast and in-place (i.e., allocates no new cons cells). ``Fast''
  2327. means <em>O</em>(<em>n</em>log(<em>n</em>)) worse-case, and substantially better if the data
  2328. is already mostly ordered, all the way down to linear time for
  2329. a completely-ordered input list (i.e., it is a ``natural'' sort).<p>
  2330. Note that sorting lists involves chasing pointers through memory, which
  2331. can be a loser on modern machine architectures because of poor cache and
  2332. page locality.
  2333. Sorting vectors has inherently better locality.</p>
  2334. <p>
  2335. This implementation's destructive list merge and merge sort
  2336. implementations are opportunistic -- they avoid redundant
  2337. <tt>set-cdr!</tt>s, and try to take long
  2338. already-ordered runs of list structure as-is when doing the merges.
  2339. </p>
  2340. </dd><dt><b>Pure list merge sort</b></dt><dd>
  2341. Stable and fast -- <em>O</em>(<em>n</em>log(<em>n</em>)) worst-case, and possibly <em>O</em>(<em>n</em>),
  2342. depending upon the input list (see discussion above).
  2343. </dd></dl><p></p>
  2344. <p>
  2345. </p>
  2346. <div align=center><table><tr><td>
  2347. <table border=0><tr><td valign=top >Algorithm </td><td valign=top >Stable? </td><td valign=top >Worst case </td><td valign=top >Average case </td><td valign=top >In-place</td></tr>
  2348. <tr><td valign=top >Vector insert </td><td valign=top >Yes</td><td valign=top ><em>O</em>(<em>n</em><sup>2</sup>) </td><td valign=top ><em>O</em>(<em>n</em><sup>2</sup>)</td><td valign=top >Yes</td></tr>
  2349. <tr><td valign=top >Vector quick </td><td valign=top >No </td><td valign=top ><em>O</em>(<em>n</em><sup>2</sup>) </td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >Yes</td></tr>
  2350. <tr><td valign=top >Vector heap </td><td valign=top >No </td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >Yes</td></tr>
  2351. <tr><td valign=top >Vector merge </td><td valign=top >Yes</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >No</td></tr>
  2352. <tr><td valign=top >List merge </td><td valign=top >Yes</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >Either
  2353. </td></tr></table>
  2354. </td></tr></table></div>
  2355. <p>
  2356. </p>
  2357. <a name="node_sec_5.20"></a>
  2358. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.20">5.20&nbsp;&nbsp;Regular expressions</a></h2>
  2359. <p></p>
  2360. <p>
  2361. This section describes a functional interface for building regular
  2362. expressions and matching them against strings.
  2363. The matching is done using the POSIX regular expression package.
  2364. Regular expressions are in the structure <tt>regexps</tt>.</p>
  2365. <p>
  2366. A regular expression is either a character set, which matches any character
  2367. in the set, or a composite expression containing one or more subexpressions.
  2368. A regular expression can be matched against a string to determine success
  2369. or failure, and to determine the substrings matched by particular subexpressions.</p>
  2370. <p>
  2371. </p>
  2372. <a name="node_sec_5.20.1"></a>
  2373. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.20.1">5.20.1&nbsp;&nbsp;Character sets</a></h3>
  2374. <p>Character sets may be defined using a list of characters and strings,
  2375. using a range or ranges of characters, or by using set operations on
  2376. existing character sets.</p>
  2377. <p>
  2378. </p>
  2379. <ul>
  2380. <li><p><tt>(set<i> character-or-string <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_340"></a>
  2381. </p>
  2382. <li><p><tt>(range<i> low-char high-char</i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_342"></a>
  2383. </p>
  2384. <li><p><tt>(ranges<i> low-char high-char <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_344"></a>
  2385. </p>
  2386. <li><p><tt>(ascii-range<i> low-char high-char</i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_346"></a>
  2387. </p>
  2388. <li><p><tt>(ascii-ranges<i> low-char high-char <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_348"></a>
  2389. </p>
  2390. </ul><p>
  2391. <tt>Set</tt> returns a set that contains the character arguments and the
  2392. characters in any string arguments. <tt>Range</tt> returns a character
  2393. set that contain all characters between <i>low-char</i> and <i>high-char</i>,
  2394. inclusive. <tt>Ranges</tt> returns a set that contains all characters in
  2395. the given ranges. <tt>Range</tt> and <tt>ranges</tt> use the ordering induced by
  2396. <tt>char-&gt;integer</tt>. <tt>Ascii-range</tt> and <tt>ascii-ranges</tt> use the
  2397. ASCII ordering.
  2398. It is an error for a <i>high-char</i> to be less than the preceding
  2399. <i>low-char</i> in the appropriate ordering.</p>
  2400. <p>
  2401. </p>
  2402. <ul>
  2403. <li><p><tt>(negate<i> char-set</i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_350"></a>
  2404. </p>
  2405. <li><p><tt>(intersection<i> char-set char-set</i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_352"></a>
  2406. </p>
  2407. <li><p><tt>(union<i> char-set char-set</i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_354"></a>
  2408. </p>
  2409. <li><p><tt>(subtract<i> char-set char-set</i>)&nbsp;-&gt;&nbsp;<i>char-set</i></tt><a name="node_idx_356"></a>
  2410. </p>
  2411. </ul><p>
  2412. These perform the indicated operations on character sets.</p>
  2413. <p>
  2414. The following character sets are predefined:
  2415. </p>
  2416. <div align=center><table><tr><td>
  2417. <table border=0><tr><td valign=top ><tt>lower-case</tt> </td><td valign=top ><tt>(set &quot;abcdefghijklmnopqrstuvwxyz&quot;)</tt> </td></tr>
  2418. <tr><td valign=top ><tt>upper-case</tt> </td><td valign=top ><tt>(set &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;)</tt> </td></tr>
  2419. <tr><td valign=top ><tt>alphabetic</tt> </td><td valign=top ><tt>(union lower-case upper-case)</tt> </td></tr>
  2420. <tr><td valign=top ><tt>numeric</tt> </td><td valign=top ><tt>(set &quot;0123456789&quot;)</tt> </td></tr>
  2421. <tr><td valign=top ><tt>alphanumeric</tt> </td><td valign=top ><tt>(union alphabetic numeric)</tt> </td></tr>
  2422. <tr><td valign=top ><tt>punctuation</tt> </td><td valign=top ><tt>(set &quot;</tt><code class=verbatim>!\&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\\]^_`{|}~</code><tt>&quot;)</tt> </td></tr>
  2423. <tr><td valign=top ><tt>graphic</tt> </td><td valign=top ><tt>(union alphanumeric punctuation)</tt> </td></tr>
  2424. <tr><td valign=top ><tt>printing</tt> </td><td valign=top ><tt>(union graphic (set #</tt><code class=verbatim>\</code><tt>space))</tt> </td></tr>
  2425. <tr><td valign=top ><tt>control</tt> </td><td valign=top ><tt>(negate printing)</tt> </td></tr>
  2426. <tr><td valign=top ><tt>blank</tt> </td><td valign=top ><tt>(set #</tt><code class=verbatim>\</code><tt>space (ascii-&gt;char 9))</tt> ; 9 is tab </td></tr>
  2427. <tr><td valign=top ><tt>whitespace</tt> </td><td valign=top ><tt>(union (set #</tt><code class=verbatim>\</code><tt>space) (ascii-range 9 13))</tt> </td></tr>
  2428. <tr><td valign=top ><tt>hexdigit</tt> </td><td valign=top ><tt>(set &quot;0123456789abcdefABCDEF&quot;)</tt> </td></tr>
  2429. <tr><td valign=top ></td></tr></table></td></tr></table></div>
  2430. The above are taken from the default locale in POSIX.
  2431. The characters in <tt>whitespace</tt> are <i>space</i>, <i>tab</i>,
  2432. <i>newline</i> (= <i>line feed</i>), <i>vertical tab</i>, <i>form feed</i>, and
  2433. <i>carriage return</i>.<p>
  2434. </p>
  2435. <a name="node_sec_5.20.2"></a>
  2436. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.20.2">5.20.2&nbsp;&nbsp;Anchoring</a></h3>
  2437. <p></p>
  2438. <ul>
  2439. <li><p><tt>(string-start<i></i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_358"></a>
  2440. </p>
  2441. <li><p><tt>(string-end<i></i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_360"></a>
  2442. </p>
  2443. </ul><p>
  2444. <tt>String-start</tt> returns a regular expression that matches the beginning
  2445. of the string being matched against; string-end returns one that matches
  2446. the end.</p>
  2447. <p>
  2448. </p>
  2449. <a name="node_sec_5.20.3"></a>
  2450. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.20.3">5.20.3&nbsp;&nbsp;Composite expressions</a></h3>
  2451. <p></p>
  2452. <ul>
  2453. <li><p><tt>(sequence<i> reg-exp <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_362"></a>
  2454. </p>
  2455. <li><p><tt>(one-of<i> reg-exp <tt>...</tt></i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_364"></a>
  2456. </p>
  2457. </ul><p>
  2458. <tt>Sequence</tt> matches the concatenation of its arguments, <tt>one-of</tt> matches
  2459. any one of its arguments.</p>
  2460. <p>
  2461. </p>
  2462. <ul>
  2463. <li><p><tt>(text<i> string</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_366"></a>
  2464. </p>
  2465. </ul><p>
  2466. <tt>Text</tt> returns a regular expression that matches the characters in
  2467. <i>string</i>, in order.</p>
  2468. <p>
  2469. </p>
  2470. <ul>
  2471. <li><p><tt>(repeat<i> reg-exp</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_368"></a>
  2472. </p>
  2473. <li><p><tt>(repeat<i> count reg-exp</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_370"></a>
  2474. </p>
  2475. <li><p><tt>(repeat<i> min max reg-exp</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_372"></a>
  2476. </p>
  2477. </ul><p>
  2478. <tt>Repeat</tt> returns a regular expression that matches zero or more
  2479. occurences of its <i>reg-exp</i> argument. With no count the result
  2480. will match any number of times (<i>reg-exp</i>*). With a single
  2481. count the returned expression will match
  2482. <i>reg-exp</i> exactly that number of times.
  2483. The final case will match from <i>min</i> to <i>max</i>
  2484. repetitions, inclusive.
  2485. <i>Max</i> may be <tt>#f</tt>, in which case there
  2486. is no maximum number of matches.
  2487. <i>Count</i> and <i>min</i> should be exact, non-negative integers;
  2488. <i>max</i> should either be an exact non-negative integer or <tt>#f</tt>.</p>
  2489. <p>
  2490. </p>
  2491. <a name="node_sec_5.20.4"></a>
  2492. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.20.4">5.20.4&nbsp;&nbsp;Case sensitivity</a></h3>
  2493. <p>Regular expressions are normally case-sensitive.
  2494. </p>
  2495. <ul>
  2496. <li><p><tt>(ignore-case<i> reg-exp</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_374"></a>
  2497. </p>
  2498. <li><p><tt>(use-case<i> reg-exp</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_376"></a>
  2499. </p>
  2500. </ul><p>
  2501. The value returned by
  2502. <tt>ignore-case</tt> is identical its argument except that case will be
  2503. ignored when matching.
  2504. The value returned by <tt>use-case</tt> is protected
  2505. from future applications of <tt>ignore-case</tt>.
  2506. The expressions returned
  2507. by <tt>use-case</tt> and <tt>ignore-case</tt> are unaffected by later uses of the
  2508. these procedures.
  2509. By way of example, the following matches <tt>&quot;ab&quot;</tt> but not <tt>&quot;aB&quot;</tt>,
  2510. <tt>&quot;Ab&quot;</tt>, or <tt>&quot;AB&quot;</tt>.
  2511. </p>
  2512. <pre class=verbatim><tt>(text &quot;ab&quot;)</tt>
  2513. </pre><p>
  2514. while
  2515. </p>
  2516. <pre class=verbatim><tt>(ignore-case (test &quot;ab&quot;))</tt>
  2517. </pre><p>
  2518. matches <tt>&quot;ab&quot;</tt>, <tt>&quot;aB&quot;</tt>,
  2519. <tt>&quot;Ab&quot;</tt>, and <tt>&quot;AB&quot;</tt> and
  2520. </p>
  2521. <pre class=verbatim>(ignore-case (sequence (text &quot;a&quot;)
  2522. (use-case (text &quot;b&quot;))))
  2523. </pre><p>
  2524. matches <tt>&quot;ab&quot;</tt> and <tt>&quot;Ab&quot;</tt> but not <tt>&quot;aB&quot;</tt> or <tt>&quot;AB&quot;</tt>.</p>
  2525. <p>
  2526. </p>
  2527. <a name="node_sec_5.20.5"></a>
  2528. <h3><a href="manual-Z-H-2.html#node_toc_node_sec_5.20.5">5.20.5&nbsp;&nbsp;Submatches and matching</a></h3>
  2529. <p>A subexpression within a larger expression can be marked as a submatch.
  2530. When an expression is matched against a string, the success or failure
  2531. of each submatch within that expression is reported, as well as the
  2532. location of the substring matched be each successful submatch.</p>
  2533. <p>
  2534. </p>
  2535. <ul>
  2536. <li><p><tt>(submatch<i> key reg-exp</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_378"></a>
  2537. </p>
  2538. <li><p><tt>(no-submatches<i> reg-exp</i>)&nbsp;-&gt;&nbsp;<i>reg-exp</i></tt><a name="node_idx_380"></a>
  2539. </p>
  2540. </ul><p>
  2541. <tt>Submatch</tt> returns a regular expression that matches its argument and
  2542. causes the result of matching its argument to be reported by the <tt>match</tt>
  2543. procedure.
  2544. <i>Key</i> is used to indicate the result of this particular submatch
  2545. in the alist of successful submatches returned by <tt>match</tt>.
  2546. Any value may be used as a <i>key</i>.
  2547. <tt>No-submatches</tt> returns an expression identical to its
  2548. argument, except that all submatches have been elided.</p>
  2549. <p>
  2550. </p>
  2551. <ul>
  2552. <li><p><tt>(any-match?<i> reg-exp string</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_382"></a>
  2553. </p>
  2554. <li><p><tt>(exact-match?<i> reg-exp string</i>)&nbsp;-&gt;&nbsp;<i>boolean</i></tt><a name="node_idx_384"></a>
  2555. </p>
  2556. <li><p><tt>(match<i> reg-exp string</i>)&nbsp;-&gt;&nbsp;<i>match or <tt>#f</tt></i></tt><a name="node_idx_386"></a>
  2557. </p>
  2558. <li><p><tt>(match-start<i> match</i>)&nbsp;-&gt;&nbsp;<i>index</i></tt><a name="node_idx_388"></a>
  2559. </p>
  2560. <li><p><tt>(match-end<i> match</i>)&nbsp;-&gt;&nbsp;<i>index</i></tt><a name="node_idx_390"></a>
  2561. </p>
  2562. <li><p><tt>(match-submatches<i> match</i>)&nbsp;-&gt;&nbsp;<i>alist</i></tt><a name="node_idx_392"></a>
  2563. </p>
  2564. </ul><p>
  2565. <tt>Any-match?</tt> returns <tt>#t</tt> if <i>string</i> matches <i>reg-exp</i> or
  2566. contains a substring that does, and <tt>#f</tt> otherwise.
  2567. <tt>Exact-match?</tt> returns <tt>#t</tt> if <i>string</i> matches
  2568. <i>reg-exp</i> and <tt>#f</tt> otherwise.</p>
  2569. <p>
  2570. <tt>Match</tt> returns <tt>#f</tt> if <i>reg-exp</i> does not match <i>string</i>
  2571. and a match record if it does match.
  2572. A match record contains three values: the beginning and end of the substring
  2573. that matched
  2574. the pattern and an a-list of submatch keys and corresponding match records
  2575. for any submatches that also matched.
  2576. <tt>Match-start</tt> returns the index of
  2577. the first character in the matching substring and <tt>match-end</tt> gives index
  2578. of the first character after the matching substring.
  2579. <tt>Match-submatches</tt> returns an alist of submatch keys and match records.
  2580. Only the top match record returned by <tt>match</tt> has a submatch alist.</p>
  2581. <p>
  2582. Matching occurs according to POSIX.
  2583. The match returned is the one with the lowest starting index in <i>string</i>.
  2584. If there is more than one such match, the longest is returned.
  2585. Within that match the longest possible submatches are returned.</p>
  2586. <p>
  2587. All three matching procedures cache a compiled version of <i>reg-exp</i>.
  2588. Subsequent calls with the same <i>reg-exp</i> will be more efficient.</p>
  2589. <p>
  2590. The C interface to the POSIX regular expression code uses ASCII <tt>nul</tt>
  2591. as an end-of-string marker.
  2592. The matching procedures will ignore any characters following an
  2593. embedded ASCII <tt>nul</tt>s in <i>string</i>.</p>
  2594. <p>
  2595. </p>
  2596. <pre class=verbatim>(define pattern (text &quot;abc&quot;))
  2597. (any-match? pattern &quot;abc&quot;) $$#t
  2598. (any-match? pattern &quot;abx&quot;) $$#f
  2599. (any-match? pattern &quot;xxabcxx&quot;) $$#t
  2600. (exact-match? pattern &quot;abc&quot;) $$#t
  2601. (exact-match? pattern &quot;abx&quot;) $$#f
  2602. (exact-match? pattern &quot;xxabcxx&quot;) $$#f
  2603. (match pattern &quot;abc&quot;) $$(#{match 0 3})
  2604. (match pattern &quot;abx&quot;) $$#f
  2605. (match pattern &quot;xxabcxx&quot;) $$(#{match 2 5})
  2606. (let ((x (match (sequence (text &quot;ab&quot;)
  2607. (submatch 'foo (text &quot;cd&quot;))
  2608. (text &quot;ef&quot;))
  2609. &quot;xxxabcdefxx&quot;)))
  2610. (list x (match-submatches x)))
  2611. $$(#{match 3 9} ((foo . #{match 5 7}))
  2612. (match-submatches
  2613. (match (sequence
  2614. (set &quot;a&quot;)
  2615. (one-of (submatch 'foo (text &quot;bc&quot;))
  2616. (submatch 'bar (text &quot;BC&quot;))))
  2617. &quot;xxxaBCd&quot;))
  2618. $$((bar . #{match 4 6}))
  2619. </pre><p></p>
  2620. <p>
  2621. </p>
  2622. <a name="node_sec_5.21"></a>
  2623. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_5.21">5.21&nbsp;&nbsp;SRFIs</a></h2>
  2624. <p>`SRFI' stands for `Scheme Request For Implementation'.
  2625. An SRFI is a description of an extension to standard Scheme.
  2626. Draft and final SRFI documents, a FAQ, and other information about SRFIs
  2627. can be found at
  2628. <a href="http://srfi.schemers.org">the SRFI web site</a>.</p>
  2629. <p>
  2630. Scheme&nbsp;48 includes implementations of the following (final) SRFIs:
  2631. </p>
  2632. <ul>
  2633. <li><p>SRFI 1 - List Library
  2634. </p>
  2635. <li><p>SRFI 2 - <tt>and-let*</tt>
  2636. </p>
  2637. <li><p>SRFI 5 - <tt>let</tt> with signatures and rest arguments
  2638. </p>
  2639. <li><p>SRFI 6 - Basic string ports
  2640. </p>
  2641. <li><p>SRFI 7 - Program configuration
  2642. </p>
  2643. <li><p>SRFI 8 - <tt>receive</tt>
  2644. </p>
  2645. <li><p>SRFI 9 - Defining record types
  2646. </p>
  2647. <li><p>SRFI 11 - Syntax for receiving multiple values
  2648. </p>
  2649. <li><p>SRFI 13 - String Library
  2650. </p>
  2651. <li><p>SRFI 14 - Character-Set Library (see note below)
  2652. </p>
  2653. <li><p>SRFI 16 - Syntax for procedures of variable arity
  2654. </p>
  2655. <li><p>SRFI 17 - Generalized <tt>set!</tt>
  2656. </p>
  2657. <li><p>SRFI 22 - Running Scheme Scripts on Unix
  2658. </p>
  2659. <li><p>SRFI 23 - Error reporting mechanism
  2660. </p>
  2661. <li><p>SRFI 25 - Multi-dimensional Array Primitives
  2662. </p>
  2663. <li><p>SRFI 26 - Notation for Specializing Parameters without Currying
  2664. </p>
  2665. <li><p>SRFI 27 - Sources of Random Bits
  2666. </p>
  2667. <li><p>SRFI 28 - Basic Format Strings
  2668. </p>
  2669. <li><p>SRFI 31 - A special form <tt>rec</tt> for recursive evaluation
  2670. </p>
  2671. <li><p>SRFI 34 - Exception Handling for Programs
  2672. </p>
  2673. <li><p>SRFI 35 - Conditions
  2674. </p>
  2675. <li><p>SRFI 36 - I/O Conditions
  2676. </p>
  2677. <li><p>SRFI 37 - args-fold: a program argument processor
  2678. </p>
  2679. <li><p>SRFI 42 - Eager Comprehensions
  2680. </p>
  2681. <li><p>SRFI 45 - Primitives for Expressing Iterative Lazy Algorithms
  2682. </p>
  2683. </ul><p>
  2684. Documentation on these can be found at the web site mentioned above.</p>
  2685. <p>
  2686. SRFI&nbsp;14 includes the procedure <tt>-&gt;char-set</tt> which is not a standard
  2687. Scheme identifier (in R<sup>5</sup>RS the only required identifier starting
  2688. with <tt>-</tt> is <tt>-</tt> itself).
  2689. In the Scheme&nbsp;48 version of SRFI&nbsp;14 we have renamed <tt>-&gt;char-set</tt>
  2690. as <tt>x-&gt;char-set</tt>.</p>
  2691. <p>
  2692. The SRFI bindings can be accessed either by opening the appropriate structure
  2693. (the structure <tt>srfi-</tt><i>n</i> contains SRFI <i>n</i>)
  2694. or by loading structure <tt>srfi-7</tt> and then using
  2695. the <tt>,load-srfi-7-program</tt> command to load an SRFI 7-style program.
  2696. The syntax for the command is
  2697. </p>
  2698. <pre class=verbatim><tt>,load-srfi-7-program <i>name</i> <i>filename</i></tt>
  2699. </pre><p>
  2700. This creates a new structure and associated package, binds the structure
  2701. to <i>name</i> in the configuration package, and then loads the program
  2702. found in <i>filename</i> into the package.</p>
  2703. <p>
  2704. As an example, if the file <tt>test.scm</tt> contains
  2705. </p>
  2706. <pre class=verbatim>(program (code (define x 10)))
  2707. </pre><p>
  2708. this program can be loaded as follows:
  2709. </p>
  2710. <pre class=verbatim>&gt; ,load-package srfi-7
  2711. &gt; ,load-srfi-7-program test test.scm
  2712. [test]
  2713. &gt; ,in test
  2714. test&gt; x
  2715. 10
  2716. test&gt;
  2717. </pre><p></p>
  2718. <p>
  2719. </p>
  2720. <div align=right class=navigation><i>[Go to <span><a href="manual.html">first</a>, <a href="manual-Z-H-6.html">previous</a></span><span>, <a href="manual-Z-H-8.html">next</a></span> page<span>; &nbsp;&nbsp;</span><span><a href="manual-Z-H-2.html#node_toc_start">contents</a></span><span><span>; &nbsp;&nbsp;</span><a href="manual-Z-H-13.html#node_index_start">index</a></span>]</i></div>
  2721. </div>
  2722. </body>
  2723. </html>