concept-loop.tex 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  1. % -*- Mode: TeX -*-
  2. \beginsubsection{Overview of the Loop Facility}
  3. \Themacro{loop} performs iteration.
  4. \beginsubsubsection{Simple vs Extended Loop}
  5. \macref{loop} \term{forms} are partitioned into two categories:
  6. simple \macref{loop} \term{forms}
  7. and extended \macref{loop} \term{forms}.
  8. \beginsubsubsubsection{Simple Loop}
  9. \DefineSection{SimpleLoop}
  10. A simple \macref{loop} \term{form} is one that has a body containing
  11. only \term{compound forms}.
  12. %% 7.8.1 3
  13. Each \term{form} is \term{evaluated} in turn from left to right.
  14. When the last \param{form} has been \term{evaluated},
  15. then the first \param{form} is evaluated again, and so on, in a never-ending cycle.
  16. %% 7.8.1 4
  17. A simple \macref{loop} \term{form} establishes an \term{implicit block} named \nil.
  18. The execution of a simple \macref{loop} can be terminated by explicitly
  19. transfering control to the \term{implicit block} (using \macref{return} or
  20. \specref{return-from}) or to some \term{exit point} outside of the \term{block}
  21. (\eg using \specref{throw}, \specref{go}, or \specref{return-from}).
  22. %KMP: What about LOOP-FINISH here?
  23. % Test cases: (loop (loop-finish)) and (loop do (loop (loop-finish))).
  24. % Mail sent to X3J13 saying I was going to make this explicitly vague,
  25. % since some implementations
  26. % See the LOOP-FINISH dictionary entry.
  27. \endsubsubsubsection%{Simple Loop}
  28. %========================================
  29. \beginsubsubsubsection{Extended Loop}
  30. An extended \macref{loop} \term{form} is one that has a body containing
  31. \term{atomic} \term{expressions}. When \themacro{loop} processes such a
  32. \term{form}, it invokes a facility that is commonly called ``the Loop Facility.''
  33. The Loop Facility provides standardized access to mechanisms commonly used
  34. in iterations through Loop schemas, which are introduced by \term{loop keywords}.
  35. The body of an extended \macref{loop} \term{form} is divided into \macref{loop} clauses,
  36. each which is in turn made up of \term{loop keywords} and \term{forms}.
  37. \endsubsubsubsection%{Extended Loop}
  38. \endsubsubsection%{Simple vs Extended Loop}
  39. \beginsubsubsection{Loop Keywords}
  40. %2
  41. \term{Loop keywords} are not true \term{keywords}\meaning{1};
  42. they are special \term{symbols}, recognized by \term{name} rather than \term{object} identity,
  43. that are meaningful only to the \macref{loop} facility.
  44. A \term{loop keyword} is a \term{symbol} but is recognized by its \term{name}
  45. (not its identity), regardless of the \term{packages} in which it is \term{accessible}.
  46. \issue{JUN90-TRIVIAL-ISSUES:11}
  47. In general, \term{loop keywords} are not \term{external symbols} of \thepackage{common-lisp},
  48. except in the coincidental situation that a \term{symbol} with the same name as a
  49. \term{loop keyword} was needed for some other purpose in \clisp. For example,
  50. there is a \term{symbol} in \thepackage{common-lisp} whose \term{name} is \f{"UNLESS"} but
  51. not one whose \term{name} is \f{"UNTIL"}.
  52. \endissue{JUN90-TRIVIAL-ISSUES:11}
  53. If no \term{loop keywords} are supplied in a \macref{loop} \term{form},
  54. the Loop Facility executes the loop body repeatedly; \seesection\SimpleLoop.
  55. \endsubsubsection%{Loop Keywords}
  56. \beginsubsubsection{Parsing Loop Clauses}
  57. %7
  58. The syntactic parts of an extended \macref{loop} \term{form} are called clauses;
  59. %the scope \reviewer{Barmar: what meaning of scope?}%!!!
  60. %of each clause
  61. the rules for parsing are determined by
  62. %the parsing of
  63. that clause's keyword.
  64. The following example shows a \macref{loop} \term{form} with six clauses:
  65. \code
  66. (loop for i from 1 to (compute-top-value) ; first clause
  67. while (not (unacceptable i)) ; second clause
  68. collect (square i) ; third clause
  69. do (format t "Working on ~D now" i) ; fourth clause
  70. when (evenp i) ; fifth clause
  71. do (format t "~D is a non-odd number" i)
  72. finally (format t "About to exit!")) ; sixth clause
  73. \endcode
  74. %8
  75. Each \term{loop keyword} introduces
  76. either a compound loop clause or a simple loop clause
  77. that can consist of a \term{loop keyword} followed by a single \term{form}.
  78. The number of \term{forms} in a clause is determined by the \term{loop keyword}
  79. that begins the clause and by the auxiliary keywords in the clause.
  80. The keywords \loopref{do},
  81. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  82. \loopref{doing},
  83. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  84. \loopref{initially}, and \loopref{finally}
  85. are the only loop keywords that can take any number of \term{forms} and
  86. group them as an \term{implicit progn}.
  87. %9
  88. Loop clauses can contain auxiliary keywords, which are sometimes
  89. called prepositions. For example, the first clause in the code
  90. above includes the prepositions \loopref{from} and \loopref{to},
  91. which mark the value from which stepping begins and the value at which stepping
  92. ends.
  93. For detailed information about \macref{loop} syntax,
  94. \seemac{loop}.
  95. \endsubsubsection%{Parsing Loop Clauses}
  96. \beginsubsubsection{Expanding Loop Forms}
  97. %!!! Barmar: This seems to legislate the implementation. We should define the meaning
  98. % and let the implementors worry about how to code it.
  99. %3
  100. A \macref{loop} \term{macro form} expands into a \term{form} containing
  101. %one or more \term{lambda expressions} for the local \term{binding} of loop variables
  102. one or more binding forms (that \term{establish} \term{bindings} of loop variables)
  103. and a \specref{block} and a \specref{tagbody} (that express a looping control
  104. structure). The variables established in \macref{loop} are bound as
  105. if by \specref{let} or \misc{lambda}.
  106. Implementations can interleave the setting of initial values with the \term{bindings}.
  107. However, the assignment of the initial values is always calculated in the order
  108. specified by the user. A variable is thus sometimes bound to a meaningless value
  109. of the correct \term{type}, and then later in the prologue it is set to the true
  110. initial value by using \specref{setq}.
  111. \issue{LOOP-INITFORM-ENVIRONMENT:PARTIAL-INTERLEAVING-VAGUE}
  112. One implication of this interleaving is that it is \term{implementation-dependent}
  113. whether the \term{lexical environment} in which the initial value \term{forms}
  114. (variously called the \param{form1}, \param{form2}, \param{form3}, \param{step-fun},
  115. \param{vector}, \param{hash-table}, and \param{package}) in any \param{for-as-subclause},
  116. except \param{for-as-equals-then},
  117. are \term{evaluated} includes only the loop variables preceding that \term{form}
  118. or includes more or all of the loop variables;
  119. the \param{form1} and \param{form2} in a \param{for-as-equals-then} form
  120. includes the \term{lexical environment} of all the loop variables.
  121. \endissue{LOOP-INITFORM-ENVIRONMENT:PARTIAL-INTERLEAVING-VAGUE}
  122. %4
  123. After the \term{form} is expanded, it consists of three basic parts in the
  124. \specref{tagbody}:
  125. the loop prologue,
  126. the loop body,
  127. and the loop epilogue.
  128. \beginlist
  129. \itemitem{\b{Loop prologue}}
  130. The loop prologue contains \term{forms}
  131. that are executed before iteration begins, such as
  132. any automatic variable initializations prescribed
  133. by the \param{variable} clauses, along with any \loopref{initially} clauses
  134. in the order they appear in the source.
  135. \itemitem{\b{Loop body}}
  136. The loop body contains those \term{forms} that are executed during iteration,
  137. including application-specific calculations, termination tests,
  138. and variable \term{stepping}\meaning{1}.
  139. \itemitem{\b{Loop epilogue}}
  140. The loop epilogue contains \term{forms} that are executed after iteration
  141. terminates, such as \loopref{finally} clauses, if any, along
  142. with any implicit return value from an \param{accumulation} clause or
  143. an \param{termination-test} clause.
  144. \endlist
  145. %5
  146. Some clauses from the source \term{form}
  147. contribute code only to the loop prologue; these clauses must
  148. come before other clauses that are in the main body of the \macref{loop} form.
  149. Others contribute code only to the loop epilogue.
  150. All other clauses contribute to the final
  151. translated \term{form} in the same
  152. order given in the original source \term{form} of the \macref{loop}.
  153. %6
  154. Expansion of the \macref{loop} macro produces an \term{implicit block} named \nil\
  155. \issue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  156. unless \loopref{named} is supplied.
  157. \endissue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  158. Thus, \specref{return-from} (and sometimes \macref{return})
  159. can be used to return values from \macref{loop} or to exit \macref{loop}.
  160. %%Barmar: Has nothing to do with LOOP specifically.
  161. % Within the executable parts of loop clauses and around the entire
  162. % \macref{loop} form, variables can be bound by using \specref{let}.
  163. \endsubsubsection%{Expanding Loop Forms}
  164. \beginsubsubsection{Summary of Loop Clauses}
  165. %11
  166. Loop clauses fall into one of the following categories:
  167. \beginsubsubsubsection{Summary of Variable Initialization and Stepping Clauses}
  168. The \loopref{for} and \loopref{as} constructs provide iteration control clauses
  169. that establish a variable to be initialized.
  170. \loopref{for} and \loopref{as} clauses can be combined with the loop
  171. keyword \loopref{and} to get \term{parallel} initialization and \term{stepping}\meaning{1}.
  172. Otherwise, the initialization and \term{stepping}\meaning{1} are \term{sequential}.
  173. %\issue{LOOP-AND-DISCREPANCY:NO-REITERATION}
  174. % When two or more such clauses are joined with \loopref{and},
  175. % clauses after the first do not have \loopref{for} or \loopref{as} before them.
  176. %\endissue{LOOP-AND-DISCREPANCY:NO-REITERATION}
  177. The \loopref{with} construct is similar to a single \specref{let} clause.
  178. \loopref{with} clauses can be combined using the \term{loop keyword} \loopref{and}
  179. to get \term{parallel} initialization.
  180. %% Removed per Barmar. See Termination Clauses.
  181. % The \loopref{repeat} construct causes iteration to terminate after
  182. % a specified number of times. It uses an internal variable
  183. % to keep track of the number of iterations.
  184. For more information, \seesection\LOOPVarInitAndStep.
  185. \endsubsubsubsection%{Summary of Variable Initialization and Stepping Clauses}
  186. \beginsubsubsubsection{Summary of Value Accumulation Clauses}
  187. The \loopref{collect} (or \loopref{collecting}) construct
  188. takes one \term{form} in its clause
  189. and adds the value of that \term{form} to the end of a \term{list}
  190. of values. By default, the \term{list} of values is returned
  191. when the \macref{loop} finishes.
  192. The \loopref{append} (or \loopref{appending}) construct
  193. takes one \term{form} in its clause
  194. and appends the value of that \term{form} to the end of a \term{list}
  195. of values. By default, the \term{list} of values is returned when the
  196. \macref{loop} finishes.
  197. The \loopref{nconc} (or \loopref{nconcing}) construct
  198. is similar to the \loopref{append} construct,
  199. but its \term{list} values are concatenated as if by the function
  200. \loopref{nconc}. By default, the \term{list} of values is returned when
  201. the \macref{loop} finishes.
  202. The \loopref{sum} (or \loopref{summing}) construct
  203. takes one \term{form} in its clause
  204. that must evaluate to a \term{number} and accumulates the sum of all these
  205. \term{numbers}. By default, the cumulative sum is returned when the
  206. \macref{loop} finishes.
  207. The \loopref{count} (or \loopref{counting}) construct
  208. takes one \term{form} in its clause
  209. and counts the number of times that the \term{form} evaluates to \term{true}.
  210. By default, the count is returned when the \macref{loop} finishes.
  211. The \loopref{minimize} (or \loopref{minimizing}) construct
  212. takes one \term{form} in its clause
  213. and determines the minimum value obtained by evaluating that \term{form}.
  214. By default, the minimum value is returned when the \macref{loop} finishes.
  215. The \loopref{maximize} (or \loopref{maximizing}) construct
  216. takes one \term{form} in its clause
  217. and determines the maximum value obtained by evaluating that \term{form}.
  218. By default, the maximum value is returned when the \macref{loop} finishes.
  219. For more information, \seesection\LOOPValAcc.
  220. \endsubsubsubsection%{Summary of Value Accumulation Clauses}
  221. \beginsubsubsubsection{Summary of Termination Test Clauses}
  222. The \loopref{for} and \loopref{as} constructs provide a termination test
  223. that is determined by the iteration control clause.
  224. The \loopref{repeat} construct causes termination after a specified
  225. number of iterations.
  226. %Moved from text removed by Barmar above, so it doesn't get lost. -kmp 30-Jul-91
  227. (It uses an internal variable to keep track of the number of iterations.)
  228. The \loopref{while} construct takes one \term{form}, a \param{test},
  229. and terminates the iteration if the \param{test} evaluates to \term{false}.
  230. %!!! Barmar thinks this is not necessary:
  231. A \loopref{while} clause is equivalent to the expression
  232. \f{(if (not \param{test}) (loop-finish))}.
  233. The \loopref{until} construct is the inverse of \loopref{while};
  234. it terminates the iteration if the \param{test} evaluates to
  235. any \term{non-nil} value.
  236. %!!! Barmar thinks this is not necessary:
  237. An \loopref{until} clause is equivalent to the expression
  238. \hbox{\f{(if \param{test} (loop-finish))}}.
  239. The \loopref{always} construct takes one \term{form} and
  240. terminates the \macref{loop} if the \term{form} ever evaluates to \term{false};
  241. in this case, the \macref{loop} \term{form} returns \nil.
  242. Otherwise, it provides a default return value of \t.
  243. The \loopref{never} construct takes one \term{form} and
  244. terminates the \macref{loop} if the \term{form} ever evaluates to \term{true};
  245. in this case, the \macref{loop} \term{form} returns \nil.
  246. Otherwise, it provides a default return value of \t.
  247. The \loopref{thereis} construct takes one \term{form} and
  248. terminates the \macref{loop} if the \term{form} ever evaluates to
  249. a \term{non-nil} \term{object};
  250. in this case, the \macref{loop} \term{form} returns that \term{object}.
  251. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  252. Otherwise, it provides a default return value of \nil.
  253. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  254. %Added per Barmar:
  255. If multiple termination test clauses are specified,
  256. the \macref{loop} \term{form} terminates if any are satisfied.
  257. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  258. % Note also that the \macref{loop-finish} macro terminates iteration and returns any
  259. % accumulated result. Any \loopref{finally} clauses that are supplied are evaluated.
  260. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  261. For more information, \seesection\LOOPTermTest.
  262. \endsubsubsubsection%{Summary of Termination Test Clauses}
  263. \beginsubsubsubsection{Summary of Unconditional Execution Clauses}
  264. The \loopref{do} (or \loopref{doing}) construct evaluates all \term{forms} in its clause.
  265. The \loopref{return} construct takes one
  266. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  267. % \term{form} and returns its value.
  268. \term{form}. Any \term{values} returned by the \term{form} are
  269. immediately returned by the \macref{loop} form.
  270. % It is equivalent to the clause \f{do (return \i{value})}.
  271. It is equivalent to the clause
  272. \f{do (return-from \i{block-name} \i{value})},
  273. where \i{block-name} is the name specified in a \loopref{named}
  274. clause, or \nil\ if there is no \loopref{named} clause.
  275. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  276. For more information, \seesection\LOOPUnconditional.
  277. \endsubsubsubsection%{Summary of Unconditional Execution Clauses}
  278. \beginsubsubsubsection{Summary of Conditional Execution Clauses}
  279. The \loopref{if} and \loopref{when} constructs take one \term{form} as a test
  280. and a clause that is executed when the test \term{yields} \term{true}.
  281. The clause can be a value accumulation, unconditional, or
  282. another conditional clause; it can also be any combination
  283. of such clauses connected by \theloopkeyword{and}.
  284. \Theloopconstruct{unless} is similar to \theloopconstruct{when}
  285. except that it complements the test result.
  286. \Theloopconstruct{else} provides an optional component of \loopref{if},
  287. \loopref{when}, and \loopref{unless} clauses that is executed
  288. when an \loopref{if} or \loopref{when} test \term{yields} \term{false}
  289. or when an \loopref{unless} test \term{yields} \term{true}.
  290. The component is one of the clauses described under \loopref{if}.
  291. \Theloopconstruct{end} provides an optional component to mark the
  292. end of a conditional clause.
  293. For more information, \seesection\LOOPConditional.
  294. \endsubsubsubsection%{Summary of Conditional Execution Clauses}
  295. \beginsubsubsubsection{Summary of Miscellaneous Clauses}
  296. \Theloopconstruct{named} gives a name for the \term{block} of the loop.
  297. \Theloopconstruct{initially} causes its \term{forms} to be
  298. evaluated in the loop prologue, which precedes all \macref{loop} code
  299. except for initial settings supplied by the constructs \loopref{with},
  300. \loopref{for}, or \loopref{as}.
  301. \Theloopconstruct{finally} causes its \term{forms} to
  302. be evaluated in the loop epilogue after normal iteration terminates.
  303. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  304. % An unconditional clause can also follow \theloopkeyword{finally}.
  305. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  306. For more information, \seesection\LOOPMisc.
  307. \endsubsubsubsection%{Summary of Miscellaneous Clauses}
  308. \endsubsubsection%{Summary of Loop Clauses}
  309. \beginsubsubsection{Order of Execution}\idxtext{order of evaluation}\idxtext{evaluation order}
  310. %10
  311. With the exceptions listed below, clauses are executed in the loop body
  312. in the order in which they appear in the source. Execution is repeated
  313. until a clause
  314. terminates the \macref{loop} or until a \macref{return}, \specref{go},
  315. or \specref{throw} form is encountered
  316. %I added this next. -kmp 10-Feb-92
  317. which transfers control to a point outside of the loop.
  318. The following actions are
  319. exceptions to the linear order of execution:
  320. \beginlist
  321. \itemitem{\bull} All variables are initialized first,
  322. regardless of where the establishing clauses appear in the
  323. source. The order of initialization follows the order of these clauses.
  324. \itemitem{\bull} The code for any \loopref{initially} clauses is collected
  325. into one \specref{progn} in the order in which the clauses appear in
  326. the source. The collected code is executed once in the loop prologue
  327. after any implicit variable initializations.
  328. \itemitem{\bull} The code for any \loopref{finally} clauses is collected
  329. into one \specref{progn} in the order in which the clauses appear in
  330. the source. The collected code is executed once in the loop epilogue
  331. before any implicit values from the accumulation clauses are returned.
  332. Explicit returns anywhere in the source, however, will exit the
  333. \macref{loop} without executing the epilogue code.
  334. \itemitem{\bull} A \loopref{with} clause introduces a variable \term{binding}
  335. and an optional initial value. The initial values are calculated
  336. in the order in which the \loopref{with} clauses occur.
  337. \itemitem{\bull}
  338. Iteration control clauses implicitly perform the following actions:
  339. \beginlist
  340. \itemitem{--} initialize variables;
  341. \itemitem{--} \term{step} variables, generally
  342. between each execution of the loop body;
  343. \itemitem{--} perform termination tests,
  344. generally just before the execution of the
  345. loop body.
  346. \endlist
  347. \endlist
  348. \endsubsubsection%{Order of Execution}
  349. \beginsubsubsection{Destructuring}
  350. \DefineSection{DestructuringLOOPVars}
  351. The \param{d-type-spec} argument is used for destructuring.
  352. If the
  353. \param{d-type-spec} argument consists solely of \thetype{fixnum},
  354. \typeref{float}, \typeref{t}, or \nil, the \loopref{of-type} keyword is optional.
  355. The \loopref{of-type} construct is optional in these cases to provide backwards
  356. compatibility; thus, the following two expressions are the same:
  357. %!!! Barmar: Examples belong in the examples section
  358. \code
  359. ;;; This expression uses the old syntax for type specifiers.
  360. (loop for i fixnum upfrom 3 ...)
  361. ;;; This expression uses the new syntax for type specifiers.
  362. (loop for i of-type fixnum upfrom 3 ...)
  363. ;; Declare X and Y to be of type VECTOR and FIXNUM respectively.
  364. (loop for (x y) of-type (vector fixnum)
  365. in l do ...)
  366. \endcode
  367. A \term{type specifier} for a destructuring pattern is a \term{tree} of
  368. \term{type specifiers} with the same shape as the \term{tree} of
  369. \term{variable} \term{names}, with the following exceptions:
  370. \beginlist
  371. \itemitem{\bull}
  372. When aligning the \term{trees}, an \term{atom} in the
  373. \term{tree} of \term{type specifiers} that matches a \term{cons}
  374. in the variable tree declares the same \term{type} for each variable
  375. in the subtree rooted at the \term{cons}.
  376. \itemitem{\bull}
  377. A \term{cons} in the \term{tree} of \term{type specifiers} that
  378. matches an \term{atom} in the \term{tree} of \term{variable} \term{names}
  379. is a \term{compound type specifer}.
  380. \endlist
  381. Destructuring allows \term{binding} of a set of variables to a corresponding
  382. set of values anywhere that a value can normally be bound to a single
  383. variable. During \macref{loop} expansion,
  384. each variable in the variable list
  385. is matched with the values in the values list. If there are more variables
  386. in the variable list than there are values in the values list, the
  387. remaining variables are given a value of \nil. If there are more
  388. values than variables listed, the extra values are discarded.
  389. To assign values from a list to the variables \f{a},
  390. \f{b}, and \f{c}, the \loopref{for} clause could be used to
  391. bind the variable \f{numlist} to the
  392. \term{car} of the supplied \param{form},
  393. and then another \loopref{for} clause could be used to bind the variables
  394. \f{a}, \f{b}, and \f{c} \term{sequentially}.
  395. \issue{LOOP-AND-DISCREPANCY:NO-REITERATION}
  396. \code
  397. ;; Collect values by using FOR constructs.
  398. (loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  399. for a of-type integer = (first numlist)
  400. and b of-type integer = (second numlist)
  401. and c of-type float = (third numlist)
  402. collect (list c b a))
  403. \EV ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  404. \endcode
  405. \endissue{LOOP-AND-DISCREPANCY:NO-REITERATION}
  406. Destructuring makes this process easier by allowing the variables to
  407. be bound in each loop iteration.
  408. \term{Types} can be declared by using a
  409. list of \param{type-spec} arguments. If
  410. all the \term{types}
  411. are the same, a shorthand destructuring syntax can be used, as the second
  412. example illustrates.
  413. \code
  414. ;; Destructuring simplifies the process.
  415. (loop for (a b c) of-type (integer integer float) in
  416. '((1 2 4.0) (5 6 8.3) (8 9 10.4))
  417. collect (list c b a))
  418. \EV ((4.0 2 1) (8.3 6 5) (10.4 9 8))
  419. ;; If all the types are the same, this way is even simpler.
  420. (loop for (a b c) of-type float in
  421. '((1.0 2.0 4.0) (5.0 6.0 8.3) (8.0 9.0 10.4))
  422. collect (list c b a))
  423. \EV ((4.0 2.0 1.0) (8.3 6.0 5.0) (10.4 9.0 8.0))
  424. \endcode
  425. If destructuring is used to declare or initialize a number of groups
  426. of variables into \term{types}, the \term{loop keyword} \loopref{and} can be used
  427. to simplify the process further.
  428. \issue{LOOP-AND-DISCREPANCY:NO-REITERATION}
  429. \code
  430. ;; Initialize and declare variables in parallel by using the AND construct.\kern-7pt
  431. (loop with (a b) of-type float = '(1.0 2.0)
  432. and (c d) of-type integer = '(3 4)
  433. and (e f)
  434. return (list a b c d e f))
  435. \EV (1.0 2.0 3 4 NIL NIL)
  436. \endcode
  437. \endissue{LOOP-AND-DISCREPANCY:NO-REITERATION}
  438. If \nil\ is used in a destructuring list, no variable is provided for
  439. its place.
  440. \code
  441. (loop for (a nil b) = '(1 2 3)
  442. do (return (list a b)))
  443. \EV (1 3)
  444. \endcode
  445. Note that
  446. %% Replaced per Moon #42 (first public review) -kmp 6-May-93
  447. %nonstandard lists
  448. \term{dotted lists}
  449. can specify destructuring.
  450. \code
  451. (loop for (x . y) = '(1 . 2)
  452. do (return y))
  453. \EV 2
  454. (loop for ((a . b) (c . d)) of-type ((float . float) (integer . integer)) in
  455. '(((1.2 . 2.4) (3 . 4)) ((3.4 . 4.6) (5 . 6)))
  456. collect (list a b c d))
  457. \EV ((1.2 2.4 3 4) (3.4 4.6 5 6))
  458. \endcode
  459. An error \oftype{program-error} is signaled (at macro expansion time)
  460. if the same variable is bound twice in any variable-binding
  461. clause of a single \macref{loop} expression. Such variables include
  462. local variables, iteration control variables, and variables found by
  463. destructuring.
  464. \endsubsubsection%{Destructuring}
  465. \beginsubsubsection{Restrictions on Side-Effects}
  466. \issue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  467. \Seesection\TraversalRules.
  468. \endissue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  469. \endsubsubsection%{Restrictions on Side-Effects}
  470. \endsubsection%{Overview of the Loop Facility}
  471. \beginsubsection{Variable Initialization and Stepping Clauses}
  472. \DefineSection{LOOPVarInitAndStep}
  473. \beginsubsubsection{Iteration Control}
  474. Iteration control clauses allow direction of \macref{loop} iteration.
  475. The \term{loop keywords} \loopref{for} and \loopref{as}
  476. %, and \loopref{repeat}
  477. designate iteration control clauses.
  478. Iteration control clauses differ with respect to the specification of
  479. termination tests and to the initialization and \term{stepping}\meaning{1}
  480. of loop variables. Iteration clauses by themselves
  481. do not cause the Loop Facility to return values, but they
  482. can be used in conjunction with value-accumulation clauses to
  483. return values.
  484. All variables are initialized in the loop prologue.
  485. A \term{variable} \term{binding} has \term{lexical scope}
  486. unless it is proclaimed \declref{special};
  487. thus, by default, the variable can be \term{accessed} only by \term{forms}
  488. that lie textually within the \macref{loop}.
  489. Stepping assignments are made in the loop body before any other \term{forms}
  490. are evaluated in the body.
  491. The variable argument in iteration control clauses can be a
  492. destructuring list. A destructuring list
  493. is a \term{tree} whose \term{non-nil} \term{atoms} are \term{variable} \term{names}.
  494. \Seesection\DestructuringLOOPVars.
  495. The iteration control clauses \loopref{for}, \loopref{as}, and \loopref{repeat}
  496. must precede any other loop clauses, except
  497. \loopref{initially}, \loopref{with}, and \loopref{named},
  498. since they establish variable \term{bindings}.
  499. When iteration control clauses are
  500. used in a \macref{loop},
  501. %Next line added for JonL:
  502. the corresponding
  503. termination tests in the loop body are evaluated
  504. before any other loop body code is executed.
  505. If multiple iteration clauses are used to control iteration, variable
  506. initialization and \term{stepping}\meaning{1} occur \term{sequentially} by default.
  507. The \loopref{and} construct can be used to connect two or more
  508. iteration clauses when \term{sequential} \term{binding} and
  509. \term{stepping}\meaning{1} are not necessary.
  510. The iteration behavior of clauses joined by \loopref{and}
  511. is analogous to the behavior of the macro \macref{do} with
  512. respect to \macref{do*}.
  513. The \loopref{for} and \loopref{as} clauses iterate by using one or more local
  514. loop variables that are initialized to some value and that
  515. can be modified or \term{stepped}\meaning{1} after each iteration.
  516. For these clauses, iteration terminates when a local
  517. variable reaches some supplied value or when some other loop clause
  518. terminates iteration.
  519. %!!! Barmar: These aren't the only ways for/as can step the variables.
  520. At each iteration, variables can be
  521. \term{stepped}\meaning{1} by an increment or a decrement
  522. or can be assigned a new value by the evaluation of a \term{form}).
  523. Destructuring can be used to assign
  524. %% Removed per barmar--It is also used during stepping.
  525. %initial
  526. values to variables during iteration.
  527. The \loopref{for} and \loopref{as} keywords are synonyms; they can be used
  528. interchangeably. There are seven syntactic formats for these constructs.
  529. In each syntactic format, the \term{type} of
  530. \param{var} can be supplied by the optional \param{type-spec}
  531. argument. If \param{var} is a destructuring list, the \term{type}
  532. supplied by the \param{type-spec} argument must appropriately match
  533. the elements of the list.
  534. %!!! Barmar: "conventions" belong in the "Notes" section.
  535. By convention, \loopref{for} introduces new iterations and \loopref{as}
  536. introduces iterations that depend on a previous iteration specification.
  537. \beginsubsubsubsection{The for-as-arithmetic subclause}
  538. In the \i{for-as-arithmetic} subclause, the \loopref{for}
  539. or \loopref{as} construct iterates from the value supplied by
  540. \param{form1} to the value supplied by \param{form2} in increments or
  541. decrements denoted by \param{form3}. Each
  542. expression is evaluated only once and must evaluate to a \term{number}.
  543. The variable \param{var} is bound to the value of
  544. \param{form1} in the first iteration and is \term{stepped}\meaning{1}
  545. by the value of \param{form3} in each succeeding iteration,
  546. or by 1 if \param{form3} is not provided.
  547. The following \term{loop keywords} serve as valid prepositions within this
  548. syntax.
  549. At least one of the
  550. %three classes of
  551. prepositions must be used;
  552. and at most one from each line may be used in a single subclause.
  553. \beginlist
  554. \itemitem{\tt from | downfrom | upfrom}
  555. \itemitem{\tt to | downto | upto | below | above}
  556. \itemitem{\tt by}
  557. \endlist
  558. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  559. The prepositional phrases in each subclause may appear in any order.
  560. For example, either ``\f{from x by y}'' or ``\f{by y from x}'' is permitted.
  561. However, because left-to-right order of evaluation is preserved,
  562. the effects will be different in the case of side effects.
  563. \idxtext{order of evaluation}\idxtext{evaluation order}%
  564. Consider:
  565. \code
  566. (let ((x 1)) (loop for i from x by (incf x) to 10 collect i))
  567. \EV (1 3 5 7 9)
  568. (let ((x 1)) (loop for i by (incf x) from x to 10 collect i))
  569. \EV (2 4 6 8 10)
  570. \endcode
  571. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  572. The descriptions of the prepositions follow:
  573. \beginlist
  574. \itemitem{\tt from}
  575. The \term{loop keyword} \loopref{from} specifies the value from which
  576. \term{stepping}\meaning{1} begins, as supplied by \param{form1}.
  577. \term{Stepping}\meaning{1} is incremental by default. If
  578. decremental \term{stepping}\meaning{1} is desired,
  579. the preposition \loopref{downto}
  580. or \loopref{above} must be used with \param{form2}. For incremental
  581. \term{stepping}\meaning{1}, the default \loopref{from} value is 0.
  582. \itemitem{\tt downfrom, upfrom}
  583. The \term{loop keyword} \loopref{downfrom}
  584. indicates that the variable \param{var} is decreased in decrements
  585. supplied by \param{form3}; the \term{loop keyword} \loopref{upfrom} indicates that
  586. \param{var} is increased in increments supplied by \param{form3}.
  587. \itemitem{\tt to}
  588. The \term{loop keyword} \loopref{to} marks the end value
  589. for \term{stepping}\meaning{1} supplied in \param{form2}.
  590. \term{Stepping}\meaning{1} is incremental by default.
  591. If decremental \term{stepping}\meaning{1} is desired,
  592. the preposition \loopref{downfrom} must be used with \param{form1},
  593. or else the preposition \loopref{downto} or \loopref{above} should be used instead
  594. of \loopref{to} with \param{form2}.
  595. \itemitem{\tt downto, upto}
  596. The \term{loop keyword} \loopref{downto} specifies decremental \term{stepping};
  597. the \term{loop keyword} \loopref{upto} specifies incremental \term{stepping}.
  598. In both cases, the amount of change on each step is specified by \param{form3},
  599. and the \macref{loop} terminates when the variable \param{var} passes
  600. the value of \param{form2}.
  601. Since there is no default for \param{form1} in decremental \term{stepping}\meaning{1},
  602. a \param{form1} value must be supplied (using \loopref{from} or \loopref{downfrom})
  603. when \loopref{downto} is supplied.
  604. \itemitem{\tt below, above}
  605. The \term{loop keywords} \loopref{below} and \loopref{above} are analogous to
  606. \loopref{upto} and \loopref{downto} respectively. These keywords stop
  607. iteration just before the value of the variable \param{var} reaches the value
  608. supplied by \param{form2}; the end value of \param{form2} is not included.
  609. Since there is no default for \param{form1} in decremental \term{stepping}\meaning{1},
  610. a \param{form1} value must be supplied (using \loopref{from} or \loopref{downfrom})
  611. when \loopref{above} is supplied.
  612. \itemitem{\tt by}
  613. The \term{loop keyword} \loopref{by} marks the increment or decrement supplied by
  614. \param{form3}. The value of \param{form3} can be any
  615. %!!! Jonl wants to know why "positive" here.
  616. positive
  617. \term{number}.
  618. The default value is 1.
  619. \endlist
  620. In an iteration control clause, the \loopref{for} or \loopref{as} construct
  621. causes termination when the supplied limit is reached. That is,
  622. iteration continues until the value \param{var} is stepped to the
  623. exclusive or inclusive limit supplied by \param{form2}. The range is
  624. exclusive if \param{form3} increases or decreases \param{var}
  625. to the value of \param{form2} without reaching that value; the loop
  626. keywords \loopref{below} and \loopref{above} provide exclusive limits. An
  627. inclusive limit allows \param{var} to attain the value of
  628. \param{form2}; \loopref{to}, \loopref{downto}, and \loopref{upto} provide inclusive
  629. limits.
  630. %!!! JonL wonders if we maybe shouldn't define "incremental" and "decremental" here.
  631. \beginsubsubsubsubsection{Examples of for-as-arithmetic subclause}
  632. \code
  633. ;; Print some numbers.
  634. (loop for i from 1 to 3
  635. do (print i))
  636. \OUT 1
  637. \OUT 2
  638. \OUT 3
  639. \EV NIL
  640. ;; Print every third number.
  641. (loop for i from 10 downto 1 by 3
  642. do (print i))
  643. \OUT 10
  644. \OUT 7
  645. \OUT 4
  646. \OUT 1
  647. \EV NIL
  648. ;; Step incrementally from the default starting value.
  649. (loop for i below 3
  650. do (print i))
  651. \OUT 0
  652. \OUT 1
  653. \OUT 2
  654. \EV NIL
  655. \endcode
  656. \endsubsubsubsubsection%{Examples of for-as-arithmetic subclause}
  657. \endsubsubsubsection%{The for-as-arithmetic subclause}
  658. \beginsubsubsubsection{The for-as-in-list subclause}
  659. In the \i{for-as-in-list} subclause,
  660. the \loopref{for}
  661. or \loopref{as} construct iterates over the contents of a
  662. \term{list}. It checks for
  663. the end of the \term{list} as if by using \funref{endp}.
  664. The variable \param{var} is bound to the successive elements of
  665. the \term{list} in \param{form1} before each
  666. iteration. At the end of each iteration, the function \param{step-fun}
  667. is applied to the \term{list}; the default value for \param{step-fun} is
  668. \funref{cdr}.
  669. The \term{loop keywords} \loopref{in} and \loopref{by} serve as valid prepositions in
  670. this syntax.
  671. The \loopref{for} or \loopref{as} construct causes termination when the
  672. end of the \term{list} is reached.
  673. \beginsubsubsubsubsection{Examples of for-as-in-list subclause}
  674. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  675. %Added OF-TYPE in the third example. -kmp 29-Apr-93
  676. \code
  677. ;; Print every item in a list.
  678. (loop for item in '(1 2 3) do (print item))
  679. \OUT 1
  680. \OUT 2
  681. \OUT 3
  682. \EV NIL
  683. ;; Print every other item in a list.
  684. (loop for item in '(1 2 3 4 5) by #'cddr
  685. do (print item))
  686. \OUT 1
  687. \OUT 3
  688. \OUT 5
  689. \EV NIL
  690. ;; Destructure a list, and sum the x values using fixnum arithmetic.
  691. (loop for (item . x) of-type (t . fixnum) in '((A . 1) (B . 2) (C . 3))
  692. unless (eq item 'B) sum x)
  693. \EV 4
  694. \endcode
  695. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  696. \endsubsubsubsubsection%{Examples of for-as-in-list subclause}
  697. \endsubsubsubsection%{The for-as-in-list subclause}
  698. \beginsubsubsubsection{The for-as-on-list subclause}
  699. \issue{LOOP-FOR-AS-ON-TYPO:FIX-TYPO}
  700. In the \i{for-as-on-list} subclause, the \loopref{for} or \loopref{as}
  701. construct iterates over
  702. %the contents of
  703. a \term{list}. It checks for the
  704. end of the \term{list} as if by using \funref{atom}.
  705. \endissue{LOOP-FOR-AS-ON-TYPO:FIX-TYPO}
  706. The variable \param{var} is bound to the successive tails of the
  707. \term{list} in
  708. \param{form1}. At the end of each iteration, the function \param{step-fun}
  709. is applied to the \term{list}; the default value for \param{step-fun} is \funref{cdr}.
  710. The \term{loop keywords} \loopref{on} and \loopref{by} serve as valid
  711. prepositions in this syntax.
  712. The \loopref{for} or \loopref{as} construct causes termination when the
  713. end of the \term{list} is reached.
  714. \beginsubsubsubsubsection{Examples of for-as-on-list subclause}
  715. \code
  716. ;; Collect successive tails of a list.
  717. (loop for sublist on '(a b c d)
  718. collect sublist)
  719. \EV ((A B C D) (B C D) (C D) (D))
  720. ;; Print a list by using destructuring with the loop keyword ON.
  721. (loop for (item) on '(1 2 3)
  722. do (print item))
  723. \OUT 1
  724. \OUT 2
  725. \OUT 3
  726. \EV NIL
  727. \endcode
  728. \endsubsubsubsubsection%{Examples of for-as-on-list subclause}
  729. \endsubsubsubsection%{The for-as-on-list subclause}
  730. \beginsubsubsubsection{The for-as-equals-then subclause}
  731. In the \i{for-as-equals-then} subclause
  732. the \loopref{for}
  733. or \loopref{as} construct
  734. initializes the variable \param{var} by setting it to the
  735. result of evaluating \param{form1} on the first iteration, then setting
  736. it to the result of evaluating \param{form2} on the second and
  737. subsequent iterations. If \param{form2} is omitted, the construct
  738. uses \param{form1} on the second and
  739. subsequent iterations.
  740. %When \param{form2} is omitted, the expanded
  741. % code shows the following optimization:
  742. The \term{loop keywords} {$=$} and \loopref{then} serve as valid prepositions
  743. in this syntax.
  744. This construct does not provide any termination tests.
  745. \beginsubsubsubsubsection{Examples of for-as-equals-then subclause}
  746. %\code
  747. %;; The original code:
  748. % (prog (...)
  749. % (setq x (some-value))
  750. % tag (print x)
  751. % (setq x (some-value))
  752. % (go tag))
  753. %
  754. %;; The expanded code:
  755. % (prog (...)
  756. % tag (setq x (some-value))
  757. % (print x)
  758. % (go tag))
  759. %\endcode
  760. \code
  761. ;; Collect some numbers.
  762. (loop for item = 1 then (+ item 10)
  763. for iteration from 1 to 5
  764. collect item)
  765. \EV (1 11 21 31 41)
  766. \endcode
  767. \endsubsubsubsubsection%{Examples of for-as-equals-then subclause}
  768. \endsubsubsubsection%{The for-as-equals-then subclause}
  769. \beginsubsubsubsection{The for-as-across subclause}
  770. In the \i{for-as-across} subclause the \loopref{for}
  771. or \loopref{as} construct binds the variable \param{var} to the value of
  772. each element in the array \param{vector}.
  773. The \term{loop keyword} \loopref{across} marks the array \param{vector}; \loopref{across}
  774. is used as a preposition in this syntax.
  775. Iteration stops when there are no more elements in the supplied
  776. \term{array} that can be referenced.
  777. Some implementations might recognize a \specref{the} special form
  778. in the \param{vector} form to produce more efficient code.
  779. \beginsubsubsubsubsection{Examples of for-as-across subclause}
  780. \code
  781. (loop for char across (the simple-string (find-message channel))
  782. do (write-char char stream))
  783. \endcode
  784. \endsubsubsubsubsection%{Examples of for-as-across subclause}
  785. \endsubsubsubsection%{The for-as-across subclause}
  786. \beginsubsubsubsection{The for-as-hash subclause}
  787. In the \i{for-as-hash} subclause
  788. the \loopref{for}
  789. or \loopref{as} construct
  790. iterates over the elements, keys, and values of a \term{hash-table}.
  791. In this syntax, a compound preposition is used to designate access to a
  792. \term{hash table}.
  793. The variable \param{var} takes on the value of each hash key
  794. or hash value in the supplied \param{hash-table}.
  795. The following \term{loop keywords} serve as valid prepositions within this syntax:
  796. \beginlist
  797. \itemitem{\loopref{being}}
  798. The keyword \loopref{being} introduces either the Loop schema
  799. \loopref{hash-key} or \loopref{hash-value}.
  800. \itemitem{\loopref{each}, \loopref{the}}
  801. The \term{loop keyword} \loopref{each}
  802. follows the \term{loop keyword} \loopref{being} when \loopref{hash-key} or
  803. \loopref{hash-value} is used. The \term{loop keyword} {\tt the} is used with
  804. \loopref{hash-keys} and \loopref{hash-values} only for ease of reading.
  805. This agreement isn't required.
  806. \itemitem{\loopref{hash-key}, \loopref{hash-keys}}
  807. These \term{loop keywords} access each key entry of the \term{hash table}. If
  808. the name \loopref{hash-value} is supplied in a \loopref{using} construct with one
  809. of these Loop schemas, the iteration can optionally access the keyed
  810. value. The order in which the keys are accessed is undefined; empty
  811. slots in the \term{hash table} are ignored.
  812. \itemitem{\loopref{hash-value}, \loopref{hash-values}}
  813. These \term{loop keywords} access each value entry of a
  814. \term{hash table}. If
  815. the name \loopref{hash-key} is supplied in a \loopref{using} construct with one of
  816. these Loop schemas, the iteration can optionally access the key that
  817. corresponds to the value. The order in which the keys are accessed is
  818. undefined; empty slots in the \term{hash table} are ignored.
  819. \itemitem{\loopref{using}}
  820. The \term{loop keyword} \loopref{using} introduces
  821. the optional key or the keyed value to
  822. be accessed. It allows access to the hash key if iteration is over
  823. the hash values, and the hash value if
  824. iteration is over the hash keys.
  825. \itemitem{\loopref{in}, \loopref{of}}
  826. These loop prepositions introduce \param{hash-table}.
  827. \endlist
  828. %!!! Barmar: What does this mean?
  829. In effect
  830. \loopref{being}
  831. \curly{\loopref{each} | \loopref{the}}
  832. \curly{\loopref{hash-value} |
  833. \loopref{hash-values} |
  834. \loopref{hash-key} |
  835. \loopref{hash-keys}}
  836. \curly{\loopref{in} | \loopref{of}}
  837. % \code
  838. % being \lbracket\ each|the\rbracket \lbracket\ hash-value|hash-values|hash-key|hash-key\rbracket \lbracket\ in|of\rbracket
  839. % \endcode
  840. is a compound preposition.
  841. Iteration stops when there are no more hash keys or hash values to be
  842. referenced in the supplied \param{hash-table}.
  843. \endsubsubsubsection%{The for-as-hash subclause}
  844. \beginsubsubsubsection{The for-as-package subclause}
  845. In the \i{for-as-package} subclause
  846. the \loopref{for}
  847. or \loopref{as} construct
  848. iterates over the \term{symbols} in a \term{package}.
  849. In this syntax, a compound preposition is used to designate access to a
  850. \term{package}.
  851. The variable \param{var} takes on the value of each \term{symbol}
  852. in the supplied \term{package}.
  853. The following \term{loop keywords} serve as valid prepositions within this syntax:
  854. \beginlist
  855. \itemitem{\loopref{being}}
  856. The keyword \loopref{being} introduces either the Loop schema
  857. \loopref{symbol}, \loopref{present-symbol}, or \loopref{external-symbol}.
  858. \itemitem{\loopref{each}, \loopref{the}}
  859. The \term{loop keyword} \loopref{each}
  860. follows the \term{loop keyword} \loopref{being} when \loopref{symbol},
  861. \loopref{present-symbol}, or \loopref{external-symbol} is used.
  862. The \term{loop keyword} \loopref{the} is used with \loopref{symbols},
  863. \loopref{present-symbols}, and \loopref{external-symbols} only for ease of reading.
  864. This agreement isn't required.
  865. \itemitem{\loopref{present-symbol}, \loopref{present-symbols}}
  866. These Loop schemas iterate over the \term{symbols}
  867. \issue{LOOP-PRESENT-SYMBOLS-TYPO:FLUSH-WRONG-WORDS}
  868. that are \term{present} in a \term{package}.
  869. % but not \term{external symbols} of that \term{package}.
  870. \endissue{LOOP-PRESENT-SYMBOLS-TYPO:FLUSH-WRONG-WORDS}
  871. The \param{package} to be iterated over is supplied in the same way
  872. that \term{package} arguments to \funref{find-package} are supplied.
  873. If the \param{package} for the iteration is not supplied,
  874. the \term{current package} is used.
  875. If a \param{package} that does not exist is supplied,
  876. an error \oftype{package-error} is signaled.
  877. \itemitem{\loopref{symbol}, \loopref{symbols}}
  878. These Loop schemas iterate over \term{symbols} that are
  879. \term{accessible} in a given \param{package}.
  880. The \param{package} to be iterated over is supplied in the same way
  881. that \term{package} arguments to \funref{find-package} are supplied.
  882. If the \param{package} for the iteration is not supplied,
  883. the \term{current package} is used.
  884. If a \param{package} that does not exist is supplied,
  885. an error \oftype{package-error} is signaled.
  886. \itemitem{\loopref{external-symbol}, \loopref{external-symbols}}
  887. These Loop schemas iterate over the \term{external symbols} of a \param{package}.
  888. The \param{package} to be iterated over is supplied in the same way
  889. that \term{package} arguments to \funref{find-package} are supplied.
  890. If the \param{package} for the iteration is not supplied,
  891. the \term{current package} is used.
  892. If a \param{package} that does not exist is supplied,
  893. an error \oftype{package-error} is signaled.
  894. \itemitem{\loopref{in}, \loopref{of}}
  895. These loop prepositions introduce \param{package}.
  896. \endlist
  897. %!!! Barmar: What does this mean?
  898. In effect
  899. \loopref{being}
  900. \curly{\loopref{each} | \loopref{the}}
  901. \curly{\loopref{symbol} |
  902. \loopref{symbols} |
  903. \loopref{present-symbol} |
  904. \loopref{present-symbols} |
  905. \loopref{external-symbol} |
  906. \loopref{external-symbols}}
  907. \curly{\loopref{in} | \loopref{of}}
  908. % \code
  909. % being \lbracket\ each|the\rbracket \lbracket\ \lbracket\ \lbracket\
  910. % present|external\rbracket\ symbol\rbracket | \lbracket\
  911. % \lbracket\ present|external\rbracket symbols\rbracket\rbracket \lbracket\ in|of\rbracket
  912. % \endcode
  913. is a compound preposition.
  914. Iteration stops when there are no more \term{symbols} to be referenced
  915. in the supplied \param{package}.
  916. \beginsubsubsubsubsection{Examples of for-as-package subclause}
  917. \issue{LOOP-PRESENT-SYMBOLS-TYPO:FLUSH-WRONG-WORDS}
  918. \code
  919. (let ((*package* (make-package "TEST-PACKAGE-1")))
  920. ;; For effect, intern some symbols
  921. (read-from-string "(THIS IS A TEST)")
  922. (export (intern "THIS"))
  923. (loop for x being each present-symbol of *package*
  924. do (print x)))
  925. \OUT A
  926. \OUT TEST
  927. \OUT THIS
  928. \OUT IS
  929. \EV NIL
  930. \endcode
  931. \endissue{LOOP-PRESENT-SYMBOLS-TYPO:FLUSH-WRONG-WORDS}
  932. \endsubsubsubsubsection%{Examples of for-as-package subclause}
  933. \endsubsubsubsection%{The for-as-package subclause}
  934. \endsubsubsection%{Iteration Control}
  935. \beginsubsubsection{Local Variable Initializations}
  936. When a \macref{loop} \term{form} is executed, the local variables are bound and are
  937. initialized to some value. These local variables exist until \macref{loop}
  938. iteration terminates, at which point they cease to exist.
  939. Implicit variables are also established by iteration control clauses and the
  940. \loopref{into} preposition of accumulation clauses.
  941. The \loopref{with} construct initializes variables that are local to
  942. a loop. The variables are initialized one time only.
  943. If the optional \param{type-spec} argument is supplied for the variable
  944. \param{var}, but there is no related expression to be evaluated, \param{var}
  945. is initialized to an appropriate default value for its \term{type}.
  946. %!!! Barmar: How is this default specified for "wierd" types? e.g.,
  947. % CLOS classes, DEFSTRUCT types, (OR ...), (AND ...), (SATISFIES ...), package, etc.
  948. For example, for the types \typeref{t}, \typeref{number},
  949. and \typeref{float},
  950. the default values are \nil, \f{0}, and \f{0.0} respectively.
  951. The consequences are undefined if a
  952. \param{type-spec} argument is supplied for \param{var} if
  953. the related expression returns a value that is not of the supplied
  954. \term{type}.
  955. By default, the \loopref{with} construct initializes variables
  956. \term{sequentially}; that is, one variable is assigned a value before the
  957. next expression is evaluated. However, by using the \term{loop keyword} \loopref{and}
  958. to join several \loopref{with} clauses,
  959. initializations can be forced to occur in \term{parallel}; that
  960. is, all of the supplied
  961. \param{forms} are evaluated, and the results are bound to the respective
  962. variables simultaneously.
  963. %The optional \loopref{and} clause forces \term{parallel} rather than \term{sequential}
  964. %initializations.
  965. \term{Sequential} \term{binding} is used when it is desireable for the initialization of
  966. some variables to depend on the values of previously bound variables.
  967. For example, suppose the variables \f{a}, \f{b}, and \f{c} are to be bound in sequence:
  968. \code
  969. (loop with a = 1
  970. with b = (+ a 2)
  971. with c = (+ b 3)
  972. return (list a b c))
  973. \EV (1 3 6)
  974. \endcode
  975. The execution of the above \macref{loop} is equivalent to the execution of
  976. the following code:
  977. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  978. %BLOCK moved to outside.
  979. \code
  980. (block nil
  981. (let* ((a 1)
  982. (b (+ a 2))
  983. (c (+ b 3)))
  984. (tagbody
  985. (next-loop (return (list a b c))
  986. (go next-loop)
  987. end-loop))))
  988. \endcode
  989. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  990. If the values of previously bound variables are not needed
  991. for the initialization of other local variables, an
  992. \loopref{and} clause can be used to
  993. %force the bindings to occur in \term{parallel}:
  994. %% for JonL:
  995. specify that the bindings are to occur in \term{parallel}:
  996. \code
  997. (loop with a = 1
  998. and b = 2
  999. and c = 3
  1000. return (list a b c))
  1001. \EV (1 2 3)
  1002. \endcode
  1003. The execution of the above loop is equivalent to the execution of
  1004. the following code:
  1005. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1006. %BLOCK moved to outside.
  1007. \code
  1008. (block nil
  1009. (let ((a 1)
  1010. (b 2)
  1011. (c 3))
  1012. (tagbody
  1013. (next-loop (return (list a b c))
  1014. (go next-loop)
  1015. end-loop))))
  1016. \endcode
  1017. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1018. \beginsubsubsubsection{Examples of WITH clause}
  1019. \code
  1020. ;; These bindings occur in sequence.
  1021. (loop with a = 1
  1022. with b = (+ a 2)
  1023. with c = (+ b 3)
  1024. return (list a b c))
  1025. \EV (1 3 6)
  1026. ;; These bindings occur in parallel.
  1027. (setq a 5 b 10)
  1028. \EV 10
  1029. (loop with a = 1
  1030. and b = (+ a 2)
  1031. and c = (+ b 3)
  1032. return (list a b c))
  1033. \EV (1 7 13)
  1034. ;; This example shows a shorthand way to declare local variables
  1035. ;; that are of different types.
  1036. (loop with (a b c) of-type (float integer float)
  1037. return (format nil "~A ~A ~A" a b c))
  1038. \EV "0.0 0 0.0"
  1039. ;; This example shows a shorthand way to declare local variables
  1040. ;; that are the same type.
  1041. (loop with (a b c) of-type float
  1042. return (format nil "~A ~A ~A" a b c))
  1043. \EV "0.0 0.0 0.0"
  1044. \endcode
  1045. \endsubsubsubsection%{Examples of WITH clause}
  1046. \endsubsubsection%{Local Variable Initializations}
  1047. \endsubsection%{Variable Initialization and Stepping Clauses}
  1048. \beginsubsection{Value Accumulation Clauses}
  1049. \DefineSection{LOOPValAcc}
  1050. %!!! Moon (comment #40) thinks it would be nice to coalesce
  1051. % the discussion of "into" here.
  1052. The constructs \loopref{collect}, \loopref{collecting},
  1053. \loopref{append}, \loopref{appending},
  1054. \loopref{nconc}, \loopref{nconcing},
  1055. \loopref{count}, \loopref{counting},
  1056. \loopref{maximize}, \loopref{maximizing},
  1057. \loopref{minimize}, \loopref{minimizing},
  1058. \loopref{sum}, and \loopref{summing},
  1059. allow values to be accumulated in a \macref{loop}.
  1060. %Accumulating values during iteration and returning them from a loop
  1061. %is often useful. Some of these accumulations occur so
  1062. %frequently that special loop clauses have been developed to handle them.
  1063. The constructs \loopref{collect},
  1064. \loopref{collecting}, \loopref{append}, \loopref{appending},
  1065. \loopref{nconc}, and \loopref{nconcing},
  1066. designate clauses that
  1067. accumulate values in \term{lists} and return them.
  1068. The constructs \loopref{count}, \loopref{counting},
  1069. \loopref{maximize}, \loopref{maximizing}, \loopref{minimize}, \loopref{minimizing},
  1070. \loopref{sum}, and \loopref{summing} designate clauses that accumulate and
  1071. return numerical values.
  1072. During each iteration, the constructs
  1073. \loopref{collect} and \loopref{collecting}
  1074. collect the value of the supplied
  1075. \param{form} into a \term{list}.
  1076. When iteration terminates, the \term{list} is returned.
  1077. The argument \param{var} is
  1078. set to the \term{list}
  1079. of collected values; if \param{var} is supplied, the \macref{loop}
  1080. does not return the final \term{list} automatically. If
  1081. \param{var} is not
  1082. supplied, it is equivalent to supplying an internal name for
  1083. \param{var} and returning its value in a \loopref{finally} clause.
  1084. The \param{var} argument
  1085. is bound as if by the construct \loopref{with}.
  1086. %A \term{type} cannot be supplied for \param{var};
  1087. No mechanism is provided for declaring the \term{type} of \param{var};
  1088. it must be \oftype{list}.
  1089. %%Removed per Barmar. Fully redundant with next couple of paragraphs.
  1090. % The \loopref{append} construct is similar to \loopref{collect} except the
  1091. % values of the supplied \param{form} must be
  1092. % \term{lists}. These \term{lists} are not modified
  1093. % but are concatenated together into a single
  1094. % \term{list}, as if they were arguments
  1095. % to \funref{append}.
  1096. % The argument \param{var} is
  1097. % bound to the list of concatenated values; if \param{var} is supplied, the loop
  1098. % does not return the final
  1099. % \term{list} automatically. The \param{var} argument
  1100. % is bound as if by the construct \loopref{with}.
  1101. % A \term{type} cannot be supplied for \param{var}; it must be \oftype{list}.
  1102. The constructs \loopref{append}, \loopref{appending},
  1103. \loopref{nconc}, and \loopref{nconcing}
  1104. are similar to \loopref{collect} except that the
  1105. values of the supplied \param{form} must be \term{lists}.
  1106. \beginlist
  1107. \itemitem{\bull}
  1108. The \loopref{append} keyword causes its \term{list} values to be concatenated
  1109. into a single \term{list}, as if
  1110. they were arguments to \thefunction{append}.
  1111. \itemitem{\bull}
  1112. The \loopref{nconc} keyword causes its \term{list} values to be concatenated
  1113. into a single \term{list},
  1114. as if they were arguments to \thefunction{nconc}.
  1115. \endlist
  1116. The argument \param{var} is
  1117. set to the \term{list} of
  1118. concatenated values; if \param{var} is supplied,
  1119. \macref{loop}
  1120. does not return the final \term{list} automatically.
  1121. The \param{var} argument
  1122. is bound as if by the construct \loopref{with}.
  1123. A \term{type} cannot be supplied for \param{var};
  1124. it must be \oftype{list}.
  1125. The construct \loopref{nconc}
  1126. destructively modifies its argument \term{lists}.
  1127. % Barmar: Is it required to coerce the sum to the specified type?
  1128. % Test case: (loop for x in '(a b c) count x into z of-type float finally (return z))
  1129. % Mail sent to Quinquevirate (issue LOOP-DECLARATION-VS-COERCION)
  1130. The \loopref{count} construct counts the number of times
  1131. that the supplied \param{form} returns \term{true}.
  1132. The argument \param{var} accumulates the number of occurrences;
  1133. if \param{var} is supplied,
  1134. \macref{loop} does not return the final count automatically.
  1135. The \param{var} argument is bound as if by the construct \loopref{with}
  1136. %% Per Moon #40, First Public Review. -kmp 28-Jul-93
  1137. to a zero of the appropriate type.
  1138. %% Per X3J13. -kmp 05-Oct-93
  1139. Subsequent values (including any necessary coercions)
  1140. are computed as if by the function \funref{1+}.
  1141. If \loopref{into} \param{var} is used,
  1142. a \term{type} can be supplied for \param{var} with the \param{type-spec} argument;
  1143. the consequences are unspecified if a nonnumeric \term{type} is supplied.
  1144. If there is no \loopref{into} variable,
  1145. the optional \param{type-spec} argument
  1146. applies to the internal variable that is keeping the count.
  1147. The default \term{type} is \term{implementation-dependent};
  1148. but it must be
  1149. %% Per Moon #40, First Public Review -kmp 28-Jul-93
  1150. a \supertypeof{fixnum}.
  1151. %Barmar: For these next three, is type-spec a declaration, coercion, or both?
  1152. % Test cases: (loop for x in '(1 2 3) minimize x into z of-type float finally (return z))
  1153. % (loop for x in '(1 2 3) sum x into z of-type float finally (return z))
  1154. The \loopref{maximize} and
  1155. %% Per X3J13. -kmp 05-Oct-93
  1156. %\loopref{minimum}
  1157. \loopref{minimize}
  1158. constructs compare
  1159. the value of the supplied \param{form} obtained during the first
  1160. iteration with values obtained in successive iterations.
  1161. The maximum (for \loopref{maximize}) or minimum (for \loopref{minimize})
  1162. value encountered is determined
  1163. %% Per Moon #40, First Public Review -kmp 28-Jul-93
  1164. (as if by \thefunction{max} for \loopref{maximize} and
  1165. as if by \thefunction{min} for \loopref{minimize})
  1166. and returned.
  1167. %% Per Moon #40, First Public Review -kmp 28-Jul-93
  1168. If the \loopref{maximize} or \loopref{minimize} clause
  1169. is never executed, the accumulated value is unspecified.
  1170. The argument \param{var} accumulates the maximum or minimum value;
  1171. if \param{var} is supplied,
  1172. \macref{loop} does not return the maximum or minimum automatically.
  1173. The \param{var} argument is bound as if by the construct \loopref{with}.
  1174. If \loopref{into} \param{var} is used,
  1175. a \term{type} can be supplied for \param{var} with the \param{type-spec} argument;
  1176. the consequences are unspecified if a nonnumeric \term{type} is supplied.
  1177. If there is no \loopref{into} variable,
  1178. the optional \param{type-spec} argument applies to the internal variable
  1179. that is keeping the maximum or minimum value.
  1180. %% Per Moon #40, First Public Review -kmp 28-Jul-93
  1181. The default \term{type}
  1182. %% Per X3J13. -kmp 05-Oct-93
  1183. is \term{implementation-dependent}; but it
  1184. must be a \supertypeof{real}.
  1185. The \loopref{sum} construct forms a cumulative sum
  1186. of the successive \term{primary values} of the supplied \param{form}
  1187. at each iteration.
  1188. The argument \param{var} is used to accumulate the sum;
  1189. if \param{var} is supplied,
  1190. \macref{loop} does not return the final sum automatically.
  1191. The \param{var} argument is bound as if by the construct \loopref{with}
  1192. %% Per Moon #40, First Public Review -kmp 28-Jul-93
  1193. to a zero of the appropriate type.
  1194. Subsequent values (including any necessary coercions) are computed as if by \thefunction{+}.
  1195. If \loopref{into} \param{var} is used,
  1196. a \term{type} can be supplied for \param{var} with the \param{type-spec} argument;
  1197. the consequences are unspecified if a nonnumeric \term{type} is supplied.
  1198. If there is no \loopref{into} variable,
  1199. the optional \param{type-spec} argument applies to the internal variable
  1200. that is keeping the sum.
  1201. %% Per Moon #40, First Public Review -kmp 28-Jul-93
  1202. The default \term{type}
  1203. %% Per X3J13. -kmp 05-Oct-93
  1204. is \term{implementation-dependent}; but it
  1205. must be a \supertypeof{number}.
  1206. %% Removed per barmar. Redundant with the above.
  1207. % The loop preposition \loopref{into} can be used to name the
  1208. % variable used to hold partial accumulations.
  1209. % The variable is bound as if by the loop
  1210. % construct \loopref{with}.
  1211. If \loopref{into} is used,
  1212. the construct does not provide a default return value;
  1213. however, the variable is available
  1214. for use in any \loopref{finally} clause.
  1215. %These \term{loop keywords}
  1216. %can also be spelled with the optional suffix {\tt ing}.
  1217. %% Replaced per Moon #40, First Public Review -kmp 28-Jul-93
  1218. % Value-returning accumulation clauses can be combined in a \macref{loop} if
  1219. % all the clauses accumulate the same \term{type} of \term{object}.
  1220. % By default, the Loop Facility returns only one value;
  1221. % thus, the \term{objects} collected by multiple accumulation clauses
  1222. % as return values must have compatible \term{types}. For example, since both
  1223. % the \loopref{collect} and \loopref{append} constructs accumulate
  1224. % \term{objects} into a
  1225. % \term{list} that is returned from a
  1226. % \macref{loop}, they can be combined safely.
  1227. Certain kinds of accumulation clauses can be combined in a \macref{loop}
  1228. if their destination is the same
  1229. (the result of \macref{loop} or an \loopref{into} \param{var})
  1230. because they are considered to accumulate conceptually compatible quantities.
  1231. In particular,
  1232. any elements of following sets of accumulation clauses can be mixed
  1233. with other elements of the same set for the same destination
  1234. in a \macref{loop} \term{form}:
  1235. \beginlist
  1236. \itemitem{\bull} \loopref{collect}, \loopref{append}, \loopref{nconc}
  1237. \itemitem{\bull} \loopref{sum}, \loopref{count}
  1238. \itemitem{\bull} \loopref{maximize}, \loopref{minimize}
  1239. \endlist
  1240. \code
  1241. ;; Collect every name and the kids in one list by using
  1242. ;; COLLECT and APPEND.
  1243. (loop for name in '(fred sue alice joe june)
  1244. for kids in '((bob ken) () () (kris sunshine) ())
  1245. collect name
  1246. append kids)
  1247. \EV (FRED BOB KEN SUE ALICE JOE KRIS SUNSHINE JUNE)
  1248. \endcode
  1249. %% Per X3J13. -kmp 05-Oct-93
  1250. %Multiple
  1251. Any two
  1252. clauses that do not accumulate the same \term{type} of
  1253. \term{object}
  1254. can coexist in a \macref{loop} only
  1255. if each clause accumulates its values into
  1256. a different
  1257. %% Removed per Barmar -- "some can still use the default" (i.e., not supply a var)
  1258. %user-specified
  1259. \term{variable}.
  1260. %Any number of values can
  1261. %be returned from \macref{loop} if \thefunction{values} is used,
  1262. %as the next example shows:
  1263. %
  1264. %\code
  1265. %;; Count and collect names and ages.
  1266. % (loop for name in '(fred sue alice joe june)
  1267. % as age in '(22 26 19 20 10)
  1268. % append (list name age) into name-and-age-list
  1269. % count name into name-count
  1270. % sum age into total-age
  1271. % finally
  1272. % (return
  1273. % (values (round total-age name-count)
  1274. % name-and-age-list)))
  1275. %\EV 19, (FRED 22 SUE 26 ALICE 19 JOE 20 JUNE 10)
  1276. %\endcode
  1277. \beginsubsubsection{Examples of COLLECT clause}
  1278. \code
  1279. ;; Collect all the symbols in a list.
  1280. (loop for i in '(bird 3 4 turtle (1 . 4) horse cat)
  1281. when (symbolp i) collect i)
  1282. \EV (BIRD TURTLE HORSE CAT)
  1283. ;; Collect and return odd numbers.
  1284. (loop for i from 1 to 10
  1285. if (oddp i) collect i)
  1286. \EV (1 3 5 7 9)
  1287. ;; Collect items into local variable, but don't return them.
  1288. (loop for i in '(a b c d) by #'cddr
  1289. collect i into my-list
  1290. finally (print my-list))
  1291. \OUT (A C)
  1292. \EV NIL
  1293. \endcode
  1294. \endsubsubsection%{Examples of COLLECT clause}
  1295. \beginsubsubsection{Examples of APPEND and NCONC clauses}
  1296. \code
  1297. ;; Use APPEND to concatenate some sublists.
  1298. (loop for x in '((a) (b) ((c)))
  1299. append x)
  1300. \EV (A B (C))
  1301. ;; NCONC some sublists together. Note that only lists made by the
  1302. ;; call to LIST are modified.
  1303. (loop for i upfrom 0
  1304. as x in '(a b (c))
  1305. nconc (if (evenp i) (list x) nil))
  1306. \EV (A (C))
  1307. \endcode
  1308. \endsubsubsection%{Examples of APPEND and NCONC clauses}
  1309. \beginsubsubsection{Examples of COUNT clause}
  1310. \code
  1311. (loop for i in '(a b nil c nil d e)
  1312. count i)
  1313. \EV 5
  1314. \endcode
  1315. \endsubsubsection%{Examples of COUNT clause}
  1316. \beginsubsubsection{Examples of MAXIMIZE and MINIMIZE clauses}
  1317. \code
  1318. (loop for i in '(2 1 5 3 4)
  1319. maximize i)
  1320. \EV 5
  1321. (loop for i in '(2 1 5 3 4)
  1322. minimize i)
  1323. \EV 1
  1324. ;; In this example, FIXNUM applies to the internal variable that holds
  1325. ;; the maximum value.
  1326. (setq series '(1.2 4.3 5.7))
  1327. \EV (1.2 4.3 5.7)
  1328. (loop for v in series
  1329. maximize (round v) of-type fixnum)
  1330. \EV 6
  1331. ;; In this example, FIXNUM applies to the variable RESULT.
  1332. (loop for v of-type float in series
  1333. minimize (round v) into result of-type fixnum
  1334. finally (return result))
  1335. \EV 1
  1336. \endcode
  1337. \endsubsubsection%{Examples of MAXIMIZE and MINIMIZE clauses}
  1338. \beginsubsubsection{Examples of SUM clause}
  1339. \code
  1340. (loop for i of-type fixnum in '(1 2 3 4 5)
  1341. sum i)
  1342. \EV 15
  1343. (setq series '(1.2 4.3 5.7))
  1344. \EV (1.2 4.3 5.7)
  1345. (loop for v in series
  1346. sum (* 2.0 v))
  1347. \EV 22.4
  1348. \endcode
  1349. \endsubsubsection%{Examples of SUM clause}
  1350. \endsubsection%{Value Accumulation Clauses}
  1351. \beginsubsection{Termination Test Clauses}
  1352. \DefineSection{LOOPTermTest}
  1353. The \loopref{repeat} construct causes iteration to terminate after a
  1354. specified number of times.
  1355. The loop body executes \i{n} times, where \i{n} is the value
  1356. of the expression \param{form}. The \param{form} argument is evaluated one time
  1357. in the loop prologue. If the expression evaluates to 0 or
  1358. to a negative \term{number}, the loop body is not evaluated.
  1359. The constructs \loopref{always},
  1360. \loopref{never},
  1361. \loopref{thereis},
  1362. \loopref{while},
  1363. \loopref{until},
  1364. and the macro \macref{loop-finish}
  1365. allow conditional termination of iteration within
  1366. a \macref{loop}.
  1367. The constructs \loopref{always}, \loopref{never}, and \loopref{thereis} provide
  1368. specific values to be returned when a \macref{loop} terminates.
  1369. Using \loopref{always}, \loopref{never}, or \loopref{thereis} in a loop with
  1370. value accumulation clauses that are not \loopref{into} causes
  1371. an error \oftype{program-error} to be signaled (at macro expansion time).
  1372. Since \loopref{always}, \loopref{never}, and \loopref{thereis}
  1373. use
  1374. \issue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1375. \thespecop{return-from}
  1376. \endissue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1377. to terminate iteration,
  1378. any \loopref{finally} clause that is supplied is not evaluated
  1379. %Added for JonL
  1380. when exit occurs due to any of these constructs.
  1381. In all other respects these
  1382. constructs behave like the \loopref{while} and \loopref{until} constructs.
  1383. The \loopref{always} construct takes one \term{form} and terminates the
  1384. \macref{loop}
  1385. if the \term{form} ever evaluates to \nil; in this case, it returns
  1386. \nil. Otherwise, it provides a default return value of \t.
  1387. If the value of the supplied \term{form} is never \nil, some other construct
  1388. can terminate the iteration.
  1389. % The {\tt never} construct takes one
  1390. %\term{form} and terminates the \macref{loop}
  1391. % if the \term{form} ever evaluates to \term{non-nil}; in this case, it returns
  1392. % \nil. Otherwise, it provides a default return value of {\tt t}.
  1393. The \loopref{never} construct terminates iteration the first time that
  1394. the value of the supplied \param{form} is \term{non-nil}; the \macref{loop} returns
  1395. \nil.
  1396. If the value of the supplied \param{form} is always \nil, some other
  1397. construct can terminate the iteration.
  1398. Unless some other clause contributes
  1399. a return value, the default value returned is \t.
  1400. % The {\tt thereis} construct takes one \param{form} and terminates the
  1401. %\macref{loop}
  1402. % if the \param{form} ever evaluates to \term{non-nil}; in this case, it returns
  1403. % that value.
  1404. The \loopref{thereis} construct terminates iteration the first time that the
  1405. value of the supplied \param{form} is \term{non-nil}; the \macref{loop} returns the
  1406. value of the supplied \param{form}.
  1407. If the value of the supplied \param{form}
  1408. is always \nil, some other
  1409. construct can terminate the iteration. Unless some other clause contributes a
  1410. return value, the default value returned is \nil.
  1411. %!!! Barmar: Combine this differences info with never/until below.
  1412. There are two differences between the \loopref{thereis} and \loopref{until}
  1413. constructs:
  1414. \beginlist
  1415. \itemitem{\bull} The \loopref{until} construct does not return a value or
  1416. \nil\ based on the value of the supplied \param{form}.
  1417. \itemitem{\bull} The \loopref{until} construct executes
  1418. any \loopref{finally} clause.
  1419. Since \loopref{thereis} uses
  1420. \issue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1421. \thespecop{return-from}
  1422. \endissue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1423. to terminate iteration,
  1424. any \loopref{finally} clause that is supplied is not evaluated
  1425. %Added per JonL
  1426. when exit occurs due to \loopref{thereis}.
  1427. \endlist
  1428. The \loopref{while} construct allows iteration to continue until the
  1429. supplied \param{form}
  1430. evaluates to \term{false}. The supplied \param{form}
  1431. is reevaluated at the location of the \loopref{while} clause.
  1432. The \loopref{until} construct is equivalent to
  1433. \f{while (not \param{form})\dots}. If the value of the
  1434. supplied \param{form} is \term{non-nil}, iteration terminates.
  1435. %% Removed per X3J13. -kmp 4-Oct-93
  1436. % The \loopref{while} and \loopref{until} constructs can be used
  1437. % at any point in a \macref{loop}.
  1438. %% Next two lines (sentences) moved from farther down per X3J13. -kmp 4-Oct-93
  1439. Termination-test control constructs can be used anywhere within the loop body.
  1440. The termination tests are used in the order in which they appear.
  1441. If an \loopref{until} or \loopref{while} clause causes
  1442. termination, any clauses that precede it in the source
  1443. are still evaluated.
  1444. If the \loopref{until} and {\tt while} constructs cause termination,
  1445. control is passed to the loop epilogue, where any \loopref{finally}
  1446. clauses will be executed.
  1447. There are two differences between the \loopref{never} and \loopref{until}
  1448. constructs:
  1449. \beginlist
  1450. \itemitem{\bull} The \loopref{until} construct does not return
  1451. \t\ or \nil\ based on the value of the supplied \param{form}.
  1452. \itemitem{\bull}
  1453. The \loopref{until} construct
  1454. %executes any \loopref{finally} clause.
  1455. does not bypass any \loopref{finally} clauses.
  1456. Since \loopref{never} uses
  1457. \issue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1458. \thespecop{return-from}
  1459. \endissue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1460. to terminate iteration,
  1461. any \loopref{finally} clause that is supplied is not evaluated
  1462. %Added per JonL
  1463. when exit occurs due to \loopref{never}.
  1464. \endlist
  1465. %The macro \macref{loop-finish}
  1466. %can be used at any time to cause normal
  1467. %termination.
  1468. %The macro \macref{loop-finish} terminates iteration normally
  1469. %and returns any accumulated result. If specified, a {\tt finally} clause
  1470. %is evaluated.
  1471. In most cases it is not necessary to use \macref{loop-finish}
  1472. because other loop control clauses terminate the \macref{loop}.
  1473. The macro \macref{loop-finish} is used to provide a normal exit
  1474. from a nested conditional inside a \macref{loop}.
  1475. %You can use \macref{loop-finish}
  1476. %inside nested Lisp code to provide a normal exit from a loop.
  1477. %% Removed per X3J13. -kmp 05-Oct-93
  1478. % In normal termination, \loopref{finally} clauses are
  1479. % executed and default return values are returned.
  1480. Since \macref{loop-finish} transfers control to the loop epilogue,
  1481. using \macref{loop-finish} within a \loopref{finally} expression can cause
  1482. infinite looping.
  1483. %% This information is already available in the macro entry for LOOP-FINISH.
  1484. % % Implementations are allowed to provide \macref{loop-finish}
  1485. % %as a local macro
  1486. % % by using \specref{macrolet}.
  1487. % It is \term{implementation-dependent} whether or not,
  1488. % in a particular \macref{loop} invocation, \macref{loop-finish}
  1489. % is implemented as a global \term{macro} or a local one (created as if by \specref{macrolet}).
  1490. \beginsubsubsection{Examples of REPEAT clause}
  1491. \code
  1492. (loop repeat 3
  1493. do (format t "~&What I say three times is true.~%"))
  1494. \OUT What I say three times is true.
  1495. \OUT What I say three times is true.
  1496. \OUT What I say three times is true.
  1497. \EV NIL
  1498. (loop repeat -15
  1499. do (format t "What you see is what you expect~%"))
  1500. \EV NIL
  1501. \endcode
  1502. \endsubsubsection%{Examples of REPEAT clause}
  1503. \beginsubsubsection{Examples of ALWAYS, NEVER, and THEREIS clauses}
  1504. \code
  1505. ;; Make sure I is always less than 11 (two ways).
  1506. ;; The FOR construct terminates these loops.
  1507. (loop for i from 0 to 10
  1508. always (< i 11))
  1509. \EV T
  1510. (loop for i from 0 to 10
  1511. never (> i 11))
  1512. \EV T
  1513. ;; If I exceeds 10 return I; otherwise, return NIL.
  1514. ;; The THEREIS construct terminates this loop.
  1515. (loop for i from 0
  1516. thereis (when (> i 10) i) )
  1517. \EV 11
  1518. ;;; The FINALLY clause is not evaluated in these examples.
  1519. (loop for i from 0 to 10
  1520. always (< i 9)
  1521. finally (print "you won't see this"))
  1522. \EV NIL
  1523. (loop never t
  1524. finally (print "you won't see this"))
  1525. \EV NIL
  1526. (loop thereis "Here is my value"
  1527. finally (print "you won't see this"))
  1528. \EV "Here is my value"
  1529. ;; The FOR construct terminates this loop, so the FINALLY clause
  1530. ;; is evaluated.
  1531. (loop for i from 1 to 10
  1532. thereis (> i 11)
  1533. finally (prin1 'got-here))
  1534. \OUT GOT-HERE
  1535. \EV NIL
  1536. ;; If this code could be used to find a counterexample to Fermat's
  1537. ;; last theorem, it would still not return the value of the
  1538. ;; counterexample because all of the THEREIS clauses in this example
  1539. ;; only return T. But if Fermat is right, that won't matter
  1540. ;; because this won't terminate.
  1541. (loop for z upfrom 2
  1542. thereis
  1543. (loop for n upfrom 3 below (log z 2)
  1544. thereis
  1545. (loop for x below z
  1546. thereis
  1547. (loop for y below z
  1548. thereis (= (+ (expt x n) (expt y n))
  1549. (expt z n))))))
  1550. \endcode
  1551. \endsubsubsection%{Examples of ALWAYS, NEVER, and THEREIS clauses}
  1552. \beginsubsubsection{Examples of WHILE and UNTIL clauses}
  1553. \code
  1554. (loop while (hungry-p) do (eat))
  1555. ;; UNTIL NOT is equivalent to WHILE.
  1556. (loop until (not (hungry-p)) do (eat))
  1557. ;; Collect the length and the items of STACK.
  1558. (let ((stack '(a b c d e f)))
  1559. (loop for item = (length stack) then (pop stack)
  1560. collect item
  1561. while stack))
  1562. \EV (6 A B C D E F)
  1563. ;; Use WHILE to terminate a loop that otherwise wouldn't terminate.
  1564. ;; Note that WHILE occurs after the WHEN.
  1565. (loop for i fixnum from 3
  1566. when (oddp i) collect i
  1567. while (< i 5))
  1568. \EV (3 5)
  1569. \endcode
  1570. \endsubsubsection%{Examples of WHILE and UNTIL clauses}
  1571. \endsubsection%{Termination Test Clauses}
  1572. \beginsubsection{Unconditional Execution Clauses}
  1573. \DefineSection{LOOPUnconditional}
  1574. The \loopref{do} and \loopref{doing} constructs
  1575. evaluate the
  1576. supplied \param{forms}
  1577. wherever they occur in the expanded form of \macref{loop}.
  1578. The \param{form} argument can be any \term{compound form}.
  1579. Each \param{form} is evaluated in every iteration.
  1580. Because every loop clause must begin with a \term{loop keyword},
  1581. the keyword \loopref{do} is used when no control action other than execution is
  1582. required.
  1583. %!!! This was said somewhere already. -kmp 12-May-91
  1584. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1585. %The \loopref{return} construct takes one \term{form} and returns its \term{values}.
  1586. The \loopref{return} construct takes one \term{form}.
  1587. Any \term{values} returned by the \term{form}
  1588. are immediately returned by the \macref{loop} form.
  1589. % It is equivalent to the clause \f{do (return \i{form})}.
  1590. It is equivalent to the clause
  1591. \f{do (return-from \i{block-name} \i{value})},
  1592. where \i{block-name} is the name specified in a \loopref{named}
  1593. clause, or \nil\ if there is no \loopref{named} clause.
  1594. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1595. \beginsubsubsection{Examples of unconditional execution}
  1596. \code
  1597. ;; Print numbers and their squares.
  1598. ;; The DO construct applies to multiple forms.
  1599. (loop for i from 1 to 3
  1600. do (print i)
  1601. (print (* i i)))
  1602. \OUT 1
  1603. \OUT 1
  1604. \OUT 2
  1605. \OUT 4
  1606. \OUT 3
  1607. \OUT 9
  1608. \EV NIL
  1609. \endcode
  1610. \endsubsubsection%{Examples of unconditional execution}
  1611. \endsubsection%{Unconditional Execution Clauses}
  1612. \beginsubsection{Conditional Execution Clauses}
  1613. \DefineSection{LOOPConditional}
  1614. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1615. % The
  1616. % \loopref{if}, \loopref{when}, and \loopref{unless}
  1617. % constructs
  1618. % establish conditional control in a \macref{loop}.
  1619. % If the supplied condition is \term{true}, the succeeding loop clause
  1620. % is executed. If the supplied condition is not true, the succeeding clause is
  1621. % skipped, and program control moves to the clause that follows
  1622. % the \term{loop keyword} \loopref{else}. If the supplied condition is not true and no
  1623. % \loopref{else} clause is supplied, control is transferred to the
  1624. % clause or construct following the supplied condition.
  1625. % %the entire conditional construct
  1626. % %is skipped.
  1627. %
  1628. % The constructs \loopref{if} and \loopref{when} allow execution of
  1629. % loop clauses conditionally. These constructs are synonyms and
  1630. % can be used interchangeably.
  1631. % If the value of the test expression \param{form} is \term{non-nil}, the expression
  1632. % \param{clause1} is evaluated. If the test expression evaluates to \nil\
  1633. % and an \loopref{else} construct is supplied, the \term{forms} that follow the
  1634. % \loopref{else} are evaluated; otherwise, control passes to the next clause.
  1635. % If \loopref{if} or \loopref{when} clauses are nested, each \loopref{else} is
  1636. % paired with the closest preceding \loopref{if} or \loopref{when} construct that has
  1637. % no associated \loopref{else} or \loopref{end}.
  1638. %
  1639. % The \loopref{unless} construct is equivalent to \f{when (not \param{form})}
  1640. % and \f{if (not \param{form})}.
  1641. % If the value of the test expression \param{form} is \nil, the expression
  1642. % \param{clause1} is evaluated. If the test expression evaluates to
  1643. % \term{non-nil} and an \loopref{else} construct is supplied, the statements that follow the
  1644. % \loopref{else} are evaluated; otherwise, no conditional statement is evaluated.
  1645. % The \param{clause} arguments must be either accumulation, unconditional,
  1646. % or conditional clauses.
  1647. The \loopref{if}, \loopref{when}, and \loopref{unless} constructs
  1648. establish conditional control in a \macref{loop}. If the test
  1649. passes, the succeeding loop clause is executed. If the test does
  1650. not pass, the succeeding clause is skipped, and program control
  1651. moves to the clause that follows the \term{loop keyword}
  1652. \loopref{else}. If the test does not pass and no \loopref{else}
  1653. clause is supplied, control is transferred to the clause or
  1654. construct following the entire conditional clause.
  1655. If conditional clauses are nested, each \loopref{else} is paired
  1656. with the closest preceding conditional clause that has no
  1657. associated \loopref{else} or \loopref{end}.
  1658. In the \loopref{if} and \loopref{when} clauses, which are
  1659. synonymous, the test passes if the value of \param{form} is
  1660. \term{true}.
  1661. %% Per X3J13. -kmp 05-Oct-93
  1662. % The \loopref{unless} \param{form} construct
  1663. % is equivalent to \f{when (not \param{form})};
  1664. % the test passes if the value of \param{form} is \term{false}.
  1665. In the \loopref{unless} clause,
  1666. the test passes if the value of \param{form} is \term{false}.
  1667. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1668. Clauses that follow the test expression can be grouped by using
  1669. the \term{loop keyword} \loopref{and} to produce a conditional block consisting of
  1670. a compound clause.
  1671. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1672. % The \term{loop keyword} \loopref{it} can be used to refer to the result of the
  1673. % test expression in a clause. If multiple clauses are connected
  1674. % with \loopref{and},
  1675. % the \loopref{it} construct must be in the first clause in the block. Since \loopref{it}
  1676. % is a \term{loop keyword}, \loopref{it} cannot be used as a local variable within
  1677. % \macref{loop}.
  1678. The \term{loop keyword} \loopref{it} can be used to refer to the result
  1679. of the test expression in a clause.
  1680. Use the \term{loop keyword} \loopref{it} in place of the form in a
  1681. \loopref{return} clause or an \i{accumulation} clause that is
  1682. inside a conditional execution clause.
  1683. If multiple clauses are connected with \loopref{and}, the \loopref{it}
  1684. construct must be in the first clause in the block.
  1685. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1686. The optional \term{loop keyword} \loopref{end} marks the end of the clause. If this
  1687. keyword is not supplied, the next \term{loop keyword} marks the end. The construct
  1688. \loopref{end} can be used to distinguish the scoping of compound clauses.
  1689. \beginsubsubsection{Examples of WHEN clause}
  1690. \code
  1691. ;; Signal an exceptional condition.
  1692. (loop for item in '(1 2 3 a 4 5)
  1693. when (not (numberp item))
  1694. return (cerror "enter new value" "non-numeric value: ~s" item))
  1695. Error: non-numeric value: A
  1696. ;; The previous example is equivalent to the following one.
  1697. (loop for item in '(1 2 3 a 4 5)
  1698. when (not (numberp item))
  1699. do (return
  1700. (cerror "Enter new value" "non-numeric value: ~s" item)))
  1701. Error: non-numeric value: A
  1702. \endcode
  1703. % Removed ";; The FINALLY clause prints the last value of I." per X3J13. -kmp 05-Oct-93
  1704. \code
  1705. ;; This example parses a simple printed string representation from
  1706. ;; BUFFER (which is itself a string) and returns the index of the
  1707. ;; closing double-quote character.
  1708. (let ((buffer "\\"a\\" \\"b\\""))
  1709. (loop initially (unless (char= (char buffer 0) #\\")
  1710. (loop-finish))
  1711. for i of-type fixnum from 1 below (length (the string buffer))
  1712. when (char= (char buffer i) #\\")
  1713. return i))
  1714. \EV 2
  1715. ;; The collected value is returned.
  1716. (loop for i from 1 to 10
  1717. when (> i 5)
  1718. collect i
  1719. finally (prin1 'got-here))
  1720. \OUT GOT-HERE
  1721. \EV (6 7 8 9 10)
  1722. ;; Return both the count of collected numbers and the numbers.
  1723. (loop for i from 1 to 10
  1724. when (> i 5)
  1725. collect i into number-list
  1726. and count i into number-count
  1727. finally (return (values number-count number-list)))
  1728. \EV 5, (6 7 8 9 10)
  1729. \endcode
  1730. \endsubsubsection%{Examples of WHEN clause}
  1731. \endsubsection%{Conditional Execution Clauses}
  1732. \beginsubsection{Miscellaneous Clauses}
  1733. \DefineSection{LOOPMisc}
  1734. \beginsubsubsection{Control Transfer Clauses}
  1735. The \loopref{named} construct
  1736. establishes a name for an \term{implicit block} surrounding the
  1737. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1738. entire
  1739. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1740. \macref{loop} so that \thespecop{return-from} can be used to return
  1741. values from or to exit \macref{loop}.
  1742. Only one name per \macref{loop} \term{form} can be assigned.
  1743. If used, the \loopref{named} construct must be the first clause in the loop expression.
  1744. %!!! This is said already above (twice). Consolidate? -kmp 29-Apr-93
  1745. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1746. The \loopref{return} construct takes one \term{form}.
  1747. Any \term{values} returned by the \term{form}
  1748. are immediately returned by the \macref{loop} form.
  1749. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1750. This construct is similar to \thespecop{return-from} and \themacro{return}.
  1751. % The Loop Facility supports the \loopref{return} construct for backwards
  1752. %compatibility with older loop implementations.
  1753. The \loopref{return} construct
  1754. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1755. %returns immediately and
  1756. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1757. does not execute any \loopref{finally} clause that
  1758. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1759. the \macref{loop} \term{form}
  1760. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1761. is given.
  1762. \beginsubsubsubsection{Examples of NAMED clause}
  1763. \code
  1764. ;; Just name and return.
  1765. (loop named max
  1766. for i from 1 to 10
  1767. do (print i)
  1768. do (return-from max 'done))
  1769. \OUT 1
  1770. \EV DONE
  1771. \endcode
  1772. \endsubsubsubsection%{Examples of NAMED clause}
  1773. \endsubsubsection%{Control Transfer Clauses}
  1774. \beginsubsubsection{Initial and Final Execution}
  1775. The \loopref{initially} and \loopref{finally} constructs
  1776. evaluate forms that occur before and after the loop body.
  1777. The \loopref{initially} construct causes the supplied
  1778. %% Per X3J13. -kmp 05-Oct-93
  1779. % \param{form}
  1780. \param{compound-forms}
  1781. to be evaluated
  1782. in the loop prologue, which precedes all loop code except for
  1783. initial settings supplied by constructs \loopref{with}, \loopref{for}, or
  1784. \loopref{as}.
  1785. The code for any \loopref{initially} clauses is
  1786. %"collected into one \specref{progn}" => "executed" per barmar:
  1787. % They don't have to be collected into a real PROGN since the LOOP expands into a TAGBODY.
  1788. % -kmp 31-Jul-91
  1789. executed
  1790. in the order in which the clauses appeared in
  1791. the \macref{loop}.
  1792. %The collected code is executed once in the loop prologue
  1793. % after any implicit variable initializations.
  1794. %% Removed per X3J13. -kmp 05-Oct-93
  1795. %The \param{form} argument can be any \term{compound form}.
  1796. The \loopref{finally} construct causes the supplied
  1797. %% Per X3J13. -kmp 05-Oct-93
  1798. %\param{form}
  1799. \param{compound-forms}
  1800. to be evaluated
  1801. in the loop epilogue after normal iteration terminates.
  1802. The code for any \loopref{finally} clauses is
  1803. %collected into one \specref{progn}
  1804. executed
  1805. in the order in which the clauses appeared in
  1806. the \macref{loop}. The collected code is executed once in the loop epilogue
  1807. before any implicit values are returned from the accumulation clauses.
  1808. % Explicit \macref{return} \term{forms} in
  1809. An explicit transfer of control (\eg by \macref{return}, \specref{go}, or \specref{throw})
  1810. from the loop body, however, will exit the
  1811. \macref{loop} without executing the epilogue code.
  1812. %% Removed per X3J13. -kmp 05-Oct-93
  1813. % The \param{form} argument can be any \term{compound form}.
  1814. Clauses such as \loopref{return}, \loopref{always}, \loopref{never}, and
  1815. \loopref{thereis}
  1816. can bypass the \loopref{finally} clause.
  1817. \issue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1818. \macref{return} (or \specref{return-from}, if the \loopref{named} option was supplied)
  1819. \endissue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1820. can be used after \loopref{finally} to return values from a \macref{loop}.
  1821. \issue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1822. %The evaluation of the \macref{return} form
  1823. Such an \term{explicit return}
  1824. \endissue{LOOP-NAMED-BLOCK-NIL:OVERRIDE}
  1825. inside the
  1826. \loopref{finally} clause takes precedence over returning the accumulation
  1827. from clauses supplied by such keywords as \loopref{collect}, \loopref{nconc},
  1828. \loopref{append}, \loopref{sum}, \loopref{count}, \loopref{maximize}, and
  1829. \loopref{minimize};
  1830. the accumulation values for these preempted clauses are not returned by
  1831. \macref{loop} if \macref{return} or \specref{return-from} is used.
  1832. %Clauses such as {\tt return}, {\tt always}, {\tt never}, and {\tt thereis}
  1833. %defeat the {\tt finally} clause.
  1834. %% Removed per Barmar: Said much earlier.
  1835. % The constructs \loopref{do}, \loopref{initially}, and \loopref{finally} are the
  1836. % only \term{loop keywords} that take an arbitrary number of \term{forms} and group
  1837. % them as if by using an \term{implicit progn}.
  1838. \endsubsubsection%{Initial and Final Execution}
  1839. \endsubsection%{Miscellaneous Clauses}
  1840. \beginsubSection{Examples of Miscellaneous Loop Features}
  1841. \code
  1842. (let ((i 0)) ; no loop keywords are used
  1843. (loop (incf i) (if (= i 3) (return i)))) \EV 3
  1844. (let ((i 0)(j 0))
  1845. (tagbody
  1846. (loop (incf j 3) (incf i) (if (= i 3) (go exit)))
  1847. exit)
  1848. j) \EV 9
  1849. \endcode
  1850. In the following example, the variable \f{x} is stepped
  1851. before \f{y} is stepped; thus, the value of \f{y}
  1852. reflects the updated value of \f{x}:
  1853. \code
  1854. (loop for x from 1 to 10
  1855. for y = nil then x
  1856. collect (list x y))
  1857. \EV ((1 NIL) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9) (10 10))
  1858. \endcode
  1859. In this example, \f{x} and \f{y} are stepped in \term{parallel}:
  1860. \code
  1861. (loop for x from 1 to 10
  1862. and y = nil then x
  1863. collect (list x y))
  1864. \EV ((1 NIL) (2 1) (3 2) (4 3) (5 4) (6 5) (7 6) (8 7) (9 8) (10 9))
  1865. \endcode
  1866. \beginsubsubsection{Examples of clause grouping}
  1867. \code
  1868. ;; Group conditional clauses.
  1869. (loop for i in '(1 324 2345 323 2 4 235 252)
  1870. when (oddp i)
  1871. do (print i)
  1872. and collect i into odd-numbers
  1873. and do (terpri)
  1874. else ; I is even.
  1875. collect i into even-numbers
  1876. finally
  1877. (return (values odd-numbers even-numbers)))
  1878. \OUT 1
  1879. \OUT
  1880. \OUT 2345
  1881. \OUT
  1882. \OUT 323
  1883. \OUT
  1884. \OUT 235
  1885. \EV (1 2345 323 235), (324 2 4 252)
  1886. ;; Collect numbers larger than 3.
  1887. (loop for i in '(1 2 3 4 5 6)
  1888. when (and (> i 3) i)
  1889. collect it) ; IT refers to (and (> i 3) i).
  1890. \EV (4 5 6)
  1891. ;; Find a number in a list.
  1892. (loop for i in '(1 2 3 4 5 6)
  1893. when (and (> i 3) i)
  1894. return it)
  1895. \EV 4
  1896. ;; The above example is similar to the following one.
  1897. (loop for i in '(1 2 3 4 5 6)
  1898. thereis (and (> i 3) i))
  1899. \EV 4
  1900. \medbreak
  1901. ;; Nest conditional clauses.
  1902. (let ((list '(0 3.0 apple 4 5 9.8 orange banana)))
  1903. (loop for i in list
  1904. when (numberp i)
  1905. when (floatp i)
  1906. collect i into float-numbers
  1907. else ; Not (floatp i)
  1908. collect i into other-numbers
  1909. else ; Not (numberp i)
  1910. when (symbolp i)
  1911. collect i into symbol-list
  1912. else ; Not (symbolp i)
  1913. do (error "found a funny value in list ~S, value ~S~%" list i)
  1914. finally (return (values float-numbers other-numbers symbol-list))))
  1915. \EV (3.0 9.8), (0 4 5), (APPLE ORANGE BANANA)
  1916. ;; Without the END preposition, the last AND would apply to the
  1917. ;; inner IF rather than the outer one.
  1918. (loop for x from 0 to 3
  1919. do (print x)
  1920. if (zerop (mod x 2))
  1921. do (princ " a")
  1922. and if (zerop (floor x 2))
  1923. do (princ " b")
  1924. end
  1925. and do (princ " c"))
  1926. \OUT 0 a b c
  1927. \OUT 1
  1928. \OUT 2 a c
  1929. \OUT 3
  1930. \EV NIL
  1931. \endcode
  1932. \endsubsubsection%{Examples of clause grouping}
  1933. \endsubSection%{Examples of Miscellaneous Loop Features}
  1934. \beginsubSection{Notes about Loop}
  1935. \term{Types} can be supplied for loop variables.
  1936. It is not necessary to supply a \term{type} for any variable,
  1937. but supplying the \term{type}
  1938. can ensure that the variable has a correctly typed initial value,
  1939. and it can also enable compiler optimizations
  1940. (depending on the \term{implementation}).
  1941. The clause \loopref{repeat} \i{n} ... is roughly equivalent to a clause such as
  1942. \code
  1943. (loop for \i{internal-variable} downfrom (- \i{n} 1) to 0 ...)
  1944. \endcode
  1945. but in some \term{implementations},
  1946. the \loopref{repeat} construct might be more efficient.
  1947. Within the executable parts of the loop clauses and around the entire
  1948. \macref{loop} form, variables can be bound by using \specref{let}.
  1949. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1950. Use caution when using a variable named {\tt IT} (in any \term{package})
  1951. in connection with \macref{loop}, since \loopref{it} is a \term{loop keyword}
  1952. that can be used in place of a \term{form} in certain contexts.
  1953. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1954. There is
  1955. %currently no specified portable
  1956. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1957. no
  1958. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  1959. \term{standardized}
  1960. mechanism for users to add
  1961. extensions to \macref{loop}.
  1962. % The names \f{defloop} and \f{define-loop-method}
  1963. % have been suggested as candidates for such a method.
  1964. \endsubSection%{Notes about Loop}