My various dotfiles

chap-6.texi 98KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867
  1. @node Iteration, Objects, Data and Control Flow, Top
  2. @chapter Iteration
  3. @menu
  4. * The LOOP Facility::
  5. * Iteration Dictionary::
  6. @end menu
  7. @node The LOOP Facility, Iteration Dictionary, Iteration, Iteration
  8. @section The LOOP Facility
  9. @c including concept-loop
  10. @menu
  11. * Overview of the Loop Facility::
  12. * Variable Initialization and Stepping Clauses::
  13. * Value Accumulation Clauses::
  14. * Termination Test Clauses::
  15. * Unconditional Execution Clauses::
  16. * Conditional Execution Clauses::
  17. * Miscellaneous Clauses::
  18. * Examples of Miscellaneous Loop Features::
  19. * Notes about Loop::
  20. @end menu
  21. @node Overview of the Loop Facility, Variable Initialization and Stepping Clauses, The LOOP Facility, The LOOP Facility
  22. @subsection Overview of the Loop Facility
  23. The @b{loop} @i{macro} performs iteration.
  24. @menu
  25. * Simple vs Extended Loop::
  26. * Simple Loop::
  27. * Extended Loop::
  28. * Loop Keywords::
  29. * Parsing Loop Clauses::
  30. * Expanding Loop Forms::
  31. * Summary of Loop Clauses::
  32. * Summary of Variable Initialization and Stepping Clauses::
  33. * Summary of Value Accumulation Clauses::
  34. * Summary of Termination Test Clauses::
  35. * Summary of Unconditional Execution Clauses::
  36. * Summary of Conditional Execution Clauses::
  37. * Summary of Miscellaneous Clauses::
  38. * Order of Execution::
  39. * Destructuring::
  40. * Restrictions on Side-Effects::
  41. @end menu
  42. @node Simple vs Extended Loop, Simple Loop, Overview of the Loop Facility, Overview of the Loop Facility
  43. @subsubsection Simple vs Extended Loop
  44. @b{loop} @i{forms} are partitioned into two categories:
  45. simple @b{loop} @i{forms}
  46. and extended @b{loop} @i{forms}.
  47. @node Simple Loop, Extended Loop, Simple vs Extended Loop, Overview of the Loop Facility
  48. @subsubsection Simple Loop
  49. A simple @b{loop} @i{form} is one that has a body containing
  50. only @i{compound forms}.
  51. Each @i{form} is @i{evaluated} in turn from left to right.
  52. When the last @i{form} has been @i{evaluated},
  53. then the first @i{form} is evaluated again, and so on, in a never-ending cycle.
  54. A simple @b{loop} @i{form} establishes an @i{implicit block} named @b{nil}.
  55. The execution of a simple @b{loop} can be terminated by explicitly
  56. transfering control to the @i{implicit block} (using @b{return} or
  57. @b{return-from}) or to some @i{exit point} outside of the @i{block}
  58. (@i{e.g.}, using @b{throw}, @b{go}, or @b{return-from}).
  59. @node Extended Loop, Loop Keywords, Simple Loop, Overview of the Loop Facility
  60. @subsubsection Extended Loop
  61. An extended @b{loop} @i{form} is one that has a body containing
  62. @i{atomic} @i{expressions}. When the @b{loop} @i{macro} processes such a
  63. @i{form}, it invokes a facility that is commonly called ``the Loop Facility.''
  64. The Loop Facility provides standardized access to mechanisms commonly used
  65. in iterations through Loop schemas, which are introduced by @i{loop keywords}.
  66. The body of an extended @b{loop} @i{form} is divided into @b{loop} clauses,
  67. each which is in turn made up of @i{loop keywords} and @i{forms}.
  68. @node Loop Keywords, Parsing Loop Clauses, Extended Loop, Overview of the Loop Facility
  69. @subsubsection Loop Keywords
  70. @i{Loop keywords} are not true @i{keywords}_1;
  71. they are special @i{symbols}, recognized by @i{name} rather than @i{object} identity,
  72. that are meaningful only to the @b{loop} facility.
  73. A @i{loop keyword} is a @i{symbol} but is recognized by its @i{name}
  74. (not its identity), regardless of the @i{packages} in which it is @i{accessible}.
  75. In general, @i{loop keywords} are not @i{external symbols} of the @t{COMMON-LISP} @i{package},
  76. except in the coincidental situation that a @i{symbol} with the same name as a
  77. @i{loop keyword} was needed for some other purpose in @r{Common Lisp}. For example,
  78. there is a @i{symbol} in the @t{COMMON-LISP} @i{package} whose @i{name} is @t{"UNLESS"} but
  79. not one whose @i{name} is @t{"UNTIL"}.
  80. If no @i{loop keywords} are supplied in a @b{loop} @i{form},
  81. the Loop Facility executes the loop body repeatedly; see @ref{Simple Loop}.
  82. @node Parsing Loop Clauses, Expanding Loop Forms, Loop Keywords, Overview of the Loop Facility
  83. @subsubsection Parsing Loop Clauses
  84. The syntactic parts of an extended @b{loop} @i{form} are called clauses;
  85. the rules for parsing are determined by
  86. that clause's keyword.
  87. The following example shows a @b{loop} @i{form} with six clauses:
  88. @example
  89. (loop for i from 1 to (compute-top-value) ; first clause
  90. while (not (unacceptable i)) ; second clause
  91. collect (square i) ; third clause
  92. do (format t "Working on ~D now" i) ; fourth clause
  93. when (evenp i) ; fifth clause
  94. do (format t "~D is a non-odd number" i)
  95. finally (format t "About to exit!")) ; sixth clause
  96. @end example
  97. Each @i{loop keyword} introduces
  98. either a compound loop clause or a simple loop clause
  99. that can consist of a @i{loop keyword} followed by a single @i{form}.
  100. The number of @i{forms} in a clause is determined by the @i{loop keyword}
  101. that begins the clause and by the auxiliary keywords in the clause.
  102. The keywords @t{do},
  103. @t{doing},
  104. @t{initially}, and @t{finally}
  105. are the only loop keywords that can take any number of @i{forms} and
  106. group them as an @i{implicit progn}.
  107. Loop clauses can contain auxiliary keywords, which are sometimes
  108. called prepositions. For example, the first clause in the code
  109. above includes the prepositions @t{from} and @t{to},
  110. which mark the value from which stepping begins and the value at which stepping
  111. ends.
  112. For detailed information about @b{loop} syntax,
  113. see the @i{macro} @b{loop}.
  114. @node Expanding Loop Forms, Summary of Loop Clauses, Parsing Loop Clauses, Overview of the Loop Facility
  115. @subsubsection Expanding Loop Forms
  116. A @b{loop} @i{macro form} expands into a @i{form} containing
  117. one or more binding forms (that @i{establish} @i{bindings} of loop variables)
  118. and a @b{block} and a @b{tagbody} (that express a looping control
  119. structure). The variables established in @b{loop} are bound as
  120. if by @b{let} or @b{lambda}.
  121. Implementations can interleave the setting of initial values with the @i{bindings}.
  122. However, the assignment of the initial values is always calculated in the order
  123. specified by the user. A variable is thus sometimes bound to a meaningless value
  124. of the correct @i{type}, and then later in the prologue it is set to the true
  125. initial value by using @b{setq}.
  126. One implication of this interleaving is that it is @i{implementation-dependent}
  127. whether the @i{lexical environment} in which the initial value @i{forms}
  128. (variously called the @i{form1}, @i{form2}, @i{form3}, @i{step-fun},
  129. @i{vector}, @i{hash-table}, and @i{package}) in any @i{for-as-subclause},
  130. except @i{for-as-equals-then},
  131. are @i{evaluated} includes only the loop variables preceding that @i{form}
  132. or includes more or all of the loop variables;
  133. the @i{form1} and @i{form2} in a @i{for-as-equals-then} form
  134. includes the @i{lexical environment} of all the loop variables.
  135. After the @i{form} is expanded, it consists of three basic parts in the
  136. @b{tagbody}:
  137. the loop prologue,
  138. the loop body,
  139. and the loop epilogue.
  140. @table @asis
  141. @item @b{Loop prologue}
  142. The loop prologue contains @i{forms}
  143. that are executed before iteration begins, such as
  144. any automatic variable initializations prescribed
  145. by the @i{variable} clauses, along with any @t{initially} clauses
  146. in the order they appear in the source.
  147. @item @b{Loop body}
  148. The loop body contains those @i{forms} that are executed during iteration,
  149. including application-specific calculations, termination tests,
  150. and variable @i{stepping}_1.
  151. @item @b{Loop epilogue}
  152. The loop epilogue contains @i{forms} that are executed after iteration
  153. terminates, such as @t{finally} clauses, if any, along
  154. with any implicit return value from an @i{accumulation} clause or
  155. an @i{termination-test} clause.
  156. @end table
  157. Some clauses from the source @i{form}
  158. contribute code only to the loop prologue; these clauses must
  159. come before other clauses that are in the main body of the @b{loop} form.
  160. Others contribute code only to the loop epilogue.
  161. All other clauses contribute to the final
  162. translated @i{form} in the same
  163. order given in the original source @i{form} of the @b{loop}.
  164. Expansion of the @b{loop} macro produces an @i{implicit block} named @b{nil}
  165. unless @t{named} is supplied.
  166. Thus, @b{return-from} (and sometimes @b{return})
  167. can be used to return values from @b{loop} or to exit @b{loop}.
  168. @node Summary of Loop Clauses, Summary of Variable Initialization and Stepping Clauses, Expanding Loop Forms, Overview of the Loop Facility
  169. @subsubsection Summary of Loop Clauses
  170. Loop clauses fall into one of the following categories:
  171. @node Summary of Variable Initialization and Stepping Clauses, Summary of Value Accumulation Clauses, Summary of Loop Clauses, Overview of the Loop Facility
  172. @subsubsection Summary of Variable Initialization and Stepping Clauses
  173. The @t{for} and @t{as} constructs provide iteration control clauses
  174. that establish a variable to be initialized.
  175. @t{for} and @t{as} clauses can be combined with the loop
  176. keyword @t{and} to get @i{parallel} initialization and @i{stepping}_1.
  177. Otherwise, the initialization and @i{stepping}_1 are @i{sequential}.
  178. The @t{with} construct is similar to a single @b{let} clause.
  179. @t{with} clauses can be combined using the @i{loop keyword} @t{and}
  180. to get @i{parallel} initialization.
  181. For more information, see @ref{Variable Initialization and Stepping Clauses}.
  182. @node Summary of Value Accumulation Clauses, Summary of Termination Test Clauses, Summary of Variable Initialization and Stepping Clauses, Overview of the Loop Facility
  183. @subsubsection Summary of Value Accumulation Clauses
  184. The @t{collect} (or @t{collecting}) construct
  185. takes one @i{form} in its clause
  186. and adds the value of that @i{form} to the end of a @i{list}
  187. of values. By default, the @i{list} of values is returned
  188. when the @b{loop} finishes.
  189. The @t{append} (or @t{appending}) construct
  190. takes one @i{form} in its clause
  191. and appends the value of that @i{form} to the end of a @i{list}
  192. of values. By default, the @i{list} of values is returned when the
  193. @b{loop} finishes.
  194. The @t{nconc} (or @t{nconcing}) construct
  195. is similar to the @t{append} construct,
  196. but its @i{list} values are concatenated as if by the function
  197. @t{nconc}. By default, the @i{list} of values is returned when
  198. the @b{loop} finishes.
  199. The @t{sum} (or @t{summing}) construct
  200. takes one @i{form} in its clause
  201. that must evaluate to a @i{number} and accumulates the sum of all these
  202. @i{numbers}. By default, the cumulative sum is returned when the
  203. @b{loop} finishes.
  204. The @t{count} (or @t{counting}) construct
  205. takes one @i{form} in its clause
  206. and counts the number of times that the @i{form} evaluates to @i{true}.
  207. By default, the count is returned when the @b{loop} finishes.
  208. The @t{minimize} (or @t{minimizing}) construct
  209. takes one @i{form} in its clause
  210. and determines the minimum value obtained by evaluating that @i{form}.
  211. By default, the minimum value is returned when the @b{loop} finishes.
  212. The @t{maximize} (or @t{maximizing}) construct
  213. takes one @i{form} in its clause
  214. and determines the maximum value obtained by evaluating that @i{form}.
  215. By default, the maximum value is returned when the @b{loop} finishes.
  216. For more information, see @ref{Value Accumulation Clauses}.
  217. @node Summary of Termination Test Clauses, Summary of Unconditional Execution Clauses, Summary of Value Accumulation Clauses, Overview of the Loop Facility
  218. @subsubsection Summary of Termination Test Clauses
  219. The @t{for} and @t{as} constructs provide a termination test
  220. that is determined by the iteration control clause.
  221. The @t{repeat} construct causes termination after a specified
  222. number of iterations.
  223. (It uses an internal variable to keep track of the number of iterations.)
  224. The @t{while} construct takes one @i{form}, a @i{test},
  225. and terminates the iteration if the @i{test} evaluates to @i{false}.
  226. A @t{while} clause is equivalent to the expression
  227. @t{(if (not @i{test}) (loop-finish))}.
  228. The @t{until} construct is the inverse of @t{while};
  229. it terminates the iteration if the @i{test} evaluates to
  230. any @i{non-nil} value.
  231. An @t{until} clause is equivalent to the expression
  232. @t{(if @i{test} (loop-finish))}.
  233. The @t{always} construct takes one @i{form} and
  234. terminates the @b{loop} if the @i{form} ever evaluates to @i{false};
  235. in this case, the @b{loop} @i{form} returns @b{nil}.
  236. Otherwise, it provides a default return value of @b{t}.
  237. The @t{never} construct takes one @i{form} and
  238. terminates the @b{loop} if the @i{form} ever evaluates to @i{true};
  239. in this case, the @b{loop} @i{form} returns @b{nil}.
  240. Otherwise, it provides a default return value of @b{t}.
  241. The @t{thereis} construct takes one @i{form} and
  242. terminates the @b{loop} if the @i{form} ever evaluates to
  243. a @i{non-nil} @i{object};
  244. in this case, the @b{loop} @i{form} returns that @i{object}.
  245. Otherwise, it provides a default return value of @b{nil}.
  246. If multiple termination test clauses are specified,
  247. the @b{loop} @i{form} terminates if any are satisfied.
  248. For more information, see @ref{Termination Test Clauses}.
  249. @node Summary of Unconditional Execution Clauses, Summary of Conditional Execution Clauses, Summary of Termination Test Clauses, Overview of the Loop Facility
  250. @subsubsection Summary of Unconditional Execution Clauses
  251. The @t{do} (or @t{doing}) construct evaluates all @i{forms} in its clause.
  252. The @t{return} construct takes one
  253. @i{form}. Any @i{values} returned by the @i{form} are
  254. immediately returned by the @b{loop} form.
  255. It is equivalent to the clause
  256. @t{do (return-from @i{block-name} @i{value})},
  257. where @i{block-name} is the name specified in a @t{named}
  258. clause, or @b{nil} if there is no @t{named} clause.
  259. For more information, see @ref{Unconditional Execution Clauses}.
  260. @node Summary of Conditional Execution Clauses, Summary of Miscellaneous Clauses, Summary of Unconditional Execution Clauses, Overview of the Loop Facility
  261. @subsubsection Summary of Conditional Execution Clauses
  262. The @t{if} and @t{when} constructs take one @i{form} as a test
  263. and a clause that is executed when the test @i{yields} @i{true}.
  264. The clause can be a value accumulation, unconditional, or
  265. another conditional clause; it can also be any combination
  266. of such clauses connected by the @b{loop} @t{and} keyword.
  267. The @b{loop} @t{unless} construct is similar to the @b{loop} @t{when} construct
  268. except that it complements the test result.
  269. The @b{loop} @t{else} construct provides an optional component of @t{if},
  270. @t{when}, and @t{unless} clauses that is executed
  271. when an @t{if} or @t{when} test @i{yields} @i{false}
  272. or when an @t{unless} test @i{yields} @i{true}.
  273. The component is one of the clauses described under @t{if}.
  274. The @b{loop} @t{end} construct provides an optional component to mark the
  275. end of a conditional clause.
  276. For more information, see @ref{Conditional Execution Clauses}.
  277. @node Summary of Miscellaneous Clauses, Order of Execution, Summary of Conditional Execution Clauses, Overview of the Loop Facility
  278. @subsubsection Summary of Miscellaneous Clauses
  279. The @b{loop} @t{named} construct gives a name for the @i{block} of the loop.
  280. The @b{loop} @t{initially} construct causes its @i{forms} to be
  281. evaluated in the loop prologue, which precedes all @b{loop} code
  282. except for initial settings supplied by the constructs @t{with},
  283. @t{for}, or @t{as}.
  284. The @b{loop} @t{finally} construct causes its @i{forms} to
  285. be evaluated in the loop epilogue after normal iteration terminates.
  286. For more information, see @ref{Miscellaneous Clauses}.
  287. @node Order of Execution, Destructuring, Summary of Miscellaneous Clauses, Overview of the Loop Facility
  288. @subsubsection Order of Execution
  289. @ITindex{order of evaluation}
  290. @ITindex{evaluation order}
  291. With the exceptions listed below, clauses are executed in the loop body
  292. in the order in which they appear in the source. Execution is repeated
  293. until a clause
  294. terminates the @b{loop} or until a @b{return}, @b{go},
  295. or @b{throw} form is encountered
  296. which transfers control to a point outside of the loop.
  297. The following actions are
  298. exceptions to the linear order of execution:
  299. @table @asis
  300. @item @t{*}
  301. All variables are initialized first,
  302. regardless of where the establishing clauses appear in the
  303. source. The order of initialization follows the order of these clauses.
  304. @item @t{*}
  305. The code for any @t{initially} clauses is collected
  306. into one @b{progn} in the order in which the clauses appear in
  307. the source. The collected code is executed once in the loop prologue
  308. after any implicit variable initializations.
  309. @item @t{*}
  310. The code for any @t{finally} clauses is collected
  311. into one @b{progn} in the order in which the clauses appear in
  312. the source. The collected code is executed once in the loop epilogue
  313. before any implicit values from the accumulation clauses are returned.
  314. Explicit returns anywhere in the source, however, will exit the
  315. @b{loop} without executing the epilogue code.
  316. @item @t{*}
  317. A @t{with} clause introduces a variable @i{binding}
  318. and an optional initial value. The initial values are calculated
  319. in the order in which the @t{with} clauses occur.
  320. @item @t{*}
  321. Iteration control clauses implicitly perform the following actions:
  322. @table @asis
  323. @item --
  324. initialize variables;
  325. @item --
  326. @i{step} variables, generally
  327. between each execution of the loop body;
  328. @item --
  329. perform termination tests,
  330. generally just before the execution of the
  331. loop body.
  332. @end table
  333. @end table
  334. @node Destructuring, Restrictions on Side-Effects, Order of Execution, Overview of the Loop Facility
  335. @subsubsection Destructuring
  336. The @i{d-type-spec} argument is used for destructuring.
  337. If the
  338. @i{d-type-spec} argument consists solely of the @i{type} @b{fixnum},
  339. @b{float}, @b{t}, or @b{nil}, the @t{of-type} keyword is optional.
  340. The @t{of-type} construct is optional in these cases to provide backwards
  341. compatibility; thus, the following two expressions are the same:
  342. @example
  343. ;;; This expression uses the old syntax for type specifiers.
  344. (loop for i fixnum upfrom 3 ...)
  345. ;;; This expression uses the new syntax for type specifiers.
  346. (loop for i of-type fixnum upfrom 3 ...)
  347. ;; Declare X and Y to be of type VECTOR and FIXNUM respectively.
  348. (loop for (x y) of-type (vector fixnum)
  349. in l do ...)
  350. @end example
  351. A @i{type specifier} for a destructuring pattern is a @i{tree} of
  352. @i{type specifiers} with the same shape as the @i{tree} of
  353. @i{variable} @i{names}, with the following exceptions:
  354. @table @asis
  355. @item @t{*}
  356. When aligning the @i{trees}, an @i{atom} in the
  357. @i{tree} of @i{type specifiers} that matches a @i{cons}
  358. in the variable tree declares the same @i{type} for each variable
  359. in the subtree rooted at the @i{cons}.
  360. @item @t{*}
  361. A @i{cons} in the @i{tree} of @i{type specifiers} that
  362. matches an @i{atom} in the @i{tree} of @i{variable} @i{names}
  363. is a @i{compound type specifer}.
  364. @end table
  365. Destructuring allows @i{binding} of a set of variables to a corresponding
  366. set of values anywhere that a value can normally be bound to a single
  367. variable. During @b{loop} expansion,
  368. each variable in the variable list
  369. is matched with the values in the values list. If there are more variables
  370. in the variable list than there are values in the values list, the
  371. remaining variables are given a value of @b{nil}. If there are more
  372. values than variables listed, the extra values are discarded.
  373. To assign values from a list to the variables @t{a},
  374. @t{b}, and @t{c}, the @t{for} clause could be used to
  375. bind the variable @t{numlist} to the
  376. @i{car} of the supplied @i{form},
  377. and then another @t{for} clause could be used to bind the variables
  378. @t{a}, @t{b}, and @t{c} @i{sequentially}.
  379. @example
  380. ;; Collect values by using FOR constructs.
  381. (loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  382. for a of-type integer = (first numlist)
  383. and b of-type integer = (second numlist)
  384. and c of-type float = (third numlist)
  385. collect (list c b a))
  386. @result{} ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  387. @end example
  388. Destructuring makes this process easier by allowing the variables to
  389. be bound in each loop iteration.
  390. @i{Types} can be declared by using a
  391. list of @i{type-spec} arguments. If
  392. all the @i{types}
  393. are the same, a shorthand destructuring syntax can be used, as the second
  394. example illustrates.
  395. @example
  396. ;; Destructuring simplifies the process.
  397. (loop for (a b c) of-type (integer integer float) in
  398. '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  399. collect (list c b a))
  400. @result{} ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  401. ;; If all the types are the same, this way is even simpler.
  402. (loop for (a b c) of-type float in
  403. '((1.0 2.0 4.0) (5.0 6.0 8.3) (8.0 9.0 10.4))
  404. collect (list c b a))
  405. @result{} ((4.0 2.0 1.0) (8.3 6.0 5.0) (10.4 9.0 8.0))
  406. @end example
  407. If destructuring is used to declare or initialize a number of groups
  408. of variables into @i{types}, the @i{loop keyword} @t{and} can be used
  409. to simplify the process further.
  410. @example
  411. ;; Initialize and declare variables in parallel by using the AND construct.\kern-7pt
  412. (loop with (a b) of-type float = '(1.0 2.0)
  413. and (c d) of-type integer = '(3 4)
  414. and (e f)
  415. return (list a b c d e f))
  416. @result{} (1.0 2.0 3 4 NIL NIL)
  417. @end example
  418. If @b{nil} is used in a destructuring list, no variable is provided for
  419. its place.
  420. @example
  421. (loop for (a nil b) = '(1 2 3)
  422. do (return (list a b)))
  423. @result{} (1 3)
  424. @end example
  425. Note that
  426. @i{dotted lists}
  427. can specify destructuring.
  428. @example
  429. (loop for (x . y) = '(1 . 2)
  430. do (return y))
  431. @result{} 2
  432. (loop for ((a . b) (c . d)) of-type ((float . float) (integer . integer)) in
  433. '(((1.2 . 2.4) (3 . 4)) ((3.4 . 4.6) (5 . 6)))
  434. collect (list a b c d))
  435. @result{} ((1.2 2.4 3 4) (3.4 4.6 5 6))
  436. @end example
  437. An error of @i{type} @b{program-error} is signaled (at macro expansion time)
  438. if the same variable is bound twice in any variable-binding
  439. clause of a single @b{loop} expression. Such variables include
  440. local variables, iteration control variables, and variables found by
  441. destructuring.
  442. @node Restrictions on Side-Effects, , Destructuring, Overview of the Loop Facility
  443. @subsubsection Restrictions on Side-Effects
  444. See @ref{Traversal Rules and Side Effects}.
  445. @node Variable Initialization and Stepping Clauses, Value Accumulation Clauses, Overview of the Loop Facility, The LOOP Facility
  446. @subsection Variable Initialization and Stepping Clauses
  447. @menu
  448. * Iteration Control::
  449. * The for-as-arithmetic subclause::
  450. * Examples of for-as-arithmetic subclause::
  451. * The for-as-in-list subclause::
  452. * Examples of for-as-in-list subclause::
  453. * The for-as-on-list subclause::
  454. * Examples of for-as-on-list subclause::
  455. * The for-as-equals-then subclause::
  456. * Examples of for-as-equals-then subclause::
  457. * The for-as-across subclause::
  458. * Examples of for-as-across subclause::
  459. * The for-as-hash subclause::
  460. * The for-as-package subclause::
  461. * Examples of for-as-package subclause::
  462. * Local Variable Initializations::
  463. * Examples of WITH clause::
  464. @end menu
  465. @node Iteration Control, The for-as-arithmetic subclause, Variable Initialization and Stepping Clauses, Variable Initialization and Stepping Clauses
  466. @subsubsection Iteration Control
  467. Iteration control clauses allow direction of @b{loop} iteration.
  468. The @i{loop keywords} @t{for} and @t{as}
  469. designate iteration control clauses.
  470. Iteration control clauses differ with respect to the specification of
  471. termination tests and to the initialization and @i{stepping}_1
  472. of loop variables. Iteration clauses by themselves
  473. do not cause the Loop Facility to return values, but they
  474. can be used in conjunction with value-accumulation clauses to
  475. return values.
  476. All variables are initialized in the loop prologue.
  477. A @i{variable} @i{binding} has @i{lexical scope}
  478. unless it is proclaimed @b{special};
  479. thus, by default, the variable can be @i{accessed} only by @i{forms}
  480. that lie textually within the @b{loop}.
  481. Stepping assignments are made in the loop body before any other @i{forms}
  482. are evaluated in the body.
  483. The variable argument in iteration control clauses can be a
  484. destructuring list. A destructuring list
  485. is a @i{tree} whose @i{non-nil} @i{atoms} are @i{variable} @i{names}.
  486. See @ref{Destructuring}.
  487. The iteration control clauses @t{for}, @t{as}, and @t{repeat}
  488. must precede any other loop clauses, except
  489. @t{initially}, @t{with}, and @t{named},
  490. since they establish variable @i{bindings}.
  491. When iteration control clauses are
  492. used in a @b{loop},
  493. the corresponding
  494. termination tests in the loop body are evaluated
  495. before any other loop body code is executed.
  496. If multiple iteration clauses are used to control iteration, variable
  497. initialization and @i{stepping}_1 occur @i{sequentially} by default.
  498. The @t{and} construct can be used to connect two or more
  499. iteration clauses when @i{sequential} @i{binding} and
  500. @i{stepping}_1 are not necessary.
  501. The iteration behavior of clauses joined by @t{and}
  502. is analogous to the behavior of the macro @b{do} with
  503. respect to @b{do*}.
  504. The @t{for} and @t{as} clauses iterate by using one or more local
  505. loop variables that are initialized to some value and that
  506. can be modified or @i{stepped}_1 after each iteration.
  507. For these clauses, iteration terminates when a local
  508. variable reaches some supplied value or when some other loop clause
  509. terminates iteration.
  510. At each iteration, variables can be
  511. @i{stepped}_1 by an increment or a decrement
  512. or can be assigned a new value by the evaluation of a @i{form}).
  513. Destructuring can be used to assign
  514. values to variables during iteration.
  515. The @t{for} and @t{as} keywords are synonyms; they can be used
  516. interchangeably. There are seven syntactic formats for these constructs.
  517. In each syntactic format, the @i{type} of
  518. @i{var} can be supplied by the optional @i{type-spec}
  519. argument. If @i{var} is a destructuring list, the @i{type}
  520. supplied by the @i{type-spec} argument must appropriately match
  521. the elements of the list.
  522. By convention, @t{for} introduces new iterations and @t{as}
  523. introduces iterations that depend on a previous iteration specification.
  524. @node The for-as-arithmetic subclause, Examples of for-as-arithmetic subclause, Iteration Control, Variable Initialization and Stepping Clauses
  525. @subsubsection The for-as-arithmetic subclause
  526. In the @i{for-as-arithmetic} subclause, the @t{for}
  527. or @t{as} construct iterates from the value supplied by
  528. @i{form1} to the value supplied by @i{form2} in increments or
  529. decrements denoted by @i{form3}. Each
  530. expression is evaluated only once and must evaluate to a @i{number}.
  531. The variable @i{var} is bound to the value of
  532. @i{form1} in the first iteration and is @i{stepped}_1
  533. by the value of @i{form3} in each succeeding iteration,
  534. or by 1 if @i{form3} is not provided.
  535. The following @i{loop keywords} serve as valid prepositions within this
  536. syntax.
  537. At least one of the
  538. prepositions must be used;
  539. and at most one from each line may be used in a single subclause.
  540. @table @asis
  541. @item from | downfrom | upfrom
  542. @item to | downto | upto | below | above
  543. @item by
  544. @end table
  545. The prepositional phrases in each subclause may appear in any order.
  546. For example, either ``@t{from x by y}'' or ``@t{by y from x}'' is permitted.
  547. However, because left-to-right order of evaluation is preserved,
  548. the effects will be different in the case of side effects.
  549. @ITindex{order of evaluation}
  550. @ITindex{evaluation order}
  551. Consider:
  552. @example
  553. (let ((x 1)) (loop for i from x by (incf x) to 10 collect i))
  554. @result{} (1 3 5 7 9)
  555. (let ((x 1)) (loop for i by (incf x) from x to 10 collect i))
  556. @result{} (2 4 6 8 10)
  557. @end example
  558. The descriptions of the prepositions follow:
  559. @table @asis
  560. @item from
  561. The @i{loop keyword} @t{from} specifies the value from which
  562. @i{stepping}_1 begins, as supplied by @i{form1}.
  563. @i{Stepping}_1 is incremental by default. If
  564. decremental @i{stepping}_1 is desired,
  565. the preposition @t{downto}
  566. or @t{above} must be used with @i{form2}. For incremental
  567. @i{stepping}_1, the default @t{from} value is 0.
  568. @item downfrom, upfrom
  569. The @i{loop keyword} @t{downfrom}
  570. indicates that the variable @i{var} is decreased in decrements
  571. supplied by @i{form3}; the @i{loop keyword} @t{upfrom} indicates that
  572. @i{var} is increased in increments supplied by @i{form3}.
  573. @item to
  574. The @i{loop keyword} @t{to} marks the end value
  575. for @i{stepping}_1 supplied in @i{form2}.
  576. @i{Stepping}_1 is incremental by default.
  577. If decremental @i{stepping}_1 is desired,
  578. the preposition @t{downfrom} must be used with @i{form1},
  579. or else the preposition @t{downto} or @t{above} should be used instead
  580. of @t{to} with @i{form2}.
  581. @item downto, upto
  582. The @i{loop keyword} @t{downto} specifies decremental @i{stepping};
  583. the @i{loop keyword} @t{upto} specifies incremental @i{stepping}.
  584. In both cases, the amount of change on each step is specified by @i{form3},
  585. and the @b{loop} terminates when the variable @i{var} passes
  586. the value of @i{form2}.
  587. Since there is no default for @i{form1} in decremental @i{stepping}_1,
  588. a @i{form1} value must be supplied (using @t{from} or @t{downfrom})
  589. when @t{downto} is supplied.
  590. @item below, above
  591. The @i{loop keywords} @t{below} and @t{above} are analogous to
  592. @t{upto} and @t{downto} respectively. These keywords stop
  593. iteration just before the value of the variable @i{var} reaches the value
  594. supplied by @i{form2}; the end value of @i{form2} is not included.
  595. Since there is no default for @i{form1} in decremental @i{stepping}_1,
  596. a @i{form1} value must be supplied (using @t{from} or @t{downfrom})
  597. when @t{above} is supplied.
  598. @item by
  599. The @i{loop keyword} @t{by} marks the increment or decrement supplied by
  600. @i{form3}. The value of @i{form3} can be any
  601. positive
  602. @i{number}.
  603. The default value is 1.
  604. @end table
  605. In an iteration control clause, the @t{for} or @t{as} construct
  606. causes termination when the supplied limit is reached. That is,
  607. iteration continues until the value @i{var} is stepped to the
  608. exclusive or inclusive limit supplied by @i{form2}. The range is
  609. exclusive if @i{form3} increases or decreases @i{var}
  610. to the value of @i{form2} without reaching that value; the loop
  611. keywords @t{below} and @t{above} provide exclusive limits. An
  612. inclusive limit allows @i{var} to attain the value of
  613. @i{form2}; @t{to}, @t{downto}, and @t{upto} provide inclusive
  614. limits.
  615. @node Examples of for-as-arithmetic subclause, The for-as-in-list subclause, The for-as-arithmetic subclause, Variable Initialization and Stepping Clauses
  616. @subsubsection Examples of for-as-arithmetic subclause
  617. @example
  618. ;; Print some numbers.
  619. (loop for i from 1 to 3
  620. do (print i))
  621. @t{ |> } 1
  622. @t{ |> } 2
  623. @t{ |> } 3
  624. @result{} NIL
  625. ;; Print every third number.
  626. (loop for i from 10 downto 1 by 3
  627. do (print i))
  628. @t{ |> } 10
  629. @t{ |> } 7
  630. @t{ |> } 4
  631. @t{ |> } 1
  632. @result{} NIL
  633. ;; Step incrementally from the default starting value.
  634. (loop for i below 3
  635. do (print i))
  636. @t{ |> } 0
  637. @t{ |> } 1
  638. @t{ |> } 2
  639. @result{} NIL
  640. @end example
  641. @node The for-as-in-list subclause, Examples of for-as-in-list subclause, Examples of for-as-arithmetic subclause, Variable Initialization and Stepping Clauses
  642. @subsubsection The for-as-in-list subclause
  643. In the @i{for-as-in-list} subclause,
  644. the @t{for}
  645. or @t{as} construct iterates over the contents of a
  646. @i{list}. It checks for
  647. the end of the @i{list} as if by using @b{endp}.
  648. The variable @i{var} is bound to the successive elements of
  649. the @i{list} in @i{form1} before each
  650. iteration. At the end of each iteration, the function @i{step-fun}
  651. is applied to the @i{list}; the default value for @i{step-fun} is
  652. @b{cdr}.
  653. The @i{loop keywords} @t{in} and @t{by} serve as valid prepositions in
  654. this syntax.
  655. The @t{for} or @t{as} construct causes termination when the
  656. end of the @i{list} is reached.
  657. @node Examples of for-as-in-list subclause, The for-as-on-list subclause, The for-as-in-list subclause, Variable Initialization and Stepping Clauses
  658. @subsubsection Examples of for-as-in-list subclause
  659. @example
  660. ;; Print every item in a list.
  661. (loop for item in '(1 2 3) do (print item))
  662. @t{ |> } 1
  663. @t{ |> } 2
  664. @t{ |> } 3
  665. @result{} NIL
  666. ;; Print every other item in a list.
  667. (loop for item in '(1 2 3 4 5) by #'cddr
  668. do (print item))
  669. @t{ |> } 1
  670. @t{ |> } 3
  671. @t{ |> } 5
  672. @result{} NIL
  673. ;; Destructure a list, and sum the x values using fixnum arithmetic.
  674. (loop for (item . x) of-type (t . fixnum) in '((A . 1) (B . 2) (C . 3))
  675. unless (eq item 'B) sum x)
  676. @result{} 4
  677. @end example
  678. @node The for-as-on-list subclause, Examples of for-as-on-list subclause, Examples of for-as-in-list subclause, Variable Initialization and Stepping Clauses
  679. @subsubsection The for-as-on-list subclause
  680. In the @i{for-as-on-list} subclause, the @t{for} or @t{as}
  681. construct iterates over
  682. a @i{list}. It checks for the
  683. end of the @i{list} as if by using @b{atom}.
  684. The variable @i{var} is bound to the successive tails of the
  685. @i{list} in
  686. @i{form1}. At the end of each iteration, the function @i{step-fun}
  687. is applied to the @i{list}; the default value for @i{step-fun} is @b{cdr}.
  688. The @i{loop keywords} @t{on} and @t{by} serve as valid
  689. prepositions in this syntax.
  690. The @t{for} or @t{as} construct causes termination when the
  691. end of the @i{list} is reached.
  692. @node Examples of for-as-on-list subclause, The for-as-equals-then subclause, The for-as-on-list subclause, Variable Initialization and Stepping Clauses
  693. @subsubsection Examples of for-as-on-list subclause
  694. @example
  695. ;; Collect successive tails of a list.
  696. (loop for sublist on '(a b c d)
  697. collect sublist)
  698. @result{} ((A B C D) (B C D) (C D) (D))
  699. ;; Print a list by using destructuring with the loop keyword ON.
  700. (loop for (item) on '(1 2 3)
  701. do (print item))
  702. @t{ |> } 1
  703. @t{ |> } 2
  704. @t{ |> } 3
  705. @result{} NIL
  706. @end example
  707. @node The for-as-equals-then subclause, Examples of for-as-equals-then subclause, Examples of for-as-on-list subclause, Variable Initialization and Stepping Clauses
  708. @subsubsection The for-as-equals-then subclause
  709. In the @i{for-as-equals-then} subclause
  710. the @t{for}
  711. or @t{as} construct
  712. initializes the variable @i{var} by setting it to the
  713. result of evaluating @i{form1} on the first iteration, then setting
  714. it to the result of evaluating @i{form2} on the second and
  715. subsequent iterations. If @i{form2} is omitted, the construct
  716. uses @i{form1} on the second and
  717. subsequent iterations.
  718. The @i{loop keywords} {=} and @t{then} serve as valid prepositions
  719. in this syntax.
  720. This construct does not provide any termination tests.
  721. @node Examples of for-as-equals-then subclause, The for-as-across subclause, The for-as-equals-then subclause, Variable Initialization and Stepping Clauses
  722. @subsubsection Examples of for-as-equals-then subclause
  723. @example
  724. ;; Collect some numbers.
  725. (loop for item = 1 then (+ item 10)
  726. for iteration from 1 to 5
  727. collect item)
  728. @result{} (1 11 21 31 41)
  729. @end example
  730. @node The for-as-across subclause, Examples of for-as-across subclause, Examples of for-as-equals-then subclause, Variable Initialization and Stepping Clauses
  731. @subsubsection The for-as-across subclause
  732. In the @i{for-as-across} subclause the @t{for}
  733. or @t{as} construct binds the variable @i{var} to the value of
  734. each element in the array @i{vector}.
  735. The @i{loop keyword} @t{across} marks the array @i{vector}; @t{across}
  736. is used as a preposition in this syntax.
  737. Iteration stops when there are no more elements in the supplied
  738. @i{array} that can be referenced.
  739. Some implementations might recognize a @b{the} special form
  740. in the @i{vector} form to produce more efficient code.
  741. @node Examples of for-as-across subclause, The for-as-hash subclause, The for-as-across subclause, Variable Initialization and Stepping Clauses
  742. @subsubsection Examples of for-as-across subclause
  743. @example
  744. (loop for char across (the simple-string (find-message channel))
  745. do (write-char char stream))
  746. @end example
  747. @node The for-as-hash subclause, The for-as-package subclause, Examples of for-as-across subclause, Variable Initialization and Stepping Clauses
  748. @subsubsection The for-as-hash subclause
  749. In the @i{for-as-hash} subclause
  750. the @t{for}
  751. or @t{as} construct
  752. iterates over the elements, keys, and values of a @i{hash-table}.
  753. In this syntax, a compound preposition is used to designate access to a
  754. @i{hash table}.
  755. The variable @i{var} takes on the value of each hash key
  756. or hash value in the supplied @i{hash-table}.
  757. The following @i{loop keywords} serve as valid prepositions within this syntax:
  758. @table @asis
  759. @item @t{being}
  760. The keyword @t{being} introduces either the Loop schema
  761. @t{hash-key} or @t{hash-value}.
  762. @item @t{each}, @t{the}
  763. The @i{loop keyword} @t{each}
  764. follows the @i{loop keyword} @t{being} when @t{hash-key} or
  765. @t{hash-value} is used. The @i{loop keyword} @t{the} is used with
  766. @t{hash-keys} and @t{hash-values} only for ease of reading.
  767. This agreement isn't required.
  768. @item @t{hash-key}, @t{hash-keys}
  769. These @i{loop keywords} access each key entry of the @i{hash table}. If
  770. the name @t{hash-value} is supplied in a @t{using} construct with one
  771. of these Loop schemas, the iteration can optionally access the keyed
  772. value. The order in which the keys are accessed is undefined; empty
  773. slots in the @i{hash table} are ignored.
  774. @item @t{hash-value}, @t{hash-values}
  775. These @i{loop keywords} access each value entry of a
  776. @i{hash table}. If
  777. the name @t{hash-key} is supplied in a @t{using} construct with one of
  778. these Loop schemas, the iteration can optionally access the key that
  779. corresponds to the value. The order in which the keys are accessed is
  780. undefined; empty slots in the @i{hash table} are ignored.
  781. @item @t{using}
  782. The @i{loop keyword} @t{using} introduces
  783. the optional key or the keyed value to
  784. be accessed. It allows access to the hash key if iteration is over
  785. the hash values, and the hash value if
  786. iteration is over the hash keys.
  787. @item @t{in}, @t{of}
  788. These loop prepositions introduce @i{hash-table}.
  789. @end table
  790. In effect
  791. @t{being}
  792. @{{each} | @t{the}@}
  793. @{{hash-value} |
  794. @t{hash-values} |
  795. @t{hash-key} |
  796. @t{hash-keys}@}
  797. @{{in} | @t{of}@}
  798. is a compound preposition.
  799. Iteration stops when there are no more hash keys or hash values to be
  800. referenced in the supplied @i{hash-table}.
  801. @node The for-as-package subclause, Examples of for-as-package subclause, The for-as-hash subclause, Variable Initialization and Stepping Clauses
  802. @subsubsection The for-as-package subclause
  803. In the @i{for-as-package} subclause
  804. the @t{for}
  805. or @t{as} construct
  806. iterates over the @i{symbols} in a @i{package}.
  807. In this syntax, a compound preposition is used to designate access to a
  808. @i{package}.
  809. The variable @i{var} takes on the value of each @i{symbol}
  810. in the supplied @i{package}.
  811. The following @i{loop keywords} serve as valid prepositions within this syntax:
  812. @table @asis
  813. @item @t{being}
  814. The keyword @t{being} introduces either the Loop schema
  815. @t{symbol}, @t{present-symbol}, or @t{external-symbol}.
  816. @item @t{each}, @t{the}
  817. The @i{loop keyword} @t{each}
  818. follows the @i{loop keyword} @t{being} when @t{symbol},
  819. @t{present-symbol}, or @t{external-symbol} is used.
  820. The @i{loop keyword} @t{the} is used with @t{symbols},
  821. @t{present-symbols}, and @t{external-symbols} only for ease of reading.
  822. This agreement isn't required.
  823. @item @t{present-symbol}, @t{present-symbols}
  824. These Loop schemas iterate over the @i{symbols}
  825. that are @i{present} in a @i{package}.
  826. The @i{package} to be iterated over is supplied in the same way
  827. that @i{package} arguments to @b{find-package} are supplied.
  828. If the @i{package} for the iteration is not supplied,
  829. the @i{current package} is used.
  830. If a @i{package} that does not exist is supplied,
  831. an error of @i{type} @b{package-error} is signaled.
  832. @item @t{symbol}, @t{symbols}
  833. These Loop schemas iterate over @i{symbols} that are
  834. @i{accessible} in a given @i{package}.
  835. The @i{package} to be iterated over is supplied in the same way
  836. that @i{package} arguments to @b{find-package} are supplied.
  837. If the @i{package} for the iteration is not supplied,
  838. the @i{current package} is used.
  839. If a @i{package} that does not exist is supplied,
  840. an error of @i{type} @b{package-error} is signaled.
  841. @item @t{external-symbol}, @t{external-symbols}
  842. These Loop schemas iterate over the @i{external symbols} of a @i{package}.
  843. The @i{package} to be iterated over is supplied in the same way
  844. that @i{package} arguments to @b{find-package} are supplied.
  845. If the @i{package} for the iteration is not supplied,
  846. the @i{current package} is used.
  847. If a @i{package} that does not exist is supplied,
  848. an error of @i{type} @b{package-error} is signaled.
  849. @item @t{in}, @t{of}
  850. These loop prepositions introduce @i{package}.
  851. @end table
  852. In effect
  853. @t{being}
  854. @{{each} | @t{the}@}
  855. @{{symbol} |
  856. @t{symbols} |
  857. @t{present-symbol} |
  858. @t{present-symbols} |
  859. @t{external-symbol} |
  860. @t{external-symbols}@}
  861. @{{in} | @t{of}@}
  862. is a compound preposition.
  863. Iteration stops when there are no more @i{symbols} to be referenced
  864. in the supplied @i{package}.
  865. @node Examples of for-as-package subclause, Local Variable Initializations, The for-as-package subclause, Variable Initialization and Stepping Clauses
  866. @subsubsection Examples of for-as-package subclause
  867. @example
  868. (let ((*package* (make-package "TEST-PACKAGE-1")))
  869. ;; For effect, intern some symbols
  870. (read-from-string "(THIS IS A TEST)")
  871. (export (intern "THIS"))
  872. (loop for x being each present-symbol of *package*
  873. do (print x)))
  874. @t{ |> } A
  875. @t{ |> } TEST
  876. @t{ |> } THIS
  877. @t{ |> } IS
  878. @result{} NIL
  879. @end example
  880. @node Local Variable Initializations, Examples of WITH clause, Examples of for-as-package subclause, Variable Initialization and Stepping Clauses
  881. @subsubsection Local Variable Initializations
  882. When a @b{loop} @i{form} is executed, the local variables are bound and are
  883. initialized to some value. These local variables exist until @b{loop}
  884. iteration terminates, at which point they cease to exist.
  885. Implicit variables are also established by iteration control clauses and the
  886. @t{into} preposition of accumulation clauses.
  887. The @t{with} construct initializes variables that are local to
  888. a loop. The variables are initialized one time only.
  889. If the optional @i{type-spec} argument is supplied for the variable
  890. @i{var}, but there is no related expression to be evaluated, @i{var}
  891. is initialized to an appropriate default value for its @i{type}.
  892. For example, for the types @b{t}, @b{number},
  893. and @b{float},
  894. the default values are @b{nil}, @t{0}, and @t{0.0} respectively.
  895. The consequences are undefined if a
  896. @i{type-spec} argument is supplied for @i{var} if
  897. the related expression returns a value that is not of the supplied
  898. @i{type}.
  899. By default, the @t{with} construct initializes variables
  900. @i{sequentially}; that is, one variable is assigned a value before the
  901. next expression is evaluated. However, by using the @i{loop keyword} @t{and}
  902. to join several @t{with} clauses,
  903. initializations can be forced to occur in @i{parallel}; that
  904. is, all of the supplied
  905. @i{forms} are evaluated, and the results are bound to the respective
  906. variables simultaneously.
  907. @i{Sequential} @i{binding} is used when it is desireable for the initialization of
  908. some variables to depend on the values of previously bound variables.
  909. For example, suppose the variables @t{a}, @t{b}, and @t{c} are to be bound in sequence:
  910. @example
  911. (loop with a = 1
  912. with b = (+ a 2)
  913. with c = (+ b 3)
  914. return (list a b c))
  915. @result{} (1 3 6)
  916. @end example
  917. The execution of the above @b{loop} is equivalent to the execution of
  918. the following code:
  919. @example
  920. (block nil
  921. (let* ((a 1)
  922. (b (+ a 2))
  923. (c (+ b 3)))
  924. (tagbody
  925. (next-loop (return (list a b c))
  926. (go next-loop)
  927. end-loop))))
  928. @end example
  929. If the values of previously bound variables are not needed
  930. for the initialization of other local variables, an
  931. @t{and} clause can be used to
  932. specify that the bindings are to occur in @i{parallel}:
  933. @example
  934. (loop with a = 1
  935. and b = 2
  936. and c = 3
  937. return (list a b c))
  938. @result{} (1 2 3)
  939. @end example
  940. The execution of the above loop is equivalent to the execution of
  941. the following code:
  942. @example
  943. (block nil
  944. (let ((a 1)
  945. (b 2)
  946. (c 3))
  947. (tagbody
  948. (next-loop (return (list a b c))
  949. (go next-loop)
  950. end-loop))))
  951. @end example
  952. @node Examples of WITH clause, , Local Variable Initializations, Variable Initialization and Stepping Clauses
  953. @subsubsection Examples of WITH clause
  954. @example
  955. ;; These bindings occur in sequence.
  956. (loop with a = 1
  957. with b = (+ a 2)
  958. with c = (+ b 3)
  959. return (list a b c))
  960. @result{} (1 3 6)
  961. ;; These bindings occur in parallel.
  962. (setq a 5 b 10)
  963. @result{} 10
  964. (loop with a = 1
  965. and b = (+ a 2)
  966. and c = (+ b 3)
  967. return (list a b c))
  968. @result{} (1 7 13)
  969. ;; This example shows a shorthand way to declare local variables
  970. ;; that are of different types.
  971. (loop with (a b c) of-type (float integer float)
  972. return (format nil "~A ~A ~A" a b c))
  973. @result{} "0.0 0 0.0"
  974. ;; This example shows a shorthand way to declare local variables
  975. ;; that are the same type.
  976. (loop with (a b c) of-type float
  977. return (format nil "~A ~A ~A" a b c))
  978. @result{} "0.0 0.0 0.0"
  979. @end example
  980. @node Value Accumulation Clauses, Termination Test Clauses, Variable Initialization and Stepping Clauses, The LOOP Facility
  981. @subsection Value Accumulation Clauses
  982. The constructs @t{collect}, @t{collecting},
  983. @t{append}, @t{appending},
  984. @t{nconc}, @t{nconcing},
  985. @t{count}, @t{counting},
  986. @t{maximize}, @t{maximizing},
  987. @t{minimize}, @t{minimizing},
  988. @t{sum}, and @t{summing},
  989. allow values to be accumulated in a @b{loop}.
  990. The constructs @t{collect},
  991. @t{collecting}, @t{append}, @t{appending},
  992. @t{nconc}, and @t{nconcing},
  993. designate clauses that
  994. accumulate values in @i{lists} and return them.
  995. The constructs @t{count}, @t{counting},
  996. @t{maximize}, @t{maximizing}, @t{minimize}, @t{minimizing},
  997. @t{sum}, and @t{summing} designate clauses that accumulate and
  998. return numerical values.
  999. During each iteration, the constructs
  1000. @t{collect} and @t{collecting}
  1001. collect the value of the supplied
  1002. @i{form} into a @i{list}.
  1003. When iteration terminates, the @i{list} is returned.
  1004. The argument @i{var} is
  1005. set to the @i{list}
  1006. of collected values; if @i{var} is supplied, the @b{loop}
  1007. does not return the final @i{list} automatically. If
  1008. @i{var} is not
  1009. supplied, it is equivalent to supplying an internal name for
  1010. @i{var} and returning its value in a @t{finally} clause.
  1011. The @i{var} argument
  1012. is bound as if by the construct @t{with}.
  1013. No mechanism is provided for declaring the @i{type} of @i{var};
  1014. it must be of @i{type} @b{list}.
  1015. The constructs @t{append}, @t{appending},
  1016. @t{nconc}, and @t{nconcing}
  1017. are similar to @t{collect} except that the
  1018. values of the supplied @i{form} must be @i{lists}.
  1019. @table @asis
  1020. @item @t{*}
  1021. The @t{append} keyword causes its @i{list} values to be concatenated
  1022. into a single @i{list}, as if
  1023. they were arguments to the @i{function} @b{append}.
  1024. @item @t{*}
  1025. The @t{nconc} keyword causes its @i{list} values to be concatenated
  1026. into a single @i{list},
  1027. as if they were arguments to the @i{function} @b{nconc}.
  1028. @end table
  1029. The argument @i{var} is
  1030. set to the @i{list} of
  1031. concatenated values; if @i{var} is supplied,
  1032. @b{loop}
  1033. does not return the final @i{list} automatically.
  1034. The @i{var} argument
  1035. is bound as if by the construct @t{with}.
  1036. A @i{type} cannot be supplied for @i{var};
  1037. it must be of @i{type} @b{list}.
  1038. The construct @t{nconc}
  1039. destructively modifies its argument @i{lists}.
  1040. The @t{count} construct counts the number of times
  1041. that the supplied @i{form} returns @i{true}.
  1042. The argument @i{var} accumulates the number of occurrences;
  1043. if @i{var} is supplied,
  1044. @b{loop} does not return the final count automatically.
  1045. The @i{var} argument is bound as if by the construct @t{with}
  1046. to a zero of the appropriate type.
  1047. Subsequent values (including any necessary coercions)
  1048. are computed as if by the function @b{1+}.
  1049. If @t{into} @i{var} is used,
  1050. a @i{type} can be supplied for @i{var} with the @i{type-spec} argument;
  1051. the consequences are unspecified if a nonnumeric @i{type} is supplied.
  1052. If there is no @t{into} variable,
  1053. the optional @i{type-spec} argument
  1054. applies to the internal variable that is keeping the count.
  1055. The default @i{type} is @i{implementation-dependent};
  1056. but it must be
  1057. a @i{supertype} of @i{type} @b{fixnum}.
  1058. The @t{maximize} and
  1059. @t{minimize}
  1060. constructs compare
  1061. the value of the supplied @i{form} obtained during the first
  1062. iteration with values obtained in successive iterations.
  1063. The maximum (for @t{maximize}) or minimum (for @t{minimize})
  1064. value encountered is determined
  1065. (as if by the @i{function} @b{max} for @t{maximize} and
  1066. as if by the @i{function} @b{min} for @t{minimize})
  1067. and returned.
  1068. If the @t{maximize} or @t{minimize} clause
  1069. is never executed, the accumulated value is unspecified.
  1070. The argument @i{var} accumulates the maximum or minimum value;
  1071. if @i{var} is supplied,
  1072. @b{loop} does not return the maximum or minimum automatically.
  1073. The @i{var} argument is bound as if by the construct @t{with}.
  1074. If @t{into} @i{var} is used,
  1075. a @i{type} can be supplied for @i{var} with the @i{type-spec} argument;
  1076. the consequences are unspecified if a nonnumeric @i{type} is supplied.
  1077. If there is no @t{into} variable,
  1078. the optional @i{type-spec} argument applies to the internal variable
  1079. that is keeping the maximum or minimum value.
  1080. The default @i{type}
  1081. is @i{implementation-dependent}; but it
  1082. must be a @i{supertype} of @i{type} @b{real}.
  1083. The @t{sum} construct forms a cumulative sum
  1084. of the successive @i{primary values} of the supplied @i{form}
  1085. at each iteration.
  1086. The argument @i{var} is used to accumulate the sum;
  1087. if @i{var} is supplied,
  1088. @b{loop} does not return the final sum automatically.
  1089. The @i{var} argument is bound as if by the construct @t{with}
  1090. to a zero of the appropriate type.
  1091. Subsequent values (including any necessary coercions) are computed as if by the @i{function} @b{+}.
  1092. If @t{into} @i{var} is used,
  1093. a @i{type} can be supplied for @i{var} with the @i{type-spec} argument;
  1094. the consequences are unspecified if a nonnumeric @i{type} is supplied.
  1095. If there is no @t{into} variable,
  1096. the optional @i{type-spec} argument applies to the internal variable
  1097. that is keeping the sum.
  1098. The default @i{type}
  1099. is @i{implementation-dependent}; but it
  1100. must be a @i{supertype} of @i{type} @b{number}.
  1101. If @t{into} is used,
  1102. the construct does not provide a default return value;
  1103. however, the variable is available
  1104. for use in any @t{finally} clause.
  1105. Certain kinds of accumulation clauses can be combined in a @b{loop}
  1106. if their destination is the same
  1107. (the result of @b{loop} or an @t{into} @i{var})
  1108. because they are considered to accumulate conceptually compatible quantities.
  1109. In particular,
  1110. any elements of following sets of accumulation clauses can be mixed
  1111. with other elements of the same set for the same destination
  1112. in a @b{loop} @i{form}:
  1113. @table @asis
  1114. @item @t{*}
  1115. @t{collect}, @t{append}, @t{nconc}
  1116. @item @t{*}
  1117. @t{sum}, @t{count}
  1118. @item @t{*}
  1119. @t{maximize}, @t{minimize}
  1120. @end table
  1121. @example
  1122. ;; Collect every name and the kids in one list by using
  1123. ;; COLLECT and APPEND.
  1124. (loop for name in '(fred sue alice joe june)
  1125. for kids in '((bob ken) () () (kris sunshine) ())
  1126. collect name
  1127. append kids)
  1128. @result{} (FRED BOB KEN SUE ALICE JOE KRIS SUNSHINE JUNE)
  1129. @end example
  1130. Any two
  1131. clauses that do not accumulate the same @i{type} of
  1132. @i{object}
  1133. can coexist in a @b{loop} only
  1134. if each clause accumulates its values into
  1135. a different
  1136. @i{variable}.
  1137. @menu
  1138. * Examples of COLLECT clause::
  1139. * Examples of APPEND and NCONC clauses::
  1140. * Examples of COUNT clause::
  1141. * Examples of MAXIMIZE and MINIMIZE clauses::
  1142. * Examples of SUM clause::
  1143. @end menu
  1144. @node Examples of COLLECT clause, Examples of APPEND and NCONC clauses, Value Accumulation Clauses, Value Accumulation Clauses
  1145. @subsubsection Examples of COLLECT clause
  1146. @example
  1147. ;; Collect all the symbols in a list.
  1148. (loop for i in '(bird 3 4 turtle (1 . 4) horse cat)
  1149. when (symbolp i) collect i)
  1150. @result{} (BIRD TURTLE HORSE CAT)
  1151. ;; Collect and return odd numbers.
  1152. (loop for i from 1 to 10
  1153. if (oddp i) collect i)
  1154. @result{} (1 3 5 7 9)
  1155. ;; Collect items into local variable, but don't return them.
  1156. (loop for i in '(a b c d) by #'cddr
  1157. collect i into my-list
  1158. finally (print my-list))
  1159. @t{ |> } (A C)
  1160. @result{} NIL
  1161. @end example
  1162. @node Examples of APPEND and NCONC clauses, Examples of COUNT clause, Examples of COLLECT clause, Value Accumulation Clauses
  1163. @subsubsection Examples of APPEND and NCONC clauses
  1164. @example
  1165. ;; Use APPEND to concatenate some sublists.
  1166. (loop for x in '((a) (b) ((c)))
  1167. append x)
  1168. @result{} (A B (C))
  1169. ;; NCONC some sublists together. Note that only lists made by the
  1170. ;; call to LIST are modified.
  1171. (loop for i upfrom 0
  1172. as x in '(a b (c))
  1173. nconc (if (evenp i) (list x) nil))
  1174. @result{} (A (C))
  1175. @end example
  1176. @node Examples of COUNT clause, Examples of MAXIMIZE and MINIMIZE clauses, Examples of APPEND and NCONC clauses, Value Accumulation Clauses
  1177. @subsubsection Examples of COUNT clause
  1178. @example
  1179. (loop for i in '(a b nil c nil d e)
  1180. count i)
  1181. @result{} 5
  1182. @end example
  1183. @node Examples of MAXIMIZE and MINIMIZE clauses, Examples of SUM clause, Examples of COUNT clause, Value Accumulation Clauses
  1184. @subsubsection Examples of MAXIMIZE and MINIMIZE clauses
  1185. @example
  1186. (loop for i in '(2 1 5 3 4)
  1187. maximize i)
  1188. @result{} 5
  1189. (loop for i in '(2 1 5 3 4)
  1190. minimize i)
  1191. @result{} 1
  1192. ;; In this example, FIXNUM applies to the internal variable that holds
  1193. ;; the maximum value.
  1194. (setq series '(1.2 4.3 5.7))
  1195. @result{} (1.2 4.3 5.7)
  1196. (loop for v in series
  1197. maximize (round v) of-type fixnum)
  1198. @result{} 6
  1199. ;; In this example, FIXNUM applies to the variable RESULT.
  1200. (loop for v of-type float in series
  1201. minimize (round v) into result of-type fixnum
  1202. finally (return result))
  1203. @result{} 1
  1204. @end example
  1205. @node Examples of SUM clause, , Examples of MAXIMIZE and MINIMIZE clauses, Value Accumulation Clauses
  1206. @subsubsection Examples of SUM clause
  1207. @example
  1208. (loop for i of-type fixnum in '(1 2 3 4 5)
  1209. sum i)
  1210. @result{} 15
  1211. (setq series '(1.2 4.3 5.7))
  1212. @result{} (1.2 4.3 5.7)
  1213. (loop for v in series
  1214. sum (* 2.0 v))
  1215. @result{} 22.4
  1216. @end example
  1217. @node Termination Test Clauses, Unconditional Execution Clauses, Value Accumulation Clauses, The LOOP Facility
  1218. @subsection Termination Test Clauses
  1219. The @t{repeat} construct causes iteration to terminate after a
  1220. specified number of times.
  1221. The loop body executes @i{n} times, where @i{n} is the value
  1222. of the expression @i{form}. The @i{form} argument is evaluated one time
  1223. in the loop prologue. If the expression evaluates to 0 or
  1224. to a negative @i{number}, the loop body is not evaluated.
  1225. The constructs @t{always},
  1226. @t{never},
  1227. @t{thereis},
  1228. @t{while},
  1229. @t{until},
  1230. and the macro @b{loop-finish}
  1231. allow conditional termination of iteration within
  1232. a @b{loop}.
  1233. The constructs @t{always}, @t{never}, and @t{thereis} provide
  1234. specific values to be returned when a @b{loop} terminates.
  1235. Using @t{always}, @t{never}, or @t{thereis} in a loop with
  1236. value accumulation clauses that are not @t{into} causes
  1237. an error of @i{type} @b{program-error} to be signaled (at macro expansion time).
  1238. Since @t{always}, @t{never}, and @t{thereis}
  1239. use
  1240. the @b{return-from} @i{special operator}
  1241. to terminate iteration,
  1242. any @t{finally} clause that is supplied is not evaluated
  1243. when exit occurs due to any of these constructs.
  1244. In all other respects these
  1245. constructs behave like the @t{while} and @t{until} constructs.
  1246. The @t{always} construct takes one @i{form} and terminates the
  1247. @b{loop}
  1248. if the @i{form} ever evaluates to @b{nil}; in this case, it returns
  1249. @b{nil}. Otherwise, it provides a default return value of @b{t}.
  1250. If the value of the supplied @i{form} is never @b{nil}, some other construct
  1251. can terminate the iteration.
  1252. The @t{never} construct terminates iteration the first time that
  1253. the value of the supplied @i{form} is @i{non-nil}; the @b{loop} returns
  1254. @b{nil}.
  1255. If the value of the supplied @i{form} is always @b{nil}, some other
  1256. construct can terminate the iteration.
  1257. Unless some other clause contributes
  1258. a return value, the default value returned is @b{t}.
  1259. The @t{thereis} construct terminates iteration the first time that the
  1260. value of the supplied @i{form} is @i{non-nil}; the @b{loop} returns the
  1261. value of the supplied @i{form}.
  1262. If the value of the supplied @i{form}
  1263. is always @b{nil}, some other
  1264. construct can terminate the iteration. Unless some other clause contributes a
  1265. return value, the default value returned is @b{nil}.
  1266. There are two differences between the @t{thereis} and @t{until}
  1267. constructs:
  1268. @table @asis
  1269. @item @t{*}
  1270. The @t{until} construct does not return a value or
  1271. @b{nil} based on the value of the supplied @i{form}.
  1272. @item @t{*}
  1273. The @t{until} construct executes
  1274. any @t{finally} clause.
  1275. Since @t{thereis} uses
  1276. the @b{return-from} @i{special operator}
  1277. to terminate iteration,
  1278. any @t{finally} clause that is supplied is not evaluated
  1279. when exit occurs due to @t{thereis}.
  1280. @end table
  1281. The @t{while} construct allows iteration to continue until the
  1282. supplied @i{form}
  1283. evaluates to @i{false}. The supplied @i{form}
  1284. is reevaluated at the location of the @t{while} clause.
  1285. The @t{until} construct is equivalent to
  1286. @t{while (not @i{form})\dots}. If the value of the
  1287. supplied @i{form} is @i{non-nil}, iteration terminates.
  1288. Termination-test control constructs can be used anywhere within the loop body.
  1289. The termination tests are used in the order in which they appear.
  1290. If an @t{until} or @t{while} clause causes
  1291. termination, any clauses that precede it in the source
  1292. are still evaluated.
  1293. If the @t{until} and @t{while} constructs cause termination,
  1294. control is passed to the loop epilogue, where any @t{finally}
  1295. clauses will be executed.
  1296. There are two differences between the @t{never} and @t{until}
  1297. constructs:
  1298. @table @asis
  1299. @item @t{*}
  1300. The @t{until} construct does not return
  1301. @b{t} or @b{nil} based on the value of the supplied @i{form}.
  1302. @item @t{*}
  1303. The @t{until} construct
  1304. does not bypass any @t{finally} clauses.
  1305. Since @t{never} uses
  1306. the @b{return-from} @i{special operator}
  1307. to terminate iteration,
  1308. any @t{finally} clause that is supplied is not evaluated
  1309. when exit occurs due to @t{never}.
  1310. @end table
  1311. In most cases it is not necessary to use @b{loop-finish}
  1312. because other loop control clauses terminate the @b{loop}.
  1313. The macro @b{loop-finish} is used to provide a normal exit
  1314. from a nested conditional inside a @b{loop}.
  1315. Since @b{loop-finish} transfers control to the loop epilogue,
  1316. using @b{loop-finish} within a @t{finally} expression can cause
  1317. infinite looping.
  1318. @menu
  1319. * Examples of REPEAT clause::
  1320. * Examples of ALWAYS::
  1321. * Examples of WHILE and UNTIL clauses::
  1322. @end menu
  1323. @node Examples of REPEAT clause, Examples of ALWAYS, Termination Test Clauses, Termination Test Clauses
  1324. @subsubsection Examples of REPEAT clause
  1325. @example
  1326. (loop repeat 3
  1327. do (format t "~&What I say three times is true.~
  1328. @t{ |> } What I say three times is true.
  1329. @t{ |> } What I say three times is true.
  1330. @t{ |> } What I say three times is true.
  1331. @result{} NIL
  1332. (loop repeat -15
  1333. do (format t "What you see is what you expect~
  1334. @result{} NIL
  1335. @end example
  1336. @node Examples of ALWAYS, Examples of WHILE and UNTIL clauses, Examples of REPEAT clause, Termination Test Clauses
  1337. @subsubsection Examples of ALWAYS, NEVER, and THEREIS clauses
  1338. @example
  1339. ;; Make sure I is always less than 11 (two ways).
  1340. ;; The FOR construct terminates these loops.
  1341. (loop for i from 0 to 10
  1342. always (< i 11))
  1343. @result{} T
  1344. (loop for i from 0 to 10
  1345. never (> i 11))
  1346. @result{} T
  1347. ;; If I exceeds 10 return I; otherwise, return NIL.
  1348. ;; The THEREIS construct terminates this loop.
  1349. (loop for i from 0
  1350. thereis (when (> i 10) i) )
  1351. @result{} 11
  1352. ;;; The FINALLY clause is not evaluated in these examples.
  1353. (loop for i from 0 to 10
  1354. always (< i 9)
  1355. finally (print "you won't see this"))
  1356. @result{} NIL
  1357. (loop never t
  1358. finally (print "you won't see this"))
  1359. @result{} NIL
  1360. (loop thereis "Here is my value"
  1361. finally (print "you won't see this"))
  1362. @result{} "Here is my value"
  1363. ;; The FOR construct terminates this loop, so the FINALLY clause
  1364. ;; is evaluated.
  1365. (loop for i from 1 to 10
  1366. thereis (> i 11)
  1367. finally (prin1 'got-here))
  1368. @t{ |> } GOT-HERE
  1369. @result{} NIL
  1370. ;; If this code could be used to find a counterexample to Fermat's
  1371. ;; last theorem, it would still not return the value of the
  1372. ;; counterexample because all of the THEREIS clauses in this example
  1373. ;; only return T. But if Fermat is right, that won't matter
  1374. ;; because this won't terminate.
  1375. (loop for z upfrom 2
  1376. thereis
  1377. (loop for n upfrom 3 below (log z 2)
  1378. thereis
  1379. (loop for x below z
  1380. thereis
  1381. (loop for y below z
  1382. thereis (= (+ (expt x n) (expt y n))
  1383. (expt z n))))))
  1384. @end example
  1385. @node Examples of WHILE and UNTIL clauses, , Examples of ALWAYS, Termination Test Clauses
  1386. @subsubsection Examples of WHILE and UNTIL clauses
  1387. @example
  1388. (loop while (hungry-p) do (eat))
  1389. ;; UNTIL NOT is equivalent to WHILE.
  1390. (loop until (not (hungry-p)) do (eat))
  1391. ;; Collect the length and the items of STACK.
  1392. (let ((stack '(a b c d e f)))
  1393. (loop for item = (length stack) then (pop stack)
  1394. collect item
  1395. while stack))
  1396. @result{} (6 A B C D E F)
  1397. ;; Use WHILE to terminate a loop that otherwise wouldn't terminate.
  1398. ;; Note that WHILE occurs after the WHEN.
  1399. (loop for i fixnum from 3
  1400. when (oddp i) collect i
  1401. while (< i 5))
  1402. @result{} (3 5)
  1403. @end example
  1404. @node Unconditional Execution Clauses, Conditional Execution Clauses, Termination Test Clauses, The LOOP Facility
  1405. @subsection Unconditional Execution Clauses
  1406. The @t{do} and @t{doing} constructs
  1407. evaluate the
  1408. supplied @i{forms}
  1409. wherever they occur in the expanded form of @b{loop}.
  1410. The @i{form} argument can be any @i{compound form}.
  1411. Each @i{form} is evaluated in every iteration.
  1412. Because every loop clause must begin with a @i{loop keyword},
  1413. the keyword @t{do} is used when no control action other than execution is
  1414. required.
  1415. The @t{return} construct takes one @i{form}.
  1416. Any @i{values} returned by the @i{form}
  1417. are immediately returned by the @b{loop} form.
  1418. It is equivalent to the clause
  1419. @t{do (return-from @i{block-name} @i{value})},
  1420. where @i{block-name} is the name specified in a @t{named}
  1421. clause, or @b{nil} if there is no @t{named} clause.
  1422. @menu
  1423. * Examples of unconditional execution::
  1424. @end menu
  1425. @node Examples of unconditional execution, , Unconditional Execution Clauses, Unconditional Execution Clauses
  1426. @subsubsection Examples of unconditional execution
  1427. @example
  1428. ;; Print numbers and their squares.
  1429. ;; The DO construct applies to multiple forms.
  1430. (loop for i from 1 to 3
  1431. do (print i)
  1432. (print (* i i)))
  1433. @t{ |> } 1
  1434. @t{ |> } 1
  1435. @t{ |> } 2
  1436. @t{ |> } 4
  1437. @t{ |> } 3
  1438. @t{ |> } 9
  1439. @result{} NIL
  1440. @end example
  1441. @node Conditional Execution Clauses, Miscellaneous Clauses, Unconditional Execution Clauses, The LOOP Facility
  1442. @subsection Conditional Execution Clauses
  1443. The @t{if}, @t{when}, and @t{unless} constructs
  1444. establish conditional control in a @b{loop}. If the test
  1445. passes, the succeeding loop clause is executed. If the test does
  1446. not pass, the succeeding clause is skipped, and program control
  1447. moves to the clause that follows the @i{loop keyword}
  1448. @t{else}. If the test does not pass and no @t{else}
  1449. clause is supplied, control is transferred to the clause or
  1450. construct following the entire conditional clause.
  1451. If conditional clauses are nested, each @t{else} is paired
  1452. with the closest preceding conditional clause that has no
  1453. associated @t{else} or @t{end}.
  1454. In the @t{if} and @t{when} clauses, which are
  1455. synonymous, the test passes if the value of @i{form} is
  1456. @i{true}.
  1457. In the @t{unless} clause,
  1458. the test passes if the value of @i{form} is @i{false}.
  1459. Clauses that follow the test expression can be grouped by using
  1460. the @i{loop keyword} @t{and} to produce a conditional block consisting of
  1461. a compound clause.
  1462. The @i{loop keyword} @t{it} can be used to refer to the result
  1463. of the test expression in a clause.
  1464. Use the @i{loop keyword} @t{it} in place of the form in a
  1465. @t{return} clause or an @i{accumulation} clause that is
  1466. inside a conditional execution clause.
  1467. If multiple clauses are connected with @t{and}, the @t{it}
  1468. construct must be in the first clause in the block.
  1469. The optional @i{loop keyword} @t{end} marks the end of the clause. If this
  1470. keyword is not supplied, the next @i{loop keyword} marks the end. The construct
  1471. @t{end} can be used to distinguish the scoping of compound clauses.
  1472. @menu
  1473. * Examples of WHEN clause::
  1474. @end menu
  1475. @node Examples of WHEN clause, , Conditional Execution Clauses, Conditional Execution Clauses
  1476. @subsubsection Examples of WHEN clause
  1477. @example
  1478. ;; Signal an exceptional condition.
  1479. (loop for item in '(1 2 3 a 4 5)
  1480. when (not (numberp item))
  1481. return (cerror "enter new value" "non-numeric value: ~s" item))
  1482. Error: non-numeric value: A
  1483. ;; The previous example is equivalent to the following one.
  1484. (loop for item in '(1 2 3 a 4 5)
  1485. when (not (numberp item))
  1486. do (return
  1487. (cerror "Enter new value" "non-numeric value: ~s" item)))
  1488. Error: non-numeric value: A
  1489. @end example
  1490. @example
  1491. ;; This example parses a simple printed string representation from
  1492. ;; BUFFER (which is itself a string) and returns the index of the
  1493. ;; closing double-quote character.
  1494. (let ((buffer "\"a\" \"b\""))
  1495. (loop initially (unless (char= (char buffer 0) #\")
  1496. (loop-finish))
  1497. for i of-type fixnum from 1 below (length (the string buffer))
  1498. when (char= (char buffer i) #\")
  1499. return i))
  1500. @result{} 2
  1501. ;; The collected value is returned.
  1502. (loop for i from 1 to 10
  1503. when (> i 5)
  1504. collect i
  1505. finally (prin1 'got-here))
  1506. @t{ |> } GOT-HERE
  1507. @result{} (6 7 8 9 10)
  1508. ;; Return both the count of collected numbers and the numbers.
  1509. (loop for i from 1 to 10
  1510. when (> i 5)
  1511. collect i into number-list
  1512. and count i into number-count
  1513. finally (return (values number-count number-list)))
  1514. @result{} 5, (6 7 8 9 10)
  1515. @end example
  1516. @node Miscellaneous Clauses, Examples of Miscellaneous Loop Features, Conditional Execution Clauses, The LOOP Facility
  1517. @subsection Miscellaneous Clauses
  1518. @menu
  1519. * Control Transfer Clauses::
  1520. * Examples of NAMED clause::
  1521. * Initial and Final Execution::
  1522. @end menu
  1523. @node Control Transfer Clauses, Examples of NAMED clause, Miscellaneous Clauses, Miscellaneous Clauses
  1524. @subsubsection Control Transfer Clauses
  1525. The @t{named} construct
  1526. establishes a name for an @i{implicit block} surrounding the
  1527. entire
  1528. @b{loop} so that the @b{return-from} @i{special operator} can be used to return
  1529. values from or to exit @b{loop}.
  1530. Only one name per @b{loop} @i{form} can be assigned.
  1531. If used, the @t{named} construct must be the first clause in the loop expression.
  1532. The @t{return} construct takes one @i{form}.
  1533. Any @i{values} returned by the @i{form}
  1534. are immediately returned by the @b{loop} form.
  1535. This construct is similar to the @b{return-from} @i{special operator} and the @b{return} @i{macro}.
  1536. The @t{return} construct
  1537. does not execute any @t{finally} clause that
  1538. the @b{loop} @i{form}
  1539. is given.
  1540. @node Examples of NAMED clause, Initial and Final Execution, Control Transfer Clauses, Miscellaneous Clauses
  1541. @subsubsection Examples of NAMED clause
  1542. @example
  1543. ;; Just name and return.
  1544. (loop named max
  1545. for i from 1 to 10
  1546. do (print i)
  1547. do (return-from max 'done))
  1548. @t{ |> } 1
  1549. @result{} DONE
  1550. @end example
  1551. @node Initial and Final Execution, , Examples of NAMED clause, Miscellaneous Clauses
  1552. @subsubsection Initial and Final Execution
  1553. The @t{initially} and @t{finally} constructs
  1554. evaluate forms that occur before and after the loop body.
  1555. The @t{initially} construct causes the supplied
  1556. @i{compound-forms}
  1557. to be evaluated
  1558. in the loop prologue, which precedes all loop code except for
  1559. initial settings supplied by constructs @t{with}, @t{for}, or
  1560. @t{as}.
  1561. The code for any @t{initially} clauses is
  1562. executed
  1563. in the order in which the clauses appeared in
  1564. the @b{loop}.
  1565. The @t{finally} construct causes the supplied
  1566. @i{compound-forms}
  1567. to be evaluated
  1568. in the loop epilogue after normal iteration terminates.
  1569. The code for any @t{finally} clauses is
  1570. executed
  1571. in the order in which the clauses appeared in
  1572. the @b{loop}. The collected code is executed once in the loop epilogue
  1573. before any implicit values are returned from the accumulation clauses.
  1574. An explicit transfer of control (@i{e.g.}, by @b{return}, @b{go}, or @b{throw})
  1575. from the loop body, however, will exit the
  1576. @b{loop} without executing the epilogue code.
  1577. Clauses such as @t{return}, @t{always}, @t{never}, and
  1578. @t{thereis}
  1579. can bypass the @t{finally} clause.
  1580. @b{return} (or @b{return-from}, if the @t{named} option was supplied)
  1581. can be used after @t{finally} to return values from a @b{loop}.
  1582. Such an @i{explicit return}
  1583. inside the
  1584. @t{finally} clause takes precedence over returning the accumulation
  1585. from clauses supplied by such keywords as @t{collect}, @t{nconc},
  1586. @t{append}, @t{sum}, @t{count}, @t{maximize}, and
  1587. @t{minimize};
  1588. the accumulation values for these preempted clauses are not returned by
  1589. @b{loop} if @b{return} or @b{return-from} is used.
  1590. @node Examples of Miscellaneous Loop Features, Notes about Loop, Miscellaneous Clauses, The LOOP Facility
  1591. @subsection Examples of Miscellaneous Loop Features
  1592. @example
  1593. (let ((i 0)) ; no loop keywords are used
  1594. (loop (incf i) (if (= i 3) (return i)))) @result{} 3
  1595. (let ((i 0)(j 0))
  1596. (tagbody
  1597. (loop (incf j 3) (incf i) (if (= i 3) (go exit)))
  1598. exit)
  1599. j) @result{} 9
  1600. @end example
  1601. In the following example, the variable @t{x} is stepped
  1602. before @t{y} is stepped; thus, the value of @t{y}
  1603. reflects the updated value of @t{x}:
  1604. @example
  1605. (loop for x from 1 to 10
  1606. for y = nil then x
  1607. collect (list x y))
  1608. @result{} ((1 NIL) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) (10 10))
  1609. @end example
  1610. In this example, @t{x} and @t{y} are stepped in @i{parallel}:
  1611. @example
  1612. (loop for x from 1 to 10
  1613. and y = nil then x
  1614. collect (list x y))
  1615. @result{} ((1 NIL) (2 1) (3 2) (4 3) (5 4) (6 5) (7 6) (8 7) (9 8) (10 9))
  1616. @end example
  1617. @menu
  1618. * Examples of clause grouping::
  1619. @end menu
  1620. @node Examples of clause grouping, , Examples of Miscellaneous Loop Features, Examples of Miscellaneous Loop Features
  1621. @subsubsection Examples of clause grouping
  1622. @example
  1623. ;; Group conditional clauses.
  1624. (loop for i in '(1 324 2345 323 2 4 235 252)
  1625. when (oddp i)
  1626. do (print i)
  1627. and collect i into odd-numbers
  1628. and do (terpri)
  1629. else ; I is even.
  1630. collect i into even-numbers
  1631. finally
  1632. (return (values odd-numbers even-numbers)))
  1633. @t{ |> } 1
  1634. @t{ |> }
  1635. @t{ |> } 2345
  1636. @t{ |> }
  1637. @t{ |> } 323
  1638. @t{ |> }
  1639. @t{ |> } 235
  1640. @result{} (1 2345 323 235), (324 2 4 252)
  1641. ;; Collect numbers larger than 3.
  1642. (loop for i in '(1 2 3 4 5 6)
  1643. when (and (> i 3) i)
  1644. collect it) ; IT refers to (and (> i 3) i).
  1645. @result{} (4 5 6)
  1646. ;; Find a number in a list.
  1647. (loop for i in '(1 2 3 4 5 6)
  1648. when (and (> i 3) i)
  1649. return it)
  1650. @result{} 4
  1651. ;; The above example is similar to the following one.
  1652. (loop for i in '(1 2 3 4 5 6)
  1653. thereis (and (> i 3) i))
  1654. @result{} 4
  1655. ;; Nest conditional clauses.
  1656. (let ((list '(0 3.0 apple 4 5 9.8 orange banana)))
  1657. (loop for i in list
  1658. when (numberp i)
  1659. when (floatp i)
  1660. collect i into float-numbers
  1661. else ; Not (floatp i)
  1662. collect i into other-numbers
  1663. else ; Not (numberp i)
  1664. when (symbolp i)
  1665. collect i into symbol-list
  1666. else ; Not (symbolp i)
  1667. do (error "found a funny value in list ~S, value ~S~
  1668. finally (return (values float-numbers other-numbers symbol-list))))
  1669. @result{} (3.0 9.8), (0 4 5), (APPLE ORANGE BANANA)
  1670. ;; Without the END preposition, the last AND would apply to the
  1671. ;; inner IF rather than the outer one.
  1672. (loop for x from 0 to 3
  1673. do (print x)
  1674. if (zerop (mod x 2))
  1675. do (princ " a")
  1676. and if (zerop (floor x 2))
  1677. do (princ " b")
  1678. end
  1679. and do (princ " c"))
  1680. @t{ |> } 0 a b c
  1681. @t{ |> } 1
  1682. @t{ |> } 2 a c
  1683. @t{ |> } 3
  1684. @result{} NIL
  1685. @end example
  1686. @node Notes about Loop, , Examples of Miscellaneous Loop Features, The LOOP Facility
  1687. @subsection Notes about Loop
  1688. @i{Types} can be supplied for loop variables.
  1689. It is not necessary to supply a @i{type} for any variable,
  1690. but supplying the @i{type}
  1691. can ensure that the variable has a correctly typed initial value,
  1692. and it can also enable compiler optimizations
  1693. (depending on the @i{implementation}).
  1694. The clause @t{repeat} @i{n} ... is roughly equivalent to a clause such as
  1695. @example
  1696. (loop for @i{internal-variable} downfrom (- @i{n} 1) to 0 ...)
  1697. @end example
  1698. but in some @i{implementations},
  1699. the @t{repeat} construct might be more efficient.
  1700. Within the executable parts of the loop clauses and around the entire
  1701. @b{loop} form, variables can be bound by using @b{let}.
  1702. Use caution when using a variable named @t{IT} (in any @i{package})
  1703. in connection with @b{loop}, since @t{it} is a @i{loop keyword}
  1704. that can be used in place of a @i{form} in certain contexts.
  1705. There is
  1706. no
  1707. @i{standardized}
  1708. mechanism for users to add
  1709. extensions to @b{loop}.
  1710. @c end of including concept-loop
  1711. @node Iteration Dictionary, , The LOOP Facility, Iteration
  1712. @section Iteration Dictionary
  1713. @c including dict-iteration
  1714. @menu
  1715. * do::
  1716. * dotimes::
  1717. * dolist::
  1718. * loop::
  1719. * loop-finish::
  1720. @end menu
  1721. @node do, dotimes, Iteration Dictionary, Iteration Dictionary
  1722. @subsection do, do* [Macro]
  1723. @code{do} @i{@r{(}@{@i{var} | @r{(}@i{var} @r{[}init-form @r{[}step-form@r{]}@r{]}@r{)}@}{*}@r{)}
  1724. @r{(}end-test-form @{@i{result-form}@}{*}@r{)}
  1725. @{@i{declaration}@}{*} @{tag | statement@}{*}}@*
  1726. @result{} @i{@{@i{result}@}{*}}
  1727. @code{do*} @i{@r{(}@{@i{var} | @r{(}@i{var} @r{[}init-form @r{[}step-form@r{]}@r{]}@r{)}@}{*}@r{)}
  1728. @r{(}end-test-form {@{@i{result-form}@}{*}}@r{)}
  1729. @{@i{declaration}@}{*} @{tag | statement@}{*}}@*
  1730. @result{} @i{@{@i{result}@}{*}}
  1731. @subsubheading Arguments and Values::
  1732. @i{var}---a @i{symbol}.
  1733. @i{init-form}---a @i{form}.
  1734. @i{step-form}---a @i{form}.
  1735. @i{end-test-form}---a @i{form}.
  1736. @i{result-forms}---an @i{implicit progn}.
  1737. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1738. @i{tag}---a @i{go tag}; not evaluated.
  1739. @i{statement}---a @i{compound form}; evaluated as described below.
  1740. @i{results}---if a @b{return} or @b{return-from} form is executed,
  1741. the @i{values} passed from that @i{form};
  1742. otherwise, the @i{values} returned by the @i{result-forms}.
  1743. @subsubheading Description::
  1744. @b{do} iterates over a group of @i{statements}
  1745. while a test condition holds.
  1746. @b{do} accepts an arbitrary number of iteration @i{vars}
  1747. which are bound within the iteration and stepped in parallel.
  1748. An initial value may be supplied for each iteration variable by use of
  1749. an @i{init-form}.
  1750. @i{Step-forms} may be used to specify how the
  1751. @i{vars} should be updated on succeeding iterations through the loop.
  1752. @i{Step-forms} may be used both to generate successive
  1753. values or to accumulate results.
  1754. If the @i{end-test-form} condition
  1755. is met prior to an execution of the body, the iteration terminates.
  1756. @i{Tags} label @i{statements}.
  1757. @b{do*} is exactly like @b{do}
  1758. except that the @i{bindings} and steppings
  1759. of the @i{vars} are performed sequentially rather than in parallel.
  1760. Before the first iteration, all the @i{init-forms} are evaluated, and
  1761. each @i{var} is bound to the value of its respective @i{init-form},
  1762. if supplied.
  1763. This is a @i{binding}, not an assignment; when the loop terminates,
  1764. the old values of those variables will be restored.
  1765. For @b{do}, all
  1766. of the @i{init-forms} are evaluated before any @i{var}
  1767. is bound. The
  1768. @i{init-forms} can refer to the @i{bindings} of the @i{vars}
  1769. visible before beginning execution of
  1770. @b{do}.
  1771. For @b{do*}, the first @i{init-form} is evaluated, then the first
  1772. @i{var} is bound to that value, then the second @i{init-form}
  1773. is evaluated, then the second @i{var} is bound, and so on;
  1774. in general, the @i{k}th @i{init-form} can refer to the new binding
  1775. of the @i{j}th @i{var} if @i{j} < @i{k}, and otherwise to the
  1776. old binding of the @i{j}th @i{var}.
  1777. At the beginning of each iteration, after processing the variables,
  1778. the @i{end-test-form} is evaluated. If the result is
  1779. @i{false}, execution proceeds with the body of the @b{do}
  1780. (or @b{do*}) form.
  1781. If the result is @i{true}, the @i{result-forms} are evaluated in order
  1782. as an @i{implicit progn},
  1783. and then @b{do} or @b{do*} returns.
  1784. At the beginning of each iteration other than the first,
  1785. @i{vars} are updated as follows. All the @i{step-forms}, if supplied,
  1786. are evaluated, from left to right, and the resulting values are
  1787. assigned to the respective @i{vars}.
  1788. Any @i{var} that has no associated @i{step-form} is not assigned to.
  1789. For @b{do}, all the @i{step-forms} are evaluated before any @i{var}
  1790. is updated; the assignment of values to @i{vars} is done in parallel,
  1791. as if by @b{psetq}.
  1792. Because all of the @i{step-forms} are evaluated before any
  1793. of the @i{vars} are altered, a @i{step-form} when evaluated always has
  1794. access to the old values of all the @i{vars}, even if other @i{step-forms}
  1795. precede it.
  1796. For @b{do*}, the first @i{step-form} is evaluated, then the
  1797. value is assigned to the first @i{var}, then the second @i{step-form}
  1798. is evaluated, then the value is assigned to the second @i{var}, and so on;
  1799. the assignment of values to variables is done sequentially,
  1800. as if by @b{setq}.
  1801. For either @b{do} or @b{do*},
  1802. after the @i{vars} have been updated,
  1803. the @i{end-test-form}
  1804. is evaluated as described above, and the iteration continues.
  1805. The remainder of the @b{do} (or @b{do*}) form constitutes
  1806. an @i{implicit tagbody}.
  1807. @i{Tags} may appear within the body of a @b{do} loop
  1808. for use by @b{go} statements appearing in the body (but such @b{go}
  1809. statements may not appear in the variable specifiers, the @i{end-test-form},
  1810. or the @i{result-forms}).
  1811. When the end of a @b{do} body is reached, the next iteration cycle
  1812. (beginning with the evaluation of @i{step-forms}) occurs.
  1813. An @i{implicit block} named @b{nil} surrounds the entire @b{do}
  1814. (or @b{do*}) form.
  1815. A @b{return} statement may be used at any point to exit the loop
  1816. immediately.
  1817. @i{Init-form} is an
  1818. initial value for the @i{var} with which it is associated.
  1819. If @i{init-form} is omitted, the initial value of @i{var} is @b{nil}.
  1820. If a @i{declaration} is supplied for a @i{var}, @i{init-form}
  1821. must be consistent with the @i{declaration}.
  1822. @i{Declarations} can appear at the beginning of a @b{do}
  1823. (or @b{do*}) body.
  1824. They apply to code in the @b{do} (or @b{do*}) body,
  1825. to the @i{bindings} of the @b{do} (or @b{do*})
  1826. @i{vars},
  1827. to the @i{step-forms},
  1828. to the @i{end-test-form}, and to the @i{result-forms}.
  1829. @subsubheading Examples::
  1830. @example
  1831. (do ((temp-one 1 (1+ temp-one))
  1832. (temp-two 0 (1- temp-two)))
  1833. ((> (- temp-one temp-two) 5) temp-one)) @result{} 4
  1834. (do ((temp-one 1 (1+ temp-one))
  1835. (temp-two 0 (1+ temp-one)))
  1836. ((= 3 temp-two) temp-one)) @result{} 3
  1837. (do* ((temp-one 1 (1+ temp-one))
  1838. (temp-two 0 (1+ temp-one)))
  1839. ((= 3 temp-two) temp-one)) @result{} 2
  1840. (do ((j 0 (+ j 1)))
  1841. (nil) ;Do forever.
  1842. (format t "~
  1843. (let ((item (read)))
  1844. (if (null item) (return) ;Process items until NIL seen.
  1845. (format t "~&Output ~D: ~S" j item))))
  1846. @t{ |> } Input 0: @b{|>>}@t{banana}@b{<<|}
  1847. @t{ |> } Output 0: BANANA
  1848. @t{ |> } Input 1: @b{|>>}@t{(57 boxes)}@b{<<|}
  1849. @t{ |> } Output 1: (57 BOXES)
  1850. @t{ |> } Input 2: @b{|>>}@t{NIL}@b{<<|}
  1851. @result{} NIL
  1852. (setq a-vector (vector 1 nil 3 nil))
  1853. (do ((i 0 (+ i 1)) ;Sets every null element of a-vector to zero.
  1854. (n (array-dimension a-vector 0)))
  1855. ((= i n))
  1856. (when (null (aref a-vector i))
  1857. (setf (aref a-vector i) 0))) @result{} NIL
  1858. a-vector @result{} #(1 0 3 0)
  1859. @end example
  1860. @example
  1861. (do ((x e (cdr x))
  1862. (oldx x x))
  1863. ((null x))
  1864. body)
  1865. @end example
  1866. is an example of parallel assignment to index variables. On the first
  1867. iteration, the value of @t{oldx} is whatever value @t{x} had before
  1868. the @b{do} was entered. On succeeding iterations, @t{oldx} contains
  1869. the value that @t{x} had on the previous iteration.
  1870. @example
  1871. (do ((x foo (cdr x))
  1872. (y bar (cdr y))
  1873. (z '() (cons (f (car x) (car y)) z)))
  1874. ((or (null x) (null y))
  1875. (nreverse z)))
  1876. @end example
  1877. does the same thing as @t{(mapcar #'f foo bar)}. The step
  1878. computation for @t{z} is an example of the fact that variables
  1879. are stepped in parallel.
  1880. Also, the body of the loop is empty.
  1881. @example
  1882. (defun list-reverse (list)
  1883. (do ((x list (cdr x))
  1884. (y '() (cons (car x) y)))
  1885. ((endp x) y)))
  1886. @end example
  1887. As an example of nested iterations, consider a data structure that is a
  1888. @i{list} of @i{conses}. The @i{car} of each @i{cons} is a
  1889. @i{list} of @i{symbols},
  1890. and the @i{cdr} of each @i{cons} is a
  1891. @i{list} of equal length containing
  1892. corresponding values. Such a data structure is similar to an association
  1893. list,
  1894. but is divided into ``frames''; the overall structure resembles a rib-cage.
  1895. A lookup function on such a data structure might be:
  1896. @example
  1897. (defun ribcage-lookup (sym ribcage)
  1898. (do ((r ribcage (cdr r)))
  1899. ((null r) nil)
  1900. (do ((s (caar r) (cdr s))
  1901. (v (cdar r) (cdr v)))
  1902. ((null s))
  1903. (when (eq (car s) sym)
  1904. (return-from ribcage-lookup (car v)))))) @result{} RIBCAGE-LOOKUP
  1905. @end example
  1906. @subsubheading See Also::
  1907. other iteration functions
  1908. (
  1909. @ref{dolist}
  1910. ,
  1911. @ref{dotimes}
  1912. , and
  1913. @ref{loop}
  1914. )
  1915. and more primitive functionality
  1916. (
  1917. @ref{tagbody}
  1918. ,
  1919. @ref{go}
  1920. ,
  1921. @ref{block}
  1922. ,
  1923. @ref{return}
  1924. ,
  1925. @ref{let; let*}
  1926. , and
  1927. @ref{setq}
  1928. )
  1929. @subsubheading Notes::
  1930. If @i{end-test-form} is @b{nil}, the test will never succeed.
  1931. This provides an idiom for ``do forever'':
  1932. the body of the @b{do} or @b{do*}
  1933. is executed repeatedly.
  1934. The infinite loop can be terminated by the use of @b{return},
  1935. @b{return-from}, @b{go} to an outer level, or @b{throw}.
  1936. A @b{do} @i{form} may be explained in terms of the more primitive @i{forms}
  1937. @b{block}, @b{return},
  1938. @b{let}, @b{loop}, @b{tagbody},
  1939. and @b{psetq} as follows:
  1940. @example
  1941. (block nil
  1942. (let ((var1 init1)
  1943. (var2 init2)
  1944. ...
  1945. (varn initn))
  1946. @i{declarations}
  1947. (loop (when end-test (return (progn . result)))
  1948. (tagbody . tagbody)
  1949. (psetq var1 step1
  1950. var2 step2
  1951. ...
  1952. varn stepn))))
  1953. @end example
  1954. @b{do*} is similar, except that @b{let*} and @b{setq} replace
  1955. the @b{let} and @b{psetq}, respectively.
  1956. @node dotimes, dolist, do, Iteration Dictionary
  1957. @subsection dotimes [Macro]
  1958. @code{dotimes} @i{@r{(}var count-form @r{[}result-form@r{]}@r{)}
  1959. @{@i{declaration}@}{*}
  1960. @{tag | statement@}{*}}@*
  1961. @result{} @i{@{@i{result}@}{*}}
  1962. @subsubheading Arguments and Values::
  1963. @i{var}---a @i{symbol}.
  1964. @i{count-form}---a @i{form}.
  1965. @i{result-form}---a @i{form}.
  1966. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1967. @i{tag}---a @i{go tag}; not evaluated.
  1968. @i{statement}---a @i{compound form}; evaluated as described below.
  1969. @i{results}---if a @b{return} or @b{return-from} form is executed,
  1970. the @i{values} passed from that @i{form};
  1971. otherwise, the @i{values} returned by the @i{result-form}
  1972. or @b{nil} if there is no @i{result-form}.
  1973. @subsubheading Description::
  1974. @b{dotimes} iterates over a series of @i{integers}.
  1975. @b{dotimes} evaluates @i{count-form},
  1976. which should produce an @i{integer}.
  1977. If @i{count-form} is zero or negative,
  1978. the body is not executed.
  1979. @b{dotimes} then executes the body once for each @i{integer} from 0 up to
  1980. but not including
  1981. the value of @i{count-form},
  1982. in the order in which the
  1983. @i{tags} and @i{statements} occur, with
  1984. @i{var} bound to each @i{integer}.
  1985. Then @i{result-form}
  1986. is evaluated.
  1987. At the time @i{result-form} is processed, @i{var} is bound to
  1988. the number of times the body was executed.
  1989. @i{Tags} label
  1990. @i{statements}.
  1991. An @i{implicit block}
  1992. named @b{nil} surrounds @b{dotimes}.
  1993. @b{return} may be used to terminate the loop immediately without
  1994. performing any further iterations, returning zero or more @i{values}.
  1995. The body of the loop is an @i{implicit tagbody};
  1996. it may contain tags to serve as the targets of @b{go} statements.
  1997. Declarations may appear before the body of the loop.
  1998. The @i{scope} of the binding of @i{var}
  1999. does not include the @i{count-form},
  2000. but the @i{result-form} is included.
  2001. It is @i{implementation-dependent} whether @b{dotimes}
  2002. @i{establishes} a new @i{binding} of @i{var} on each iteration
  2003. or whether it @i{establishes} a binding for @i{var} once at the
  2004. beginning and then @i{assigns} it on any subsequent iterations.
  2005. @subsubheading Examples::
  2006. @example
  2007. (dotimes (temp-one 10 temp-one)) @result{} 10
  2008. (setq temp-two 0) @result{} 0
  2009. (dotimes (temp-one 10 t) (incf temp-two)) @result{} T
  2010. temp-two @result{} 10
  2011. @end example
  2012. Here is an example of the use of @t{dotimes} in processing strings:
  2013. @example
  2014. ;;; True if the specified subsequence of the string is a
  2015. ;;; palindrome (reads the same forwards and backwards).
  2016. (defun palindromep (string @t{&optional}
  2017. (start 0)
  2018. (end (length string)))
  2019. (dotimes (k (floor (- end start) 2) t)
  2020. (unless (char-equal (char string (+ start k))
  2021. (char string (- end k 1)))
  2022. (return nil))))
  2023. (palindromep "Able was I ere I saw Elba") @result{} T
  2024. (palindromep "A man, a plan, a canal--Panama!") @result{} NIL
  2025. (remove-if-not #'alpha-char-p ;Remove punctuation.
  2026. "A man, a plan, a canal--Panama!")
  2027. @result{} "AmanaplanacanalPanama"
  2028. (palindromep
  2029. (remove-if-not #'alpha-char-p
  2030. "A man, a plan, a canal--Panama!")) @result{} T
  2031. (palindromep
  2032. (remove-if-not
  2033. #'alpha-char-p
  2034. "Unremarkable was I ere I saw Elba Kramer, nu?")) @result{} T
  2035. (palindromep
  2036. (remove-if-not
  2037. #'alpha-char-p
  2038. "A man, a plan, a cat, a ham, a yak,
  2039. a yam, a hat, a canal--Panama!")) @result{} T
  2040. @end example
  2041. @subsubheading See Also::
  2042. @ref{do; do*}
  2043. ,
  2044. @ref{dolist}
  2045. ,
  2046. @ref{tagbody}
  2047. @subsubheading Notes::
  2048. @b{go} may be used within the body of
  2049. @b{dotimes} to transfer control to a statement labeled by a @i{tag}.
  2050. @node dolist, loop, dotimes, Iteration Dictionary
  2051. @subsection dolist [Macro]
  2052. @code{dolist} @i{@r{(}var list-form @r{[}result-form@r{]}@r{)}
  2053. @{@i{declaration}@}{*}
  2054. @{tag | statement@}{*}}@*
  2055. @result{} @i{@{@i{result}@}{*}}
  2056. @subsubheading Arguments and Values::
  2057. @i{var}---a @i{symbol}.
  2058. @i{list-form}---a @i{form}.
  2059. @i{result-form}---a @i{form}.
  2060. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  2061. @i{tag}---a @i{go tag}; not evaluated.
  2062. @i{statement}---a @i{compound form}; evaluated as described below.
  2063. @i{results}---if a @b{return} or @b{return-from} form is executed,
  2064. the @i{values} passed from that @i{form};
  2065. otherwise, the @i{values} returned by the @i{result-form}
  2066. or @b{nil} if there is no @i{result-form}.
  2067. @subsubheading Description::
  2068. @b{dolist} iterates over the elements of a @i{list}.
  2069. The body of @b{dolist} is like a @b{tagbody}.
  2070. It consists of a series of @i{tags} and @i{statements}.
  2071. @b{dolist}
  2072. evaluates @i{list-form},
  2073. which should produce a @i{list}. It then executes the body
  2074. once for each element in the @i{list}, in the order in which the
  2075. @i{tags} and @i{statements} occur, with
  2076. @i{var} bound to the element.
  2077. Then @i{result-form}
  2078. is evaluated.
  2079. @i{tags} label
  2080. @i{statements}.
  2081. At the time @i{result-form} is processed,
  2082. @i{var} is bound to @b{nil}.
  2083. An @i{implicit block}
  2084. named @b{nil} surrounds @b{dolist}.
  2085. @b{return} may be used to terminate the loop immediately without
  2086. performing any further iterations, returning zero or more @i{values}.
  2087. The @i{scope} of the binding of @i{var}
  2088. does not include the @i{list-form},
  2089. but the @i{result-form} is included.
  2090. It is @i{implementation-dependent} whether @b{dolist}
  2091. @i{establishes} a new @i{binding} of @i{var} on each iteration
  2092. or whether it @i{establishes} a binding for @i{var} once at the
  2093. beginning and then @i{assigns} it on any subsequent iterations.
  2094. @subsubheading Examples::
  2095. @example
  2096. (setq temp-two '()) @result{} NIL
  2097. (dolist (temp-one '(1 2 3 4) temp-two) (push temp-one temp-two)) @result{} (4 3 2 1)
  2098. (setq temp-two 0) @result{} 0
  2099. (dolist (temp-one '(1 2 3 4)) (incf temp-two)) @result{} NIL
  2100. temp-two @result{} 4
  2101. (dolist (x '(a b c d)) (prin1 x) (princ " "))
  2102. @t{ |> } A B C D
  2103. @result{} NIL
  2104. @end example
  2105. @subsubheading See Also::
  2106. @ref{do; do*}
  2107. ,
  2108. @ref{dotimes}
  2109. ,
  2110. @ref{tagbody}
  2111. ,
  2112. @ref{Traversal Rules and Side Effects}
  2113. @subsubheading Notes::
  2114. @b{go} may be used within the body of @b{dolist}
  2115. to transfer control to a statement labeled by a @i{tag}.
  2116. @node loop, loop-finish, dolist, Iteration Dictionary
  2117. @subsection loop [Macro]
  2118. The ``simple'' @b{loop} @i{form}:
  2119. @code{loop} @i{@{@i{compound-form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  2120. The ``extended'' @b{loop} @i{form}:
  2121. @code{loop} @i{@r{[}!@i{name-clause}@r{]}
  2122. @{!@i{variable-clause}@}{*}
  2123. @{!@i{main-clause}@}{*}} @result{} @i{@{@i{result}@}{*}}
  2124. @w{@i{name-clause} ::=@t{named} @i{name}}
  2125. @w{@i{variable-clause} ::=!@i{with-clause} | !@i{initial-final} | !@i{for-as-clause}}
  2126. @w{@i{with-clause} ::=@t{with} @i{var1} @r{[}@i{type-spec}@r{]} @r{[}= @i{form1}@r{]} @{{and} @i{var2} @r{[}@i{type-spec}@r{]} @r{[}= @i{form2}@r{]}@}{*}}
  2127. @w{@i{main-clause} ::=!@i{unconditional} | !@i{accumulation} | !@i{conditional} | !@i{termination-test} | !@i{initial-final}}
  2128. @w{@i{initial-final} ::=@t{initially} @{@i{compound-form}@}^+ | @t{finally} @{@i{compound-form}@}^+}
  2129. @w{@i{unconditional} ::=@{{do} | @t{doing}@} @{@i{compound-form}@}^+ | @t{return} @{@i{form} | @t{it}@}}
  2130. @w{@i{accumulation} ::=!@i{list-accumulation} | !@i{numeric-accumulation}}
  2131. @w{@i{list-accumulation} ::=@{{collect} | @t{collecting} | @t{append} | @t{appending} | @t{nconc} | @t{nconcing}@} @{@i{form} | @t{it}@} }
  2132. @w{ @r{[}@t{into} @i{simple-var}@r{]}}
  2133. @w{@i{numeric-accumulation} ::=@{{count} | @t{counting} | @t{sum} | @t{summing} | @}
  2134. @w{ @t{maximize} | @t{maximizing} | @t{minimize} | @t{minimizing}} @{@i{form} | @t{it}@} }
  2135. @w{ @r{[}@t{into} @i{simple-var}@r{]} @r{[}@i{type-spec}@r{]}}
  2136. @w{@i{conditional} ::=@{{if} | @t{when} | @t{unless}@} @i{form} !@i{selectable-clause} @{{and} !@i{selectable-clause}@}{*} }
  2137. @w{ @r{[}@t{else} !@i{selectable-clause} @{{and} !@i{selectable-clause}@}{*}@r{]} }
  2138. @w{ @r{[}@t{end}@r{]}}
  2139. @w{@i{selectable-clause} ::=!@i{unconditional} | !@i{accumulation} | !@i{conditional}}
  2140. @w{@i{termination-test} ::=@t{while} @i{form} | @t{until} @i{form} | @t{repeat} @i{form} | @t{always} @i{form} | @t{never} @i{form} | @t{thereis} @i{form}}
  2141. @w{@i{for-as-clause} ::=@{{for} | @t{as}@} !@i{for-as-subclause} @{{and} !@i{for-as-subclause}@}{*}}
  2142. @w{@i{for-as-subclause} ::=!@i{for-as-arithmetic} | !@i{for-as-in-list} | !@i{for-as-on-list} | !@i{for-as-equals-then} |}
  2143. @w{ !@i{for-as-across} | !@i{for-as-hash} | !@i{for-as-package}}
  2144. @w{@i{for-as-arithmetic} ::=@i{var} @r{[}@i{type-spec}@r{]} !@i{for-as-arithmetic-subclause}}
  2145. @w{@i{for-as-arithmetic-subclause} ::=!@i{arithmetic-up} | !@i{arithmetic-downto} | !@i{arithmetic-downfrom}}
  2146. @w{@i{arithmetic-up} ::=[[@{{from} | @t{upfrom}@} @i{form1} | @{{to} | @t{upto} | @t{below}@} @i{form2} | @t{by} @i{form3}]]^+}
  2147. @w{@i{arithmetic-downto} ::=[[@{{from} @i{form1}@}^1 | @{@{{downto} | @t{above}@} @i{form2}@}^1 | @t{by} @i{form3}]]}
  2148. @w{@i{arithmetic-downfrom} ::=[[@{{downfrom} @i{form1}@}^1 | @{{to} | @t{downto} | @t{above}@} @i{form2} | @t{by} @i{form3}]]}
  2149. @w{@i{for-as-in-list} ::=@i{var} @r{[}@i{type-spec}@r{]} @t{in} @i{form1} @r{[}@t{by} @i{step-fun}@r{]}}
  2150. @w{@i{for-as-on-list} ::=@i{var} @r{[}@i{type-spec}@r{]} @t{on} @i{form1} @r{[}@t{by} @i{step-fun}@r{]}}
  2151. @w{@i{for-as-equals-then} ::=@i{var} @r{[}@i{type-spec}@r{]} = @i{form1} @r{[}@t{then} @i{form2}@r{]}}
  2152. @w{@i{for-as-across} ::=@i{var} @r{[}@i{type-spec}@r{]} @t{across} @i{vector}}
  2153. @w{@i{for-as-hash} ::=@i{var} @r{[}@i{type-spec}@r{]} @t{being} @{{each} | @t{the}@} }
  2154. @w{ @{@{{hash-key} | @t{hash-keys}@} @{{in} | @t{of}@} @i{hash-table} }
  2155. @w{ @r{[}@t{using} @r{(}@t{hash-value} @i{other-var}@r{)}@r{]} | }
  2156. @w{ @{{hash-value} | @t{hash-values}@} @{{in} | @t{of}@} @i{hash-table} }
  2157. @w{ @r{[}@t{using} @r{(}@t{hash-key} @i{other-var}@r{)}@r{]}@}}
  2158. @w{@i{for-as-package} ::=@i{var} @r{[}@i{type-spec}@r{]} @t{being} @{{each} | @t{the}@} }
  2159. @w{ @{{symbol} | @t{symbols} |}
  2160. @w{ @t{present-symbol} | @t{present-symbols} |}
  2161. @w{ @t{external-symbol} | @t{external-symbols}@} }
  2162. @w{ @r{[}@{{in} | @t{of}@} @i{package}@r{]}}
  2163. @w{@i{type-spec} ::=!@i{simple-type-spec} | !@i{destructured-type-spec}}
  2164. @w{@i{simple-type-spec} ::=@b{fixnum} | @b{float} | @b{t} | @b{nil}}
  2165. @w{@i{destructured-type-spec} ::=@t{of-type} @i{d-type-spec}}
  2166. @w{@i{d-type-spec} ::=@i{type-specifier} | @t{(@i{d-type-spec} . @i{d-type-spec})}}
  2167. @w{@i{var} ::=!@i{d-var-spec}}
  2168. @w{@i{var1} ::=!@i{d-var-spec}}
  2169. @w{@i{var2} ::=!@i{d-var-spec}}
  2170. @w{@i{other-var} ::=!@i{d-var-spec}}
  2171. @w{@i{d-var-spec} ::=@i{simple-var} | @b{nil} | @r{(}!@i{d-var-spec} @t{.} !@i{d-var-spec}@r{)}}
  2172. @subsubheading Arguments and Values::
  2173. @i{compound-form}---a @i{compound form}.
  2174. @i{name}---a @i{symbol}.
  2175. @i{simple-var}---a @i{symbol} (a @i{variable} name).
  2176. @i{form}, @i{form1}, @i{form2}, @i{form3}---a @i{form}.
  2177. @i{step-fun}---a @i{form} that evaluates to a @i{function} of one @i{argument}.
  2178. @i{vector}---a @i{form} that evaluates to a @i{vector}.
  2179. @i{hash-table}---a @i{form} that evaluates to a @i{hash table}.
  2180. @i{package}---a @i{form} that evaluates to a @i{package designator}.
  2181. @i{type-specifier}---a @i{type specifier}.
  2182. This might be either an @i{atomic type specifier} or a @i{compound type specifier},
  2183. which introduces some additional complications to proper parsing in the face of
  2184. destructuring; for further information, see @ref{Destructuring}.
  2185. @i{result}---an @i{object}.
  2186. @subsubheading Description::
  2187. For details, see @ref{The LOOP Facility}.
  2188. @subsubheading Examples::
  2189. @example
  2190. ;; An example of the simple form of LOOP.
  2191. (defun sqrt-advisor ()
  2192. (loop (format t "~&Number: ")
  2193. (let ((n (parse-integer (read-line) :junk-allowed t)))
  2194. (when (not n) (return))
  2195. (format t "~&The square root of ~D is ~D.~
  2196. @result{} SQRT-ADVISOR
  2197. (sqrt-advisor)
  2198. @t{ |> } Number: @b{|>>}@t{5{@i{[<--}~]}}@b{<<|}
  2199. @t{ |> } The square root of 5 is 2.236068.
  2200. @t{ |> } Number: @b{|>>}@t{4{@i{[<--}~]}}@b{<<|}
  2201. @t{ |> } The square root of 4 is 2.
  2202. @t{ |> } Number: @b{|>>}@t{done{@i{[<--}~]}}@b{<<|}
  2203. @result{} NIL
  2204. ;; An example of the extended form of LOOP.
  2205. (defun square-advisor ()
  2206. (loop as n = (progn (format t "~&Number: ")
  2207. (parse-integer (read-line) :junk-allowed t))
  2208. while n
  2209. do (format t "~&The square of ~D is ~D.~
  2210. @result{} SQUARE-ADVISOR
  2211. (square-advisor)
  2212. @t{ |> } Number: @b{|>>}@t{4{@i{[<--}~]}}@b{<<|}
  2213. @t{ |> } The square of 4 is 16.
  2214. @t{ |> } Number: @b{|>>}@t{23{@i{[<--}~]}}@b{<<|}
  2215. @t{ |> } The square of 23 is 529.
  2216. @t{ |> } Number: @b{|>>}@t{done{@i{[<--}~]}}@b{<<|}
  2217. @result{} NIL
  2218. ;; Another example of the extended form of LOOP.
  2219. (loop for n from 1 to 10
  2220. when (oddp n)
  2221. collect n)
  2222. @result{} (1 3 5 7 9)
  2223. @end example
  2224. @subsubheading See Also::
  2225. @ref{do; do*}
  2226. ,
  2227. @ref{dolist}
  2228. ,
  2229. @ref{dotimes}
  2230. ,
  2231. @ref{return}
  2232. ,
  2233. @ref{go}
  2234. ,
  2235. @ref{throw}
  2236. ,
  2237. @ref{Destructuring}
  2238. @subsubheading Notes::
  2239. Except that @b{loop-finish} cannot be used within a simple @b{loop} @i{form},
  2240. a simple @b{loop} @i{form} is related to an extended @b{loop} @i{form}
  2241. in the following way:
  2242. @example
  2243. (loop @{@i{compound-form}@}{*}) @equiv{} (loop do @{@i{compound-form}@}{*})
  2244. @end example
  2245. @node loop-finish, , loop, Iteration Dictionary
  2246. @subsection loop-finish [Local Macro]
  2247. @subsubheading Syntax::
  2248. @code{loop-finish} @i{<@i{no @i{arguments}}>} @result{} #<NoValue>
  2249. @subsubheading Description::
  2250. The @b{loop-finish} @i{macro} can be used lexically within
  2251. an extended
  2252. @b{loop} @i{form}
  2253. to terminate that @i{form} ``normally.''
  2254. That is, it transfers control to the loop epilogue
  2255. of the lexically innermost extended @b{loop} @i{form}.
  2256. This permits execution of any @b{finally} clause (for effect)
  2257. and
  2258. the return of
  2259. any accumulated result.
  2260. @subsubheading Examples::
  2261. @example
  2262. ;; Terminate the loop, but return the accumulated count.
  2263. (loop for i in '(1 2 3 stop-here 4 5 6)
  2264. when (symbolp i) do (loop-finish)
  2265. count i)
  2266. @result{} 3
  2267. ;; The preceding loop is equivalent to:
  2268. (loop for i in '(1 2 3 stop-here 4 5 6)
  2269. until (symbolp i)
  2270. count i)
  2271. @result{} 3
  2272. ;; While LOOP-FINISH can be used can be used in a variety of
  2273. ;; situations it is really most needed in a situation where a need
  2274. ;; to exit is detected at other than the loop's `top level'
  2275. ;; (where UNTIL or WHEN often work just as well), or where some
  2276. ;; computation must occur between the point where a need to exit is
  2277. ;; detected and the point where the exit actually occurs. For example:
  2278. (defun tokenize-sentence (string)
  2279. (macrolet ((add-word (wvar svar)
  2280. `(when ,wvar
  2281. (push (coerce (nreverse ,wvar) 'string) ,svar)
  2282. (setq ,wvar nil))))
  2283. (loop with word = '() and sentence = '() and endpos = nil
  2284. for i below (length string)
  2285. do (let ((char (aref string i)))
  2286. (case char
  2287. (#\Space (add-word word sentence))
  2288. (#\. (setq endpos (1+ i)) (loop-finish))
  2289. (otherwise (push char word))))
  2290. finally (add-word word sentence)
  2291. (return (values (nreverse sentence) endpos)))))
  2292. @result{} TOKENIZE-SENTENCE
  2293. (tokenize-sentence "this is a sentence. this is another sentence.")
  2294. @result{} ("this" "is" "a" "sentence"), 19
  2295. (tokenize-sentence "this is a sentence")
  2296. @result{} ("this" "is" "a" "sentence"), NIL
  2297. @end example
  2298. @subsubheading Side Effects::
  2299. Transfers control.
  2300. @subsubheading Exceptional Situations::
  2301. Whether or not @b{loop-finish} is @i{fbound} in the
  2302. @i{global environment} is @i{implementation-dependent};
  2303. however, the restrictions on redefinition and @i{shadowing} of
  2304. @b{loop-finish} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package}
  2305. which are @i{fbound} in the @i{global environment}.
  2306. The consequences of attempting to use @b{loop-finish} outside
  2307. of @b{loop} are undefined.
  2308. @subsubheading See Also::
  2309. @ref{loop}
  2310. ,
  2311. @ref{The LOOP Facility}
  2312. @subsubheading Notes::
  2313. @c end of including dict-iteration
  2314. @c %**end of chapter