dict-iteration.tex 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. % -*- Mode: tex -*-
  2. % Iteration
  3. %-------------------- Iteration --------------------
  4. %%% ========== DO
  5. %%% ========== DO*
  6. \begincom{do, do*}\ftype{Macro}
  7. \issue{DECLS-AND-DOC}
  8. \label Syntax::
  9. \issue{VARIABLE-LIST-ASYMMETRY:SYMMETRIZE}
  10. \DefmacWithValuesNewline do
  11. {\vtop{\hbox{\paren{\star{\VarInitStep}}}
  12. \hbox{\paren{end-test-form \starparam{result-form}}}
  13. \hbox{\starparam{declaration} \star{\curly{tag $\vert$ statement}}}}}
  14. {\starparam{result}}
  15. %% 7.8.2 5
  16. \DefmacWithValuesNewline {do*}
  17. {\vtop{\hbox{\paren{\star{\VarInitStep}}}
  18. \hbox{\paren{end-test-form {\starparam{result-form}}}}
  19. \hbox{\starparam{declaration} \star{\curly{tag $\vert$ statement}}}}}
  20. {\starparam{result}}
  21. \endissue{VARIABLE-LIST-ASYMMETRY:SYMMETRIZE}
  22. \label Arguments and Values::
  23. %% 7.8.2 6
  24. %% 7.8.2 7
  25. %% 7.8.3 12
  26. \param{var}---a \term{symbol}.
  27. \param{init-form}---a \term{form}.
  28. \param{step-form}---a \term{form}.
  29. %% 7.8.2 9
  30. \param{end-test-form}---a \term{form}.
  31. \param{result-forms}---an \term{implicit progn}.
  32. %% 7.8.2 14
  33. \param{declaration}---a \misc{declare} \term{expression}; \noeval.
  34. \param{tag}---a \term{go tag}; \noeval.
  35. \param{statement}---a \term{compound form}; \evalspecial.
  36. \param{results}---if a \macref{return} or \macref{return-from} form is executed,
  37. the \term{values} passed from that \term{form};
  38. %% 7.9.2 13
  39. otherwise, the \term{values} returned by the \param{result-forms}.
  40. \label Description::
  41. \macref{do} iterates over a group of \param{statements}
  42. while a test condition holds.
  43. \macref{do} accepts an arbitrary number of iteration \param{vars}
  44. which are bound within the iteration and stepped in parallel.
  45. An initial value may be supplied for each iteration variable by use of
  46. an \param{init-form}.
  47. \param{Step-forms} may be used to specify how the
  48. \param{vars} should be updated on succeeding iterations through the loop.
  49. \param{Step-forms} may be used both to generate successive
  50. values or to accumulate results.
  51. If the \param{end-test-form} condition
  52. is met prior to an execution of the body, the iteration terminates.
  53. \param{Tags} label \param{statements}.
  54. \macref{do*} is exactly like \macref{do}
  55. except that the \term{bindings} and steppings
  56. of the \param{vars} are performed sequentially rather than in parallel.
  57. %% 7.8.2 4
  58. %% 7.8.2 8
  59. Before the first iteration, all the \param{init-forms} are evaluated, and
  60. each \param{var} is bound to the value of its respective \param{init-form},
  61. if supplied.
  62. This is a \term{binding}, not an assignment; when the loop terminates,
  63. the old values of those variables will be restored.
  64. For \macref{do}, all
  65. of the \param{init-forms} are evaluated before any \param{var}
  66. is bound. The
  67. \param{init-forms} can refer to the \term{bindings} of the \param{vars}
  68. visible before beginning execution of
  69. \macref{do}.
  70. For \macref{do*}, the first \param{init-form} is evaluated, then the first
  71. \param{var} is bound to that value, then the second \param{init-form}
  72. is evaluated, then the second \param{var} is bound, and so on;
  73. in general, the \i{k}th \param{init-form} can refer to the new binding
  74. of the \i{j}th \param{var} if \i{j} < \i{k}, and otherwise to the
  75. old binding of the \i{j}th \param{var}.
  76. At the beginning of each iteration, after processing the variables,
  77. the \param{end-test-form} is evaluated. If the result is
  78. \term{false}, execution proceeds with the body of the \macref{do}
  79. (or \macref{do*}) form.
  80. If the result is \term{true}, the \param{result-forms} are evaluated in order
  81. as an \term{implicit progn},
  82. and then \macref{do} or \macref{do*} returns.
  83. %% 7.8.2 10
  84. At the beginning of each iteration other than the first,
  85. \param{vars} are updated as follows. All the \param{step-forms}, if supplied,
  86. are evaluated, from left to right, and the resulting values are
  87. assigned to the respective \param{vars}.
  88. Any \param{var} that has no associated \param{step-form} is not assigned to.
  89. For \macref{do}, all the \param{step-forms} are evaluated before any \param{var}
  90. is updated; the assignment of values to \param{vars} is done in parallel,
  91. as if by \macref{psetq}.
  92. Because all of the \param{step-forms} are evaluated before any
  93. of the \param{vars} are altered, a \param{step-form} when evaluated always has
  94. access to the old values of all the \param{vars}, even if other \param{step-forms}
  95. precede it.
  96. For \macref{do*}, the first \param{step-form} is evaluated, then the
  97. value is assigned to the first \param{var}, then the second \param{step-form}
  98. is evaluated, then the value is assigned to the second \param{var}, and so on;
  99. the assignment of values to variables is done sequentially,
  100. as if by \specref{setq}.
  101. For either \macref{do} or \macref{do*},
  102. after the \param{vars} have been updated,
  103. the \param{end-test-form}
  104. is evaluated as described above, and the iteration continues.
  105. %% 7.8.2 12
  106. The remainder of the \macref{do} (or \macref{do*}) form constitutes
  107. an \term{implicit tagbody}.
  108. \param{Tags} may appear within the body of a \macref{do} loop
  109. for use by \specref{go} statements appearing in the body (but such \specref{go}
  110. statements may not appear in the variable specifiers, the \param{end-test-form},
  111. or the \param{result-forms}).
  112. When the end of a \macref{do} body is reached, the next iteration cycle
  113. (beginning with the evaluation of \param{step-forms}) occurs.
  114. %% 7.8.2 13
  115. An \term{implicit block} named \nil\ surrounds the entire \macref{do}
  116. (or \macref{do*}) form.
  117. A \macref{return} statement may be used at any point to exit the loop
  118. immediately.
  119. \param{Init-form} is an
  120. initial value for the \param{var} with which it is associated.
  121. If \param{init-form} is omitted, the initial value of \param{var} is \nil.
  122. If a \param{declaration} is supplied for a \param{var}, \param{init-form}
  123. must be consistent with the \param{declaration}.
  124. %% Barmar: Redundant.
  125. % If \param{step-form} is omitted, the \param{var}
  126. % with which it is associated is not changed by \macref{do}
  127. % between repetitions.
  128. \param{Declarations} can appear at the beginning of a \macref{do}
  129. (or \macref{do*}) body.
  130. They apply to code in the \macref{do} (or \macref{do*}) body,
  131. to the \term{bindings} of the \macref{do} (or \macref{do*})
  132. \param{vars},
  133. %% Barmar: The part about init forms was changed by NO-HOISTING.
  134. %% JonL: Agree
  135. % to the \param{init-forms},
  136. to the \param{step-forms},
  137. to the \param{end-test-form}, and to the \param{result-forms}.
  138. %For \macref{do}, the
  139. %\term{scope} of the name binding does not include any
  140. %initial value form, but does include the stepper forms and optional result
  141. %forms.
  142. %For \macref{do*}, a variable's \term{scope} also includes the
  143. % remaining initial value forms for subsequent variable bindings.
  144. \label Examples::
  145. %% 7.8.2 16
  146. \code
  147. (do ((temp-one 1 (1+ temp-one))
  148. (temp-two 0 (1- temp-two)))
  149. ((> (- temp-one temp-two) 5) temp-one)) \EV 4
  150. (do ((temp-one 1 (1+ temp-one))
  151. (temp-two 0 (1+ temp-one)))
  152. ((= 3 temp-two) temp-one)) \EV 3
  153. (do* ((temp-one 1 (1+ temp-one))
  154. (temp-two 0 (1+ temp-one)))
  155. ((= 3 temp-two) temp-one)) \EV 2
  156. (do ((j 0 (+ j 1)))
  157. (nil) ;Do forever.
  158. (format t "~%Input ~D:" j)
  159. (let ((item (read)))
  160. (if (null item) (return) ;Process items until NIL seen.
  161. (format t "~&Output ~D: ~S" j item))))
  162. \OUT Input 0: \IN{banana}
  163. \OUT Output 0: BANANA
  164. \OUT Input 1: \IN{(57 boxes)}
  165. \OUT Output 1: (57 BOXES)
  166. \OUT Input 2: \IN{NIL}
  167. \EV NIL
  168. (setq a-vector (vector 1 nil 3 nil))
  169. (do ((i 0 (+ i 1)) ;Sets every null element of a-vector to zero.
  170. (n (array-dimension a-vector 0)))
  171. ((= i n))
  172. (when (null (aref a-vector i))
  173. (setf (aref a-vector i) 0))) \EV NIL
  174. a-vector \EV #(1 0 3 0)
  175. \endcode
  176. \code
  177. (do ((x e (cdr x))
  178. (oldx x x))
  179. ((null x))
  180. body)
  181. \endcode
  182. is an example of parallel assignment to index variables. On the first
  183. iteration, the value of \f{oldx} is whatever value \f{x} had before
  184. the \macref{do} was entered. On succeeding iterations, \f{oldx} contains
  185. the value that \f{x} had on the previous iteration.
  186. %% 7.8.2 17
  187. \code
  188. (do ((x foo (cdr x))
  189. (y bar (cdr y))
  190. (z '() (cons (f (car x) (car y)) z)))
  191. ((or (null x) (null y))
  192. (nreverse z)))
  193. \endcode
  194. does the same thing as \f{(mapcar \#'f foo bar)}. The step
  195. computation for \f{z} is an example of the fact that variables
  196. are stepped in parallel.
  197. Also, the body of the loop is empty.
  198. \code
  199. (defun list-reverse (list)
  200. (do ((x list (cdr x))
  201. (y '() (cons (car x) y)))
  202. ((endp x) y)))
  203. \endcode
  204. %% 7.8.2 18
  205. As an example of nested iterations, consider a data structure that is a
  206. \term{list} of \term{conses}. The \term{car} of each \term{cons} is a
  207. \term{list} of \term{symbols},
  208. and the \term{cdr} of each \term{cons} is a
  209. \term{list} of equal length containing
  210. corresponding values. Such a data structure is similar to an association
  211. list,
  212. but is divided into ``frames''; the overall structure resembles a rib-cage.
  213. A lookup function on such a data structure might be:
  214. \code
  215. (defun ribcage-lookup (sym ribcage)
  216. (do ((r ribcage (cdr r)))
  217. ((null r) nil)
  218. (do ((s (caar r) (cdr s))
  219. (v (cdar r) (cdr v)))
  220. ((null s))
  221. (when (eq (car s) sym)
  222. (return-from ribcage-lookup (car v)))))) \EV RIBCAGE-LOOKUP
  223. \endcode
  224. \label Affected By:\None.
  225. \label Exceptional Situations:\None.
  226. \label See Also::
  227. other iteration functions
  228. (\macref{dolist}, \macref{dotimes}, and \macref{loop})
  229. and more primitive functionality
  230. (\specref{tagbody}, \specref{go}, \specref{block}, \macref{return},
  231. \specref{let}, and \specref{setq})
  232. \label Notes::
  233. %% 7.8.2 11
  234. If \param{end-test-form} is \nil, the test will never succeed.
  235. This provides an idiom for ``do forever'':
  236. the body of the \macref{do} or \macref{do*}
  237. is executed repeatedly.
  238. The infinite loop can be terminated by the use of \macref{return},
  239. \specref{return-from}, \specref{go} to an outer level, or \specref{throw}.
  240. %% 7.8.2 19
  241. A \macref{do} \term{form} may be explained in terms of the more primitive \term{forms}
  242. \specref{block}, \macref{return},
  243. \specref{let}, \macref{loop}, \specref{tagbody},
  244. and \macref{psetq} as follows:
  245. \code
  246. (block nil
  247. (let ((var1 init1)
  248. (var2 init2)
  249. ...
  250. (varn initn))
  251. \i{declarations}
  252. (loop (when end-test (return (progn . result)))
  253. (tagbody . tagbody)
  254. (psetq var1 step1
  255. var2 step2
  256. ...
  257. varn stepn))))
  258. \endcode
  259. \macref{do*} is similar, except that \specref{let*} and \specref{setq} replace
  260. the \specref{let} and \macref{psetq}, respectively.
  261. \endissue{DECLS-AND-DOC}
  262. \endcom
  263. %%% ========== DOTIMES
  264. \begincom{dotimes}\ftype{Macro}
  265. \issue{DECLS-AND-DOC}
  266. \label Syntax::
  267. \DefmacWithValuesNewline dotimes
  268. {\paren{var count-form \brac{result-form}}
  269. \starparam{declaration}
  270. \star{\curly{tag | statement}}}
  271. {\starparam{result}}
  272. \label Arguments and Values::
  273. \param{var}---a \term{symbol}.
  274. \param{count-form}---a \term{form}.
  275. \param{result-form}---a \term{form}.
  276. \param{declaration}---a \misc{declare} \term{expression}; \noeval.
  277. \param{tag}---a \term{go tag}; \noeval.
  278. \param{statement}---a \term{compound form}; \evalspecial.
  279. %% 7.8.3 4
  280. \param{results}---if a \macref{return} or \macref{return-from} form is executed,
  281. the \term{values} passed from that \term{form};
  282. otherwise, the \term{values} returned by the \param{result-form}
  283. or \nil\ if there is no \param{result-form}.
  284. \label Description::
  285. %% 7.8.3 9
  286. \macref{dotimes} iterates over a series of \term{integers}.
  287. \macref{dotimes} evaluates \param{count-form},
  288. which should produce an \term{integer}.
  289. If \param{count-form} is zero or negative,
  290. the body is not executed.
  291. \macref{dotimes} then executes the body once for each \term{integer} from 0 up to
  292. but not including
  293. the value of \param{count-form},
  294. in the order in which the
  295. \param{tags} and \param{statements} occur, with
  296. \param{var} bound to each \term{integer}.
  297. Then \param{result-form}
  298. is evaluated.
  299. At the time \param{result-form} is processed, \param{var} is bound to
  300. the number of times the body was executed.
  301. \param{Tags} label
  302. \param{statements}.
  303. An \term{implicit block}
  304. %In effect, a \specref{block}
  305. named \nil\ surrounds \macref{dotimes}.
  306. %% 7.8.3 5
  307. %% 7.8.3 10
  308. \macref{return} may be used to terminate the loop immediately without
  309. performing any further iterations, returning zero or more \term{values}.
  310. The body of the loop is an \term{implicit tagbody};
  311. it may contain tags to serve as the targets of \specref{go} statements.
  312. Declarations may appear before the body of the loop.
  313. % Per Loosemore #28
  314. % For \macref{dotimes}, the \term{scope} of the name binding
  315. % does not include any initial value form,
  316. % but the stepper and optional result forms are included.
  317. The \term{scope} of the binding of \param{var}
  318. does not include the \param{count-form},
  319. but the \param{result-form} is included.
  320. % Added per Loosemore #27, first public review
  321. It is \term{implementation-dependent} whether \macref{dotimes}
  322. \term{establishes} a new \term{binding} of \param{var} on each iteration
  323. or whether it \term{establishes} a binding for \param{var} once at the
  324. beginning and then \param{assigns} it on any subsequent iterations.
  325. \label Examples::
  326. \code
  327. (dotimes (temp-one 10 temp-one)) \EV 10
  328. (setq temp-two 0) \EV 0
  329. (dotimes (temp-one 10 t) (incf temp-two)) \EV T
  330. temp-two \EV 10
  331. \endcode
  332. %% 7.8.3 11
  333. Here is an example of the use of \f{dotimes} in processing strings:
  334. \code
  335. ;;; True if the specified subsequence of the string is a
  336. ;;; palindrome (reads the same forwards and backwards).
  337. (defun palindromep (string \optional
  338. (start 0)
  339. (end (length string)))
  340. (dotimes (k (floor (- end start) 2) t)
  341. (unless (char-equal (char string (+ start k))
  342. (char string (- end k 1)))
  343. (return nil))))
  344. (palindromep "Able was I ere I saw Elba") \EV T
  345. (palindromep "A man, a plan, a canal--Panama!") \EV NIL
  346. (remove-if-not #'alpha-char-p ;Remove punctuation.
  347. "A man, a plan, a canal--Panama!")
  348. \EV "AmanaplanacanalPanama"
  349. (palindromep
  350. (remove-if-not #'alpha-char-p
  351. "A man, a plan, a canal--Panama!")) \EV T
  352. (palindromep
  353. (remove-if-not
  354. #'alpha-char-p
  355. "Unremarkable was I ere I saw Elba Kramer, nu?")) \EV T
  356. (palindromep
  357. (remove-if-not
  358. #'alpha-char-p
  359. "A man, a plan, a cat, a ham, a yak,
  360. a yam, a hat, a canal--Panama!")) \EV T
  361. \endcode
  362. \label Side Effects:\None.
  363. \label Affected By:\None.
  364. \label Exceptional Situations:\None.
  365. \label See Also::
  366. \macref{do}, \macref{dolist}, \specref{tagbody}
  367. \label Notes::
  368. \specref{go} may be used within the body of
  369. \macref{dotimes} to transfer control to a statement labeled by a \param{tag}.
  370. \endissue{DECLS-AND-DOC}
  371. \endcom
  372. %%% ========== DOLIST
  373. \begincom{dolist}\ftype{Macro}
  374. \issue{DECLS-AND-DOC}
  375. \label Syntax::
  376. \DefmacWithValuesNewline dolist
  377. {\paren{var list-form \brac{result-form}}
  378. \starparam{declaration}
  379. \star{\curly{tag | statement}}}
  380. {\starparam{result}}
  381. \label Arguments and Values::
  382. \param{var}---a \term{symbol}.
  383. \param{list-form}---a \term{form}.
  384. \param{result-form}---a \term{form}.
  385. \param{declaration}---a \misc{declare} \term{expression}; \noeval.
  386. \param{tag}---a \term{go tag}; \noeval.
  387. \param{statement}---a \term{compound form}; \evalspecial.
  388. %% 7.8.3 4
  389. \param{results}---if a \macref{return} or \macref{return-from} form is executed,
  390. the \term{values} passed from that \term{form};
  391. otherwise, the \term{values} returned by the \param{result-form}
  392. or \nil\ if there is no \param{result-form}.
  393. \label Description::
  394. %% 7.8.3 6
  395. \macref{dolist} iterates over the elements of a \term{list}.
  396. The body of \macref{dolist} is like a \specref{tagbody}.
  397. It consists of a series of \param{tags} and \param{statements}.
  398. \macref{dolist}
  399. evaluates \param{list-form},
  400. which should produce a \term{list}. It then executes the body
  401. once for each element in the \term{list}, in the order in which the
  402. \param{tags} and \param{statements} occur, with
  403. \param{var} bound to the element.
  404. Then \param{result-form}
  405. is evaluated.
  406. \param{tags} label
  407. \param{statements}.
  408. At the time \param{result-form} is processed,
  409. \param{var} is bound to \nil.
  410. An \term{implicit block}
  411. %In effect, a \specref{block}
  412. named \nil\ surrounds \macref{dolist}.
  413. %% Made symmetric with the wording in DOTIMES. -kmp 26-May-93
  414. % %% 7.8.3 5
  415. % %% 7.8.3 8
  416. % \macref{return} may be used to terminate the loop
  417. % and return a specified value.
  418. \macref{return} may be used to terminate the loop immediately without
  419. performing any further iterations, returning zero or more \term{values}.
  420. %% Changed per Loosemore #28, first public review
  421. % For \macref{dolist}, the
  422. % \term{scope} of the name binding does not include any
  423. % initial value form, but the stepper and optional result forms are included.
  424. The \term{scope} of the binding of \param{var}
  425. does not include the \param{list-form},
  426. but the \param{result-form} is included.
  427. %% Added per Loosemore #27, first public review
  428. It is \term{implementation-dependent} whether \macref{dolist}
  429. \term{establishes} a new \term{binding} of \param{var} on each iteration
  430. or whether it \term{establishes} a binding for \param{var} once at the
  431. beginning and then \param{assigns} it on any subsequent iterations.
  432. \label Examples::
  433. %% 7.8.3 7
  434. \code
  435. (setq temp-two '()) \EV NIL
  436. (dolist (temp-one '(1 2 3 4) temp-two) (push temp-one temp-two)) \EV (4 3 2 1)
  437. (setq temp-two 0) \EV 0
  438. (dolist (temp-one '(1 2 3 4)) (incf temp-two)) \EV NIL
  439. temp-two \EV 4
  440. (dolist (x '(a b c d)) (prin1 x) (princ " "))
  441. \OUT A B C D
  442. \EV NIL
  443. \endcode
  444. \label Side Effects:\None.
  445. \label Affected By:\None.
  446. \label Exceptional Situations:\None.
  447. \label See Also::
  448. \macref{do},
  449. \macref{dotimes},
  450. \specref{tagbody},
  451. \issue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  452. {\secref\TraversalRules}
  453. \endissue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  454. \label Notes::
  455. \specref{go} may be used within the body of \macref{dolist}
  456. to transfer control to a statement labeled by a \param{tag}.
  457. \endissue{DECLS-AND-DOC}
  458. \endcom
  459. %%% ========== LOOP
  460. \begincom{loop}\ftype{Macro}
  461. \issue{LOOP-SYNTAX-OVERHAUL:REPAIR}
  462. \label Syntax::
  463. The ``simple'' \macref{loop} \term{form}:
  464. \DefmacWithValues loop {\starparam{compound-form}} {\starparam{result}}
  465. The ``extended'' \macref{loop} \term{form}:
  466. \DefmacWithValues loop {\brac{\down{name-clause}}
  467. \stardown{variable-clause}
  468. \stardown{main-clause}} {\starparam{result}}
  469. \auxbnf{name-clause}{\loopref{named} \param{name}}
  470. \auxbnf{variable-clause}{\down{with-clause} | \down{initial-final} | \down{for-as-clause}}
  471. \auxbnf{with-clause}%
  472. {\loopref{with} \param{var1} \brac{\param{type-spec}} \brac{$=$ \param{form1}}
  473. \star{\curly{\loopref{and} \param{var2} \brac{\param{type-spec}}
  474. \brac{$=$ \param{form2}}}}}
  475. \auxbnf{main-clause}%
  476. {\down{unconditional} |
  477. \down{accumulation} |
  478. \down{conditional} |
  479. \down{termination-test} |
  480. \down{initial-final}}
  481. \auxbnf{initial-final}%
  482. {\loopref{initially} \plusparam{compound-form} | \loopref{finally} \plusparam{compound-form}}
  483. \auxbnf{unconditional}%
  484. {\curly{\loopref{do} | \loopref{doing}} \plusparam{compound-form} |
  485. \loopref{return} \curly{\param{form} | \loopref{it}}}
  486. \auxbnf{accumulation}{\down{list-accumulation} | \down{numeric-accumulation}}
  487. \auxbnf{list-accumulation}%
  488. {\curly{\loopref{collect} | \loopref{collecting} |
  489. \loopref{append} | \loopref{appending} |
  490. \loopref{nconc} | \loopref{nconcing}} \curly{\param{form} | \loopref{it}} \CR
  491. \brac{\loopref{into} \param{simple-var}}}
  492. \auxbnf{numeric-accumulation}%
  493. {\curly{\loopref{count} | \loopref{counting} |
  494. \loopref{sum} | \loopref{summing} | \CR
  495. \xcurly\loopref{maximize} | \loopref{maximizing} |
  496. \loopref{minimize} | \loopref{minimizing}} \curly{\param{form} | \loopref{it}} \CR
  497. \brac{\loopref{into} \param{simple-var}} \brac{\param{type-spec}}}
  498. \auxbnf{conditional}%
  499. {\curly{\loopref{if} | \loopref{when} | \loopref{unless}} \param{form}
  500. \down{selectable-clause} \star{\curly{\loopref{and} \down{selectable-clause}}} \CR
  501. \brac{\loopref{else}
  502. \down{selectable-clause} \star{\curly{\loopref{and} \down{selectable-clause}}}} \CR
  503. \brac{\loopref{end}}}
  504. %% I flushed the clause/clause1/clause2 distinction in the next thing, since it's not used
  505. %% anywhere for reference, and it's not appropriate to the bnf style.
  506. %% However, I'm concerned about the loss of IT here due to LOOP-MISCELLANEOUS-REPAIRS.
  507. %% -kmp 4-May-93
  508. \auxbnf{selectable-clause}{\down{unconditional} | \down{accumulation} | \down{conditional}}
  509. \auxbnf{termination-test}%
  510. {\loopref{while} \param{form} |
  511. \loopref{until} \param{form} |
  512. \loopref{repeat} \param{form} |
  513. \loopref{always} \param{form} |
  514. \loopref{never} \param{form} |
  515. \loopref{thereis} \param{form}}
  516. \auxbnf{for-as-clause}%
  517. {\curly{\loopref{for} | \loopref{as}} \down{for-as-subclause}
  518. \star{\curly{\loopref{and} \down{for-as-subclause}}}}
  519. \auxbnf{for-as-subclause}%
  520. {\down{for-as-arithmetic} |
  521. \down{for-as-in-list} |
  522. \down{for-as-on-list} |
  523. \down{for-as-equals-then} |\CR
  524. \down{for-as-across} |
  525. \down{for-as-hash} |
  526. \down{for-as-package}}
  527. \auxbnf{for-as-arithmetic}{\param{var} \brac{\param{type-spec}}
  528. \down{for-as-arithmetic-subclause}}
  529. \auxbnf{for-as-arithmetic-subclause}%
  530. {\down{arithmetic-up} | \down{arithmetic-downto} | \down{arithmetic-downfrom}}
  531. \auxbnf{arithmetic-up}%
  532. {\begininterleave\curly{\loopref{from} | \loopref{upfrom}} \param{form1} |
  533. \extrainterleave\curly{\loopref{to} | \loopref{upto} | \loopref{below}} \param{form2} |
  534. \extrainterleave\loopref{by} \param{form3}\endinterleave\prevplus}
  535. \auxbnf{arithmetic-downto}%
  536. {\begininterleave\one{\curly{\loopref{from} \param{form1}}} |
  537. \extrainterleave\one{\curly{\curly{\loopref{downto} | \loopref{above}} \param{form2}}} |
  538. \extrainterleave\loopref{by} \param{form3}\endinterleave}
  539. \auxbnf{arithmetic-downfrom}%
  540. {\begininterleave\one{\curly{\loopref{downfrom} \param{form1}}} |
  541. \extrainterleave\curly{\loopref{to} | \loopref{downto} | \loopref{above}} \param{form2} |
  542. \extrainterleave\loopref{by} \param{form3}\endinterleave}
  543. \auxbnf{for-as-in-list}%
  544. {\param{var} \brac{\param{type-spec}}
  545. \loopref{in} \param{form1} \brac{\loopref{by} \param{step-fun}}}
  546. \auxbnf{for-as-on-list}%
  547. {\param{var} \brac{\param{type-spec}}
  548. \loopref{on} \param{form1} \brac{\loopref{by} \param{step-fun}}}
  549. \auxbnf{for-as-equals-then}%
  550. {\param{var} \brac{\param{type-spec}}
  551. $=$ \param{form1} \brac{\loopref{then} \param{form2}}}
  552. \auxbnf{for-as-across}%
  553. {\param{var} \brac{\param{type-spec}}
  554. \loopref{across} \param{vector}}
  555. \auxbnf{for-as-hash}%
  556. {\param{var} \brac{\param{type-spec}}
  557. \loopref{being} \curly{\loopref{each} | \loopref{the}} \CR
  558. \lcurly\curly{\loopref{hash-key} | \loopref{hash-keys}}
  559. \curly{\loopref{in} | \loopref{of}} \param{hash-table} \CR
  560. \xcurly\brac{\loopref{using} \paren{\loopref{hash-value} \param{other-var}}} | \CR
  561. \xcurly\curly{\loopref{hash-value} | \loopref{hash-values}}
  562. \curly{\loopref{in} | \loopref{of}} \param{hash-table} \CR
  563. \xcurly\brac{\loopref{using} \paren{\loopref{hash-key} \param{other-var}}}\rcurly}
  564. \auxbnf{for-as-package}%
  565. {\param{var} \brac{\param{type-spec}}
  566. \loopref{being} \curly{\loopref{each} | \loopref{the}} \CR
  567. \lcurly\loopref{symbol} | \loopref{symbols} |\CR
  568. \xcurly\loopref{present-symbol} | \loopref{present-symbols} |\CR
  569. \xcurly\loopref{external-symbol} | \loopref{external-symbols}\rcurly \CR
  570. \brac{\curly{\loopref{in} | \loopref{of}} \param{package}}}
  571. \auxbnf{type-spec}{\down{simple-type-spec} | \down{destructured-type-spec}}
  572. \auxbnf{simple-type-spec}%
  573. {\declref{fixnum} | \declref{float} | \declref{t} | \declref{nil}}
  574. \auxbnf{destructured-type-spec}{\loopref{of-type} \param{d-type-spec}}
  575. \auxbnf{d-type-spec}%
  576. {\param{type-specifier} | \f{(\param{d-type-spec} . \param{d-type-spec})}}
  577. \auxbnf{var}{\down{d-var-spec}}
  578. \auxbnf{var1}{\down{d-var-spec}}
  579. \auxbnf{var2}{\down{d-var-spec}}
  580. \auxbnf{other-var}{\down{d-var-spec}}
  581. \auxbnf{d-var-spec}{\param{simple-var} |
  582. \nil\ |
  583. \paren{\down{d-var-spec} \f{.} \down{d-var-spec}}}
  584. \label Arguments and Values::
  585. \param{compound-form}---a \term{compound form}.
  586. \param{name}---a \term{symbol}.
  587. %% Per X3J13. -kmp 05-Oct-93
  588. % \param{var}, \param{var1}, \param{var2}, \param{other-var}---a \term{symbol}
  589. % (a \term{variable} \term{name}).
  590. \param{simple-var}---a \term{symbol} (a \term{variable} name).
  591. \param{form}, \param{form1}, \param{form2}, \param{form3}---a \term{form}.
  592. \issue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  593. \param{step-fun}---a \term{form} that evaluates to a \term{function} of one \term{argument}.
  594. \endissue{LOOP-MISCELLANEOUS-REPAIRS:FIX}
  595. \param{vector}---a \term{form} that evaluates to a \term{vector}.
  596. \param{hash-table}---a \term{form} that evaluates to a \term{hash table}.
  597. \param{package}---a \term{form} that evaluates to a \term{package designator}.
  598. \param{type-specifier}---a \term{type specifier}.
  599. This might be either an \term{atomic type specifier} or a \term{compound type specifier},
  600. which introduces some additional complications to proper parsing in the face of
  601. destructuring; for further information, \seesection\DestructuringLOOPVars.
  602. \param{result}---an \term{object}.
  603. \endissue{LOOP-SYNTAX-OVERHAUL:REPAIR}
  604. \label Description::
  605. %!!! This should be elaborated slightly here.
  606. For details, \seesection\LoopFacility.
  607. \label Examples::
  608. \code
  609. ;; An example of the simple form of LOOP.
  610. (defun sqrt-advisor ()
  611. (loop (format t "~&Number: ")
  612. (let ((n (parse-integer (read-line) :junk-allowed t)))
  613. (when (not n) (return))
  614. (format t "~&The square root of ~D is ~D.~%" n (sqrt n)))))
  615. \EV SQRT-ADVISOR
  616. (sqrt-advisor)
  617. \OUT Number: \IN{5\CRLF}
  618. \OUT The square root of 5 is 2.236068.
  619. \OUT Number: \IN{4\CRLF}
  620. \OUT The square root of 4 is 2.
  621. \OUT Number: \IN{done\CRLF}
  622. \EV NIL
  623. ;; An example of the extended form of LOOP.
  624. (defun square-advisor ()
  625. (loop as n = (progn (format t "~&Number: ")
  626. (parse-integer (read-line) :junk-allowed t))
  627. while n
  628. do (format t "~&The square of ~D is ~D.~%" n (* n n))))
  629. \EV SQUARE-ADVISOR
  630. (square-advisor)
  631. \OUT Number: \IN{4\CRLF}
  632. \OUT The square of 4 is 16.
  633. \OUT Number: \IN{23\CRLF}
  634. \OUT The square of 23 is 529.
  635. \OUT Number: \IN{done\CRLF}
  636. \EV NIL
  637. ;; Another example of the extended form of LOOP.
  638. (loop for n from 1 to 10
  639. when (oddp n)
  640. collect n)
  641. \EV (1 3 5 7 9)
  642. \endcode
  643. \label Affected By:\None.
  644. \label Exceptional Situations:\None.
  645. %!!! Barmar: The description mentions several places where PROGRAM-ERROR is signaled.
  646. \label See Also::
  647. \macref{do}, \macref{dolist}, \macref{dotimes},
  648. \macref{return}, \specref{go}, \specref{throw},
  649. {\secref\DestructuringLOOPVars}
  650. \label Notes::
  651. %% Per X3J13. -kmp 05-Oct-93
  652. % The simple form of \macref{loop} is related to the extended form
  653. % in the following way:
  654. Except that \macref{loop-finish} cannot be used within a simple \funref{loop} \term{form},
  655. a simple \macref{loop} \term{form} is related to an extended \macref{loop} \term{form}
  656. in the following way:
  657. \code
  658. (loop \starparam{compound-form}) \EQ (loop do \starparam{compound-form})
  659. \endcode
  660. \endcom%{loop}
  661. %%% ========== LOOP-FINISH
  662. \begincom{loop-finish}\ftype{Local Macro}
  663. \label Syntax::
  664. \DefmacNoReturn loop-finish {\noargs}
  665. \label Description::
  666. \Themacro{loop-finish} can be used lexically within
  667. %% KMP changed it to say extended
  668. %a
  669. an extended
  670. \macref{loop} \term{form}
  671. to terminate that \term{form} ``normally.''
  672. That is, it transfers control to the loop epilogue
  673. %% Added per X3J13 -kmp 05-Oct-93
  674. of the lexically innermost extended \macref{loop} \term{form}.
  675. This permits execution of any \macref{finally} clause (for effect)
  676. and
  677. %% Per X3J13. -kmp 05-Oct-93
  678. %then returns
  679. the return of
  680. any accumulated result.
  681. %% Clarified above per X3J13. -kmp 05-Oct-93
  682. % %% KMP added this because it was implicitly vague and portable programs
  683. % %% must know this fact to avoid utter lossage. It affects nesting behavior,
  684. % %% as explained in a note farther down. -kmp 1-Aug-93
  685. % It is \term{implementation-defined} whether \macref{loop-finish}
  686. % can be used in a simple \macref{loop} form.
  687. \label Examples::
  688. \code
  689. ;; Terminate the loop, but return the accumulated count.
  690. (loop for i in '(1 2 3 stop-here 4 5 6)
  691. when (symbolp i) do (loop-finish)
  692. count i)
  693. \EV 3
  694. ;; The preceding loop is equivalent to:
  695. (loop for i in '(1 2 3 stop-here 4 5 6)
  696. until (symbolp i)
  697. count i)
  698. \EV 3
  699. ;; While LOOP-FINISH can be used can be used in a variety of
  700. ;; situations it is really most needed in a situation where a need
  701. ;; to exit is detected at other than the loop's `top level'
  702. ;; (where UNTIL or WHEN often work just as well), or where some
  703. ;; computation must occur between the point where a need to exit is
  704. ;; detected and the point where the exit actually occurs. For example:
  705. (defun tokenize-sentence (string)
  706. (macrolet ((add-word (wvar svar)
  707. `(when ,wvar
  708. (push (coerce (nreverse ,wvar) 'string) ,svar)
  709. (setq ,wvar nil))))
  710. (loop with word = '() and sentence = '() and endpos = nil
  711. for i below (length string)
  712. do (let ((char (aref string i)))
  713. (case char
  714. (#\\Space (add-word word sentence))
  715. (#\\. (setq endpos (1+ i)) (loop-finish))
  716. (otherwise (push char word))))
  717. finally (add-word word sentence)
  718. (return (values (nreverse sentence) endpos)))))
  719. \EV TOKENIZE-SENTENCE
  720. (tokenize-sentence "this is a sentence. this is another sentence.")
  721. \EV ("this" "is" "a" "sentence"), 19
  722. (tokenize-sentence "this is a sentence")
  723. \EV ("this" "is" "a" "sentence"), NIL
  724. \endcode
  725. \label Side Effects::
  726. Transfers control.
  727. \label Affected By:\None.
  728. \label Exceptional Situations::
  729. \issue{LEXICAL-CONSTRUCT-GLOBAL-DEFINITION:UNDEFINED}
  730. Whether or not \macref{loop-finish} is \term{fbound} in the
  731. \term{global environment} is \term{implementation-dependent};
  732. however, the restrictions on redefinition and \term{shadowing} of
  733. \macref{loop-finish} are the same as for \term{symbols} in \thepackage{common-lisp}
  734. which are \term{fbound} in the \term{global environment}.
  735. The consequences of attempting to use \macref{loop-finish} outside
  736. of \macref{loop} are undefined.
  737. \endissue{LEXICAL-CONSTRUCT-GLOBAL-DEFINITION:UNDEFINED}
  738. \label See Also::
  739. \macref{loop},
  740. {\secref\LoopFacility}
  741. %% Semantics clarified per X3J13. -kmp 05-Oct-93
  742. \label Notes::
  743. % %% This part added to make it clear to users that this is a portability pitfall.
  744. %
  745. % Because it is \term{implementation-defined} whether \macref{loop-finish}
  746. % will work in a simple \macref{loop} form, it follows an obvious consequence
  747. % that a form such as the following is not portable:
  748. %
  749. % \code
  750. % (loop (loop-finish))
  751. % \endcode
  752. %
  753. % However, what may be less obvious is that there are
  754. % implications on nesting behavior as well. In particular, it is recommended
  755. % that user code not mix styles (simple and extended) of \macref{loop} forms
  756. % when \macref{loop-finish} will be involved. For example, the following form
  757. % is also not portable:
  758. %
  759. % \code
  760. % (loop do (loop (loop-finish)))
  761. % \endcode
  762. \endcom%{loop-finish}