09-flowofcontrol.lpt 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. PSL Manual 7 February 1983 Flow Of Control
  2. section 9.0 page 9.1
  3. CHAPTER 9 CHAPTER 9 CHAPTER 9
  4. FLOW OF CONTROL FLOW OF CONTROL FLOW OF CONTROL
  5. 9.1. Introduction . . . . . . . . . . . . . . . 9.1
  6. 9.2. Conditionals . . . . . . . . . . . . . . . 9.1
  7. 9.2.1. Conds and Ifs. . . . . . . . . . . . . 9.1
  8. 9.2.2. The Case Statement . . . . . . . . . . . 9.3
  9. 9.3. Sequencing Evaluation . . . . . . . . . . . . 9.4
  10. 9.4. Iteration . . . . . . . . . . . . . . . . 9.7
  11. 9.4.1. For . . . . . . . . . . . . . . . . 9.8
  12. 9.4.2. Mapping Functions . . . . . . . . . . . 9.13
  13. 9.4.3. Do . . . . . . . . . . . . . . . . 9.16
  14. 9.5. Non-Local Exits . . . . . . . . . . . . . . 9.18
  15. 9.1. Introduction 9.1. Introduction 9.1. Introduction
  16. Most of the constructs presented in this Chapter have a special syntax in
  17. RLISP. This syntax is presented along with the definitions of the
  18. underlying functions. Many of the examples are presented using this
  19. special RLISP syntax as well as LISP.
  20. 9.2. Conditionals 9.2. Conditionals 9.2. Conditionals
  21. 9.2.1. Conds and Ifs 9.2.1. Conds and Ifs 9.2.1. Conds and Ifs
  22. Cond Cond _ ____ ____ ___ ____ ________ _____ (Cond [U:form-list]): any open-compiled, fexpr
  23. Cond If Cond If The LISP function Cond corresponds to the If statement of most
  24. If If programming languages. In RLISP this is simply the familiar If
  25. Then Else Then Else ... Then ... Else construct. For example:
  26. _________ ______ IF predicate THEN action1
  27. ______ ELSE action2
  28. _________ ______ ==> (COND (predicate action1)
  29. ______ (T action2))
  30. ______ _________ Action1 is evaluated if the predicate has a non-NIL evaluation;
  31. Else ______ Else otherwise, action2 is evaluated. Dangling Elses are resolved in
  32. Then Then the ALGOL manner by pairing them with the nearest preceding Then.
  33. For example: Flow Of Control 7 February 1983 PSL Manual
  34. page 9.2 section 9.2
  35. IF F(X) THEN
  36. IF G(Y) THEN PRINT(X)
  37. ELSE PRINT(Y);
  38. is equivalent to
  39. IF F(X) THEN
  40. << IF G(Y) THEN PRINT(X)
  41. ELSE PRINT(Y) >>;
  42. Note that if F(X) is NIL, nothing is printed.
  43. Taken simply as a function, without RLISP syntax, the arguments
  44. Cond Cond to Cond have the form:
  45. _________ ______ ______ (COND (predicate action action ...)
  46. _________ ______ ______ (predicate action action ...)
  47. ...
  48. _________ ______ ______ (predicate action action ...) )
  49. The predicates are evaluated in the order of their appearance
  50. until a non-NIL value is encountered. The corresponding actions
  51. are evaluated and the value of the last becomes the value of the
  52. Cond Else Cond Else Cond. The dangling Else example above is:
  53. (COND ((F X) (COND ((G X) (PRINT X))
  54. ( T (PRINT Y)) ) ))
  55. Go Return Go Return The actions may also contain the special functions Go, Return,
  56. Exit Next Exit Next Exit, and Next, subject to the constraints on placement of these
  57. Cond Cond functions given in Section 9.3. In these cases, Cond does not
  58. have a defined value, but rather an effect. If no predicate is
  59. Cond Cond non-NIL, the value of Cond is NIL.
  60. The following MACROs are defined in the USEFUL module for convenience,
  61. and are mostly used from LISP syntax:
  62. If If _ ____ __ ____ _ ____ ___ _____ (If E:form S0:form [S:form]): any macro
  63. If Cond If Cond If is a macro to simplify the writing of a common form of Cond in
  64. which there are only two clauses and the antecedent of the second
  65. is T. It cannot be used in RLISP syntax.
  66. (IF E S0 S1...Sn)
  67. __ _ The then-clause S0 is evaluated if and only if the test E is
  68. _ non-NIL, otherwise the else-clauses Si are evaluated, and the
  69. last returned. There may be no else-clauses.
  70. Related macros for common COND forms are WHEN and UNLESS. PSL Manual 7 February 1983 Flow Of Control
  71. section 9.2 page 9.3
  72. When When _ ____ _ ____ ___ _____ (When E:form [S:form]): any macro
  73. (WHEN E S1 S2 ... Sn)
  74. evaluates the Si and returns the value of Sn if and only if the
  75. When _ When test E is non-NIL. Otherwise When returns NIL.
  76. Unless Unless _ ____ _ ____ ___ _____ (Unless E:form [U:form]): any macro
  77. (UNLESS E S1 S2 ... Sn)
  78. _ Evaluates the Si if and only if the test E is NIL. It is
  79. equivalent to
  80. (WHEN (NOT E) S1 S2 ... Sn)
  81. And Or And Or While And and Or are primarily of interest as Boolean connectives, they
  82. are often used in LISP as conditionals. For example,
  83. (AND (FOO) (BAR) (BAZ))
  84. has the same result as
  85. (COND ((FOO) (COND ((BAR) (BAZ)))))
  86. See Section 4.2.3.
  87. 9.2.2. The Case Statement 9.2.2. The Case Statement 9.2.2. The Case Statement
  88. PSL provides a numeric case statement, that is compiled quite
  89. efficiently; some effort is made to examine special cases (compact vs. non
  90. compact sets of cases, short vs. long sets of cases, etc.). It has mostly
  91. been used in SYSLISP mode, but can also be used from LISP mode provided
  92. that case-tags are numeric. There is also an FEXPR, CASE, for the
  93. interpreter.
  94. The RLISP syntax is:
  95. Case-Statement ::= CASE expr OF case-list END
  96. Case-list ::= Case-expr [; Case-list ]
  97. Case-expr ::= Tag-expr : expr
  98. tag-expr ::= DEFAULT | OTHERWISE |
  99. tag | tag, tag ... tag |
  100. tag TO tag
  101. Tag ::= Integer | Wconst-Integer Flow Of Control 7 February 1983 PSL Manual
  102. page 9.4 section 9.2
  103. For example:
  104. CASE i OF
  105. 1: Print("First");
  106. 2,3: Print("Second");
  107. 4 to 10: Print("Third");
  108. Default: Print("Fourth");
  109. END
  110. The RLISP syntax parses into the following LISP form:
  111. Case Case _ ____ _ ____ ____ ___ ____ ________ _____ (Case I:form [U:case-list]): any open-compiled, fexpr
  112. _ _______ I is meant to evaluate to an integer, and is used as a selector
  113. _ amongst the various Us. Each case-list has the form (case-expr
  114. form) where case-expr has the form:
  115. NIL -> default case
  116. (I1 I2 ... In) -> where each Ik is an integer or
  117. (RANGE low high)
  118. The above example becomes:
  119. (CASE i ((1) (Print "First"))
  120. ((2 3) (Print "Second"))
  121. (((Range 4 10)) (Print "Third"))
  122. ( NIL (Print "Fourth")))
  123. [??? Perhaps we should move SELECTQ (and define a SELECT) from the [??? Perhaps we should move SELECTQ (and define a SELECT) from the [??? Perhaps we should move SELECTQ (and define a SELECT) from the
  124. COMMON module to the basic system ???] COMMON module to the basic system ???] COMMON module to the basic system ???]
  125. .
  126. 9.3. Sequencing Evaluation 9.3. Sequencing Evaluation 9.3. Sequencing Evaluation
  127. These functions provide for explicit control sequencing, and the
  128. definition of blocks altering the scope of local variables.
  129. ProgN ProgN _ ____ ___ ____ ________ _____ (ProgN [U:form]): any open-compiled, fexpr
  130. _ U is a set of expressions which are executed sequentially. The
  131. value returned is the value of the last expression. PSL Manual 7 February 1983 Flow Of Control
  132. section 9.3 page 9.5
  133. Prog2 Prog2 _ ____ _ ____ ___ ____ ________ ____ (Prog2 A:form B:form): any open-compiled, expr
  134. _ Returns the value of B (the second argument).
  135. [??? Redefine prog2 to take N arguments, return second. ???] [??? Redefine prog2 to take N arguments, return second. ???] [??? Redefine prog2 to take N arguments, return second. ???]
  136. Prog1 Prog1 _ ____ ___ _____ (Prog1 [U:form]): any macro
  137. Prog1 Prog1 Prog1 is a function defined in the USEFUL package; to use it,
  138. Prog1 Prog1 type (LOAD USEFUL). Prog1 evaluates its arguments in order, like
  139. ProgN ProgN ProgN, but returns the value of the first.
  140. Prog Prog ____ __ ____ _______ __ ____ ___ ____ ________ _____ (Prog VARS:id-list [PROGRAM:{id,form}]): any open-compiled, fexpr
  141. Prog ____ ____ __ Prog VARS is a list of ids which are considered FLUID if the Prog is
  142. interpreted and LOCAL if compiled (see the "Variables and
  143. Prog Prog Bindings" Section, 10.2). The Prog's variables are allocated
  144. Prog Prog space if the Prog form is applied, and are deallocated if the
  145. Prog Prog Prog Prog Prog is exited. Prog variables are initialized to NIL. The
  146. _______ PROGRAM is a set of expressions to be evaluated in order of their
  147. Prog Prog __________ appearance in the Prog function. identifiers appearing in the
  148. _______ top level of the PROGRAM are labels which can be referred to by
  149. Go Prog Go Prog Go. The value returned by the Prog function is determined by a
  150. Return Prog Return Prog Return function or NIL if the Prog "falls through".
  151. There are restrictions as to where a number of control functions, such as
  152. Go Return Go Return Go and Return, may be placed. This is so that they may have only locally
  153. determinable effects. Unlike most LISPs, which make this restriction only
  154. in compiled code, PSL enforces this restriction uniformly in both compiled
  155. and interpreted code. Not only does this help keep the semantics of
  156. compiled and interpreted code the same, but we believe it leads to more
  157. readable programs. For cases in which a non-local exit is truly required,
  158. Catch Throw Catch Throw there are the functions Catch and Throw, described in Section 9.5.
  159. Go Return Exit Next Go Return Exit Next The functions so restricted are Go, Return, Exit, and Next. They must be
  160. placed at top-level within the surrounding control structure to which they
  161. Prog Return Prog Return refer (e.g. the Prog which Return causes to be terminated), or nested
  162. within only selected functions. The functions in which they may be nested
  163. (to arbitrary depth) are:
  164. ProgN ProgN - ProgN (compound statement)
  165. Cond Cond - actions of Conds (if then else)
  166. Case Case - actions in Cases
  167. Go Go _____ __ ____ ________ ____ ________ _____ (Go LABEL:id): None Returned open-compiled, fexpr
  168. Go Prog Go Prog Go alters the normal flow of control within a Prog function. The
  169. Prog Prog next statement of a Prog function to be evaluated is immediately
  170. Go _____ Go preceded by LABEL. A Go may appear only in the following
  171. situations: Flow Of Control 7 February 1983 PSL Manual
  172. page 9.6 section 9.3
  173. Prog Prog _____ a. At the top level of a Prog referring to a LABEL that also
  174. Prog Prog appears at the top level of the same Prog.
  175. Cond Cond b. As the action of a Cond item
  176. Prog Prog i. appearing on the top level of a Prog.
  177. Cond Cond ii. which appears as the action of a Cond item to any
  178. level.
  179. ProgN ProgN c. As the last statement of a ProgN
  180. Prog Prog i. which appears at the top level of a Prog or in a
  181. ProgN Cond ProgN Cond ProgN appearing in the action of a Cond to any level
  182. subject to the restrictions of b.i, or b.ii.
  183. ProgN Cond ProgN ProgN Cond ProgN ii. within a ProgN or as the action of a Cond in a ProgN
  184. to any level subject to the restrictions of b.i,
  185. b.ii, and c.i.
  186. Prog _____ Prog If LABEL does not appear at the top level of the Prog in which
  187. Go Go the Go appears, an error occurs:
  188. ***** LABEL is not a label within the current scope
  189. Go Go If the Go has been placed in a position not defined by rules a-c,
  190. another error is detected:
  191. ***** Illegal use of GO To LABEL
  192. Return Return _ ____ ____ ________ ____ ________ ____ (Return U:form): None Returned open-compiled, expr
  193. Prog Return Prog Prog Return Prog Within a Prog, Return terminates the evaluation of a Prog and
  194. Prog _ Prog returns U as the value of the Prog. The restrictions on the
  195. Return Go Return Go placement of Return are exactly those of Go. Improper placement
  196. Return Return of Return results in the error:
  197. ***** Illegal use of RETURN
  198. 9.4. Iteration 9.4. Iteration 9.4. Iteration
  199. While While _ ____ _ ____ ___ _____ (While E:form [S:form]): NIL macro
  200. This is the most commonly used construct for indefinite iteration
  201. _ _ in LISP. E is evaluated; if non-NIL, the S's are evaluated from
  202. _ left to right and then the process is repeated. If E evaluates
  203. While Exit While Exit to NIL the While returns NIL. Exit may be used to terminate the PSL Manual 7 February 1983 Flow Of Control
  204. section 9.4 page 9.7
  205. While Next While Next While from within the body and to return a value. Next may be
  206. used to terminate the current iteration. In RLISP syntax this is
  207. While Do While Do While ... Do ... . Note that in RLISP syntax there may be only a
  208. Do ProgN Do ProgN single expression after the Do; however, it may be a ProgN
  209. delimited by <<...>>. That is,
  210. (While E S1 S2)
  211. should be written in RLISP as
  212. While E do <<S1; S2>>;
  213. Repeat Repeat _ ____ _ ____ ___ _____ (Repeat E:form [S:form]): NIL macro
  214. _ _ The S's are evaluated left to right, and then E is evaluated.
  215. Repeat _ Repeat This is repeated until the value of E is NIL, if Repeat returns
  216. Next Exit Next Exit _ NIL. Next and Exit may be used in the S's branch to the next
  217. Repeat Repeat iteration of a Repeat or to terminate one and possibly return a
  218. Go Return Go Return _ value. Go, and Return may appear in the S's. The RLISP syntax
  219. Repeat Repeat Until While Repeat Repeat Until While for Repeat is Repeat Until. Like While, RLISP syntax only allows
  220. _ a single S, so
  221. (REPEAT E S1 S2)
  222. should be written in RLISP as
  223. REPEAT << S1; S2 >> UNTIL E;
  224. [??? maybe do REPEAT S1 ... Sn E ???] [??? maybe do REPEAT S1 ... Sn E ???] [??? maybe do REPEAT S1 ... Sn E ???]
  225. Next Next ____ ________ ____ ________ __________ _____ (Next ): None Returned open-compiled, restricted, macro
  226. This terminates the current iteration of the most closely
  227. While Repeat While Repeat surrounding While or Repeat, and causes the next to commence.
  228. See the note in Section 9.3 about the lexical restrictions on
  229. GO GO placement of this construct, which is essentially a GO to a
  230. special label placed at the front of a loop construct.
  231. Exit Exit _ ____ ____ ________ ____ ________ __________ _____ (Exit [U:form]): None Returned open-compiled,restricted, macro
  232. _ The U's are evaluated left to right, the most closely surrounding
  233. While Repeat While Repeat _ While or Repeat is terminated, and the value of the last U is
  234. returned. With no arguments, NIL is returned. See the note in
  235. Section 9.3 about the lexical restrictions on placement of this
  236. Return Return construct, which is essentially a Return.
  237. While Repeat Prog Next Exit While Repeat Prog Next Exit While and Repeat each macro expand into a Prog; Next and Exit are macro
  238. Go Return Prog Go Return Prog expanded into a Go and a Return respectively to this Prog. Thus using a
  239. Next Exit Prog While Repeat Next Exit Prog While Repeat Next or an Exit within a Prog within a While or Repeat will result only in Flow Of Control 7 February 1983 PSL Manual
  240. page 9.8 section 9.4
  241. Prog Prog an exit of the internal Prog. In RLISP be careful to use
  242. WHILE E DO << S1;...;EXIT(1);...;Sn>>
  243. not
  244. WHILE E DO BEGIN S1;...;EXIT(1);...;Sn;END;
  245. 9.4.1. For 9.4.1. For 9.4.1. For
  246. For For A simple For construct is available in the basic PSL system and RLISP; an
  247. extended form can obtained by loading USEFUL. It is planned to make the
  248. extended form the version available in the basic system, combining all the
  249. FOR ForEach For FOR ForEach For features of FOR and ForEach. The basic PSL For provides only the (FROM ..)
  250. ForEach ForEach iterator, and (DO ...) action clause, and uses the ForEach construct for
  251. some of the (IN ...) and (ON ...) iterators. Most PSL syntax users should
  252. For For use the full For construct.
  253. For For _ ____ ___ _____ (For [S:form]): any macro
  254. For For The arguments to For are clauses; each clause is itself a list of
  255. a keyword and one or more arguments. The clauses may introduce
  256. local variables, specify return values and when the iteration
  257. should cease, have side-effects, and so on. Before going
  258. further, it is probably best to give some examples.
  259. (FOR (FROM I 1 10 2) (DO (PRINT I)))
  260. Prints the numbers 1 3 5 7 9
  261. (FOR (IN U '(A B C)) (DO (PRINT U)))
  262. Prints the letters A B C
  263. (FOR (ON U '(A B C)) (DO (PRINT U)))
  264. Prints the lists (A B C) (B C) and (C)
  265. Finally, the function
  266. (DE ZIP (X Y)
  267. (FOR (IN U X) (IN V Y)
  268. (COLLECT (LIST U V))))
  269. produces a list of 2 element lists, each consisting of the the
  270. corresponding elements of the three lists X, Y and Z. For
  271. example,
  272. (ZIP '(1 2 3 4) '(A B C) )
  273. produces PSL Manual 7 February 1983 Flow Of Control
  274. section 9.4 page 9.9
  275. ((1 a)(2 b)(3 c))
  276. The iteration terminates as soon as one of the (IN ..) clauses is
  277. exhausted.
  278. Note that the (IN ... ), (ON ...) and (FROM ...) clauses
  279. introduce local variables U, V or I, that are referred to in the
  280. action clause.
  281. All the possible clauses are described below. The first few
  282. introduce iteration variables. Most of these also give some
  283. means of indicating when iteration should cease. For example, if
  284. In ____ In a list being mapped over by an In clause is exhausted, iteration
  285. For For must cease. If several such clauses are given in For expression,
  286. iteration ceases when one of the clauses indicates it should,
  287. whether or not the other clauses indicate that it should cease.
  288. (IN V1 V2)
  289. ____ assigns the variable V1 successive elements of the list
  290. V2.
  291. This may take an additional, optional argument: a
  292. function to be applied to the extracted element or
  293. sublist before it is assigned to the variable. The
  294. following returns the sum of the lengths of all the
  295. elements of L.
  296. [??? Rather a kludge -- not sure why this is here. [??? Rather a kludge -- not sure why this is here. [??? Rather a kludge -- not sure why this is here.
  297. Perhaps it should come out again. ???] Perhaps it should come out again. ???] Perhaps it should come out again. ???]
  298. (DE LENGTHS (L)
  299. (FOR (IN N L LENGTH)
  300. (COLLECT (LIST N N)))
  301. is the same as
  302. (DE LENGTHS (L)
  303. (FOR (IN N L)
  304. (COLLECT
  305. (LIST (LENGTH N) (LENGTH N))))
  306. )
  307. but only calls LENGTH once. Using the (WITH ..) form to
  308. introduce a local LN may be clearer.
  309. For example,
  310. (SUMLENGTHS
  311. '((1 2 3 4 5)(a b c)(x y)))
  312. is
  313. ((5 5) (3 3) (2 2)) Flow Of Control 7 February 1983 PSL Manual
  314. page 9.10 section 9.4
  315. (ON V1 V2)
  316. Cdr Cdr ____ assigns the variable V1 successive Cdrs of the list V2.
  317. (FROM VAR INIT FINAL STEP)
  318. is a numeric iteration clause. The variable is first
  319. assigned INIT, and then incremented by step until it is
  320. larger than FINAL. INIT, FINAL, and STEP are optional.
  321. INIT and STEP both default to 1, and if FINAL is
  322. omitted the iteration continues until stopped by some
  323. other means. To specify a STEP with INIT or FINAL
  324. omitted, or a FINAL with INIT omitted, place NIL (the
  325. constant -- it cannot be an expression) in the
  326. appropriate slot to be omitted. FINAL and STEP are
  327. only evaluated once.
  328. (FOR VAR INIT NEXT)
  329. assigns the variable INIT first, and subsequently the
  330. value of the expression NEXT. INIT and NEXT may be
  331. omitted. Note that this is identical to the behavior
  332. Do Do of iterators in a Do.
  333. (WITH V1 V2 ... Vn)
  334. introduces N locals, initialized to NIL. In addition,
  335. each Vi may also be of the form (VAR INIT), in which
  336. case it is initialized to INIT.
  337. (DO S1 S2 ... Sn)
  338. causes the Si's to be evaluated at each iteration.
  339. There are two clauses which allow arbitrary code to be executed
  340. before the first iteration, and after the last.
  341. (INITIALLY S1 S2 ... Sn)
  342. causes the Si's to be evaluated in the new environment
  343. (i.e. with the iteration variables bound to their
  344. initial values) before the first iteration.
  345. (FINALLY S1 S2 ... Sn)
  346. causes the Si's to be evaluated just before the
  347. function returns.
  348. The next few clauses build up return types. Except for the
  349. RETURNS/RETURNING clause, they may each take an additional
  350. argument which specifies that instead of returning the
  351. appropriate value, it is accumulated in the specified variable.
  352. For example, an unzipper might be defined as PSL Manual 7 February 1983 Flow Of Control
  353. section 9.4 page 9.11
  354. (DE UNZIP (L)
  355. (FOR (IN U L) (WITH X Y)
  356. (COLLECT (FIRST U) X)
  357. (COLLECT (SECOND U) Y)
  358. (RETURNS (LIST X Y))))
  359. Zip Zip ____ This is essentially the opposite of Zip. Given a list of 2
  360. ____ ____ ____ element lists, it unzips them into 2 lists, and returns a list of
  361. ____ those 2 lists. For example, (unzip '((1 a)(2 b)(3 c))) returns
  362. is ((1 2 3)(a b c)).
  363. (RETURNS EXP)
  364. For For causes the given expression to be the value of the For.
  365. Returning is synonymous with returns. It may be given
  366. additional arguments, in which case they are evaluated
  367. in order and the value of the last is returned
  368. ProgN ProgN (implicit ProgN).
  369. (COLLECT EXP)
  370. causes the successive values of the expression to be
  371. Append ____ Append collected into a list. Each value is Appended to the
  372. ____ end of the list.
  373. (UNION EXP)
  374. ____ is similar, but only adds an element to the list if it
  375. is not equal to anything already there.
  376. (CONC EXP)
  377. NConc NConc causes the successive values to be NConc'd together.
  378. (JOIN EXP)
  379. causes them to be appended.
  380. (COUNT EXP)
  381. returns the number of times EXP was non-NIL.
  382. (SUM EXP), (PRODUCT EXP), (MAXIMIZE EXP), and (MINIMIZE EXP)
  383. do the obvious. Synonyms are summing, maximizing, and
  384. minimizing.
  385. (ALWAYS EXP)
  386. returns T if EXP is non-NIL on each iteration. If EXP
  387. is ever NIL, the loop terminates immediately, no
  388. epilogue code, such as that introduced by finally is
  389. run, and NIL is returned.
  390. (NEVER EXP)
  391. is equivalent to (ALWAYS (NOT EXP)).
  392. (WHILE EXP) and (UNTIL EXP)
  393. Explicit tests for the end of the loop may be given Flow Of Control 7 February 1983 PSL Manual
  394. page 9.12 section 9.4
  395. using (WHILE EXP). The loop terminates if EXP becomes
  396. NIL at the beginning of an iteration. (UNTIL EXP) is
  397. While Until While Until equivalent to (WHILE (NOT EXP)). Both While and Until
  398. may be given additional arguments; (WHILE E1 E2 ... En)
  399. is equivalent to (WHILE (AND E1 E2 ... En)) and
  400. (UNTIL E1 E2 ... En) is equivalent to
  401. (UNTIL (OR E1 E2 ... En)).
  402. (WHEN EXP)
  403. causes a jump to the next iteration if EXP is NIL.
  404. (UNLESS EXP)
  405. is equivalent to (WHEN (NOT EXP)).
  406. For For For is a general iteration construct similar in many ways to the LISP
  407. Loop Loop Machine and MACLISP Loop construct, and the earlier Interlisp CLISP
  408. For For iteration construct. For, however, is considerably simpler, far more
  409. For For "lispy", and somewhat less powerful. For only works in LISP syntax.
  410. All variable binding/updating still precedes any tests or other code.
  411. When Unless When Unless Also note that all When or Unless clauses apply to all action clauses, not
  412. For For just subsequent ones. This fixed order of evaluation makes For less
  413. Loop Loop powerful than Loop, but also keeps it considerably simpler. The basic
  414. order of evaluation is
  415. a. bind variables to initial values (computed in the outer
  416. environment)
  417. Initially Initially b. execute prologue (i.e. Initially clauses)
  418. c. while none of the termination conditions are satisfied:
  419. When Unless When Unless i. check conditionalization clauses (When and Unless), and
  420. start next iteration if all are not satisfied.
  421. ii. perform body, collecting into variables as necessary
  422. iii. next iteration
  423. d. (after a termination condition is satisfied) execute the
  424. Finally Finally epilogue (i.e. Finally clauses)
  425. For For For does all variable binding/updating in parallel. There is a similar
  426. For* For* macro, For*, which does it sequentially. PSL Manual 7 February 1983 Flow Of Control
  427. section 9.4 page 9.13
  428. For!* For!* _ ____ ___ _____ (For!* [S:form]): any macro
  429. 9.4.2. Mapping Functions 9.4.2. Mapping Functions 9.4.2. Mapping Functions
  430. )
  431. The mapping functions long familiar to LISP programmers are present in
  432. For For PSL. However, we believe that the For construct described above or the
  433. ForEach ForEach simpler ForEach described below is generally more useful, since it obviates
  434. the usual necessity of constructing a lambda expression, and is often more
  435. transparent. Mapping functions with more than two arguments are not
  436. ____ currently supported. Note however that several lists may be iterated along
  437. For For with For, and with considerably more generality. For example:
  438. (Prog (I)
  439. (Setq I 0)
  440. (Return
  441. (Mapcar L
  442. (Function (Lambda (X)
  443. (Progn
  444. (Setq I (Plus I 1))
  445. (Cons I X)))))))
  446. may be expressed more transparently as
  447. (For (IN X L) (FROM I 1) (COLLECT (CONS I X)))
  448. Note that there is currently no RLISP syntax for this, but we are
  449. contemplating something like:
  450. FOR X IN L AS I FROM 1 COLLECT I . X;
  451. For For To augment the simpler For loop present in basic PSL and support the
  452. For Each For Each RLISP For Each construct, the following list iterator has been provided:
  453. ForEach ForEach _ ___ ___ _____ (ForEach U:any): any macro
  454. _____ _____ _____ macro macro This macro is essentially equivalent to the the map functions as
  455. follows:
  456. Possible forms are:
  457. Setting X to successive elements (CARs) of U:
  458. (FOREACH X IN U DO (FOO X)) --> (MAPC U 'FOO)
  459. (FOREACH X IN U COLLECT (FOO X))--> (MAPCAR U 'FOO)
  460. (FOREACH X IN U CONC (FOO X)) --> (MAPCAN U 'FOO)
  461. (FOREACH X IN U JOIN (FOO X)) --> (MAPCAN U 'FOO)
  462. Setting X to successive CDRs of U:
  463. (FOREACH X ON U DO (FOO X)) --> (MAP U 'FOO) Flow Of Control 7 February 1983 PSL Manual
  464. page 9.14 section 9.4
  465. (FOREACH X ON U COLLECT (FOO X))--> (MAPLIST U 'FOO)
  466. (FOREACH X ON U CONC (FOO X)) --> (MAPCON U 'FOO)
  467. (FOREACH X ON U JOIN (FOO X)) --> (MAPCON U 'FOO)
  468. The RLISP syntax is quite simple:
  469. FOR EACH x IN y DO z;
  470. FOR EACH x ON y COLLECT z;
  471. etc.
  472. Note that FOR EACH may be written as FOREACH
  473. Map Map _ ____ __ ________ ___ ____ (Map X:list FN:function): NIL expr
  474. Cdr __ Cdr _ Applies FN to successive Cdr segments of X. NIL is returned.
  475. This is equivalent to:
  476. (FOREACH u ON x DO (FN u))
  477. MapC MapC _ ____ __ ________ ___ ____ (MapC X:list FN:function): NIL expr
  478. Car __ Car ____ _ FN is applied to successive Car segments of list X. NIL is
  479. returned. This is equivalent to:
  480. (FOREACH u IN x DO (FN u))
  481. MapCan MapCan _ ____ __ ________ ____ ____ (MapCan X:list FN:function): list expr
  482. Car ____ __ Car _ A concatenated list of FN applied to successive Car elements of X
  483. is returned. This is equivalent to:
  484. (FOREACH u IN x CONC (FN u))
  485. MapCar MapCar _ ____ __ ________ ____ ____ (MapCar X:list FN:function): list expr
  486. ____ __ Returned is a constructed list, the elements of which are FN
  487. Car Car ____ _ applied to each Car of list X. This is equivalent to:
  488. (FOREACH u IN x COLLECT (FN u))
  489. MapCon MapCon _ ____ __ ________ ____ ____ (MapCon X:list FN:function): list expr
  490. Cdr ____ __ Cdr Returned is a concatenated list of FN applied to successive Cdr
  491. _ segments of X. This is equivalent to: PSL Manual 7 February 1983 Flow Of Control
  492. section 9.4 page 9.15
  493. (FOREACH u ON x CONC (FN u))
  494. MapList MapList _ ____ __ ________ ____ ____ (MapList X:list FN:function): list expr
  495. ____ __ Returns a constructed list, the elements of which are FN applied
  496. Cdr Cdr _ to successive Cdr segments of X. This is equivalent to:
  497. (FOREACH u ON x COLLECT (FN u))
  498. 9.4.3. Do 9.4.3. Do 9.4.3. Do
  499. Do Let Do Let The MACLISP style Do and Let are now partially implemented in the USEFUL
  500. module.
  501. Do Do _ ____ _ ____ _ ____ ___ _____ (Do A:list B:list [S:form]): any macro
  502. Do Do The Do macro is a general iteration construct similar to that of
  503. LISPM and friends. However, it does differ in some details; in
  504. Do Do particular it is not compatible with the "old style Do" of
  505. MACLISP, nor does it support the "no end test means once only"
  506. Do Do convention. Do has the form
  507. (DO (I1 I2 ... In)
  508. (TEST R1 R2 ... Rk)
  509. S1
  510. S2
  511. ...
  512. Sm)
  513. in which there may be zero or more I's, R's, and S's. In general
  514. the I's have the form
  515. (var init step)
  516. Do Do On entry to the Do form, all the inits are evaluated, then the
  517. variables are bound to their respective inits. The test is
  518. evaluated, and if non-NIL the form evaluates the R's and returns
  519. the value of the last one. If none are supplied it returns NIL.
  520. If the test evaluates to NIL the S's are evaluated, the variables
  521. are assigned the values of their respective steps in parallel,
  522. and the test evaluated again. This iteration continues until
  523. test evaluates to a non-NIL value. Note that the inits are
  524. evaluated in the surrounding environment, while the steps are
  525. Do Do evaluated in the new environment. The body of the Do (the S's)
  526. Prog Go Prog Go is a Prog, and may contain labels and Go's, though use of this is
  527. Return Return discouraged. It may be changed at a later date. Return used
  528. Do Do within a Do returns immediately without evaluating the test or
  529. exit forms (R's). Flow Of Control 7 February 1983 PSL Manual
  530. page 9.16 section 9.4
  531. There are alternative forms for the I's: If the step is omitted,
  532. the variable's value is left unchanged. If both the init and
  533. __ step are omitted or if the I is an id, it is initialized to NIL
  534. and left unchanged. This is particularly useful for introducing
  535. SetQ SetQ dummy variables which are SetQ'd inside the body.
  536. Do!* Do!* _ ____ _ ____ _ ____ ___ _____ (Do!* A:list B:list [C:form]): any macro
  537. Do!* Do Do!* Do Do!* is like Do, except the variable bindings and updatings are
  538. done sequentially instead of in parallel.
  539. Do-Loop Do-Loop _ ____ _ ____ _ ____ _ ____ ___ _____ (Do-Loop A:list B:list C:list [S:form]): any macro
  540. Do-Loop Do Do-Loop Do Do-Loop is like Do, except that it takes an additional argument,
  541. a prologue. The general form is
  542. (DO-LOOP (I1 I2 ... In)
  543. (P1 P2 ... Pj)
  544. (TEST R1 R2 ... Rk)
  545. S1
  546. S2
  547. ...
  548. Sm)
  549. Do Do This is executed just like the corresponding Do, except that
  550. after the bindings are established and initial values assigned,
  551. but before the test is first executed the P's are evaluated, in
  552. order. Note that the P's are all evaluated exactly once
  553. (assuming that none of the P's err out, or otherwise throw to a
  554. surrounding context).
  555. Do-Loop!* Do-Loop!* _ ____ _ ____ _ ____ _ ____ ___ _____ (Do-Loop!* A:list B:list C:list [S:form_]): any macro
  556. Do-Loop!* Do-Loop!* Do-Loop!* does the variable bindings and undates sequentially
  557. instead of in parallel.
  558. Let Let _ ____ _ ____ ___ _____ (Let A:list [B:form]): any macro
  559. Let Let Let is a macro giving a more perspicuous form for writing lambda
  560. expressions. The basic form is
  561. (LET ((V1 I1) (V2 I2) ...(Vn In)) S1 S2 ... Sn)
  562. The I's are evaluated (in an unspecified order), and then the V's
  563. are bound to these values, the S's evaluated, and the value of
  564. the last is returned. Note that the I's are evaluated in the
  565. outer environment before the V's are bound. PSL Manual 7 February 1983 Flow Of Control
  566. section 9.4 page 9.17
  567. __ Note: the id LET conflicts with a similar construct in RLISP and
  568. REDUCE
  569. Let!* Let!* _ ____ _ ____ ___ _____ (Let!* A:list [B:form]): any macro
  570. Let!* Let Let!* Let Let!* is just like Let except that it makes the assignments
  571. sequentially. That is, the first binding is made before the
  572. value for the second one is computed.
  573. 9.5. Non-Local Exits 9.5. Non-Local Exits 9.5. Non-Local Exits
  574. One occasionally wishes to discontinue a computation in which the lexical
  575. Return Return restrictions on placement of Return are too restrictive. The non-local
  576. Catch Throw Catch Throw exit constructs Catch and Throw exist for these cases. They should not,
  577. however, be used indiscriminately. The lexical restrictions on their more
  578. local counterparts ensure that the flow of control can be ascertained by
  579. Catch Throw Catch Throw looking at a single piece of code. With Catch and Throw, control may be
  580. passed to and from totally unrelated pieces of code. Under some
  581. conditions, these functions are invaluable. Under others, they can wreak
  582. havoc.
  583. Catch Catch ___ __ ____ ____ ___ ____ ________ _____ (Catch TAG:id [FORM:form]): any Open-Compiled, fexpr
  584. Catch Eval Catch ___ Eval ____ Catch evaluates the TAG and then calls Eval on the FORMs in a
  585. Throw Throw ___ ___ protected environment. If during this evaluation (Throw TAG VAL)
  586. Catch Throw Catch ___ Throw occurs, Catch immediately returns VAL. If no Throw occurs, the
  587. ____ value of the last FORM is returned. Note that in general only
  588. Throw Throw Eq Throw ___ Throw ___ Eq Throws with the same TAG are caught. Throws whose TAG is not Eq
  589. Catch Catch Catch Catch ___ to that of Catch are passed on out to surrounding Catches. A TAG
  590. Catch Catch of NIL, however, is special. (Catch NIL @var[form)] catches any
  591. Throw Throw Throw.
  592. __________ ______ THROWSIGNAL!* [Initially: NIL] global
  593. __________ ______ THROWTAG!* [Initially: NIL] global
  594. The FLUID variables THROWSIGNAL!* and THROWTAG!* may be
  595. Catch Catch interrogated to find out if the most recently evaluated Catch was
  596. Throw Throw Throw Throw Thrown to, and what tag was passed to the Throw. THROWSIGNAL!*
  597. Set Catch Set Catch is Set to NIL upon normal exit from a Catch, and to T upon normal
  598. Throw Set Throw Set exit from Throw. THROWTAG!* is Set to the first argument passed
  599. Throw Throw Eval Throw Throw Eval ____ to the Throw. (Mark a place to Throw to, Eval FORM.) Flow Of Control 7 February 1983 PSL Manual
  600. page 9.18 section 9.5
  601. Throw Throw ___ __ ___ ___ ____ ________ ____ (Throw TAG:id VAL:any): None Returned expr
  602. Catch Eq Catch Eq This passes control to the closest surrounding Catch with an Eq
  603. Catch ___ Catch or null TAG. If there is no such surrounding Catch it is an
  604. _____ _____ _____ Throw __ ___ _______ __ ___ Throw error in the context of the Throw. That is, control is not
  605. Throw Error Throw Error Thrown to the top level before the call on Error. (Non-local
  606. Goto Goto Goto.)
  607. Some examples:
  608. In LISP syntax, with
  609. (DE DOIT (x)
  610. (COND ((EQN x 1) 100)
  611. (T (THROW 'FOO 200))))
  612. (CATCH 'FOO (DOIT 1) (PRINT "NOPE") 0)
  613. will continue and execute the PRINT statement
  614. and return 0
  615. while
  616. (CATCH 'FOO (DOIT 2) (PRINT "NOPE") 0)
  617. will of course THROW, returning 200 and not executing
  618. the last forms.
  619. A common problem people encounter is how to pass arguments and/or
  620. CATCH CATCH computed functions or tags into CATCH for protected evaluation. The
  621. following examples should illustrate. Note that TAG is quoted, since it is
  622. evaluated before use in CATCH and THROW.
  623. In LISP syntax:
  624. (DE PASS-ARGS(X1 X2)
  625. (CATCH 'FOO (FEE (PLUS2 X1 X2) (DIFFERENCE X1 X2))))
  626. This is simple, because CATCH compiles open. No FLUID declarations or
  627. Apply Apply LIST building is needed, as in previous versions of PSL. An explicit Apply
  628. must be used for a function argument; usually, the APPLY will compile open,
  629. with no overhead:
  630. In LISP syntax:
  631. (DE PASS-FN(X1 FN)
  632. (CATCH 'FOO (APPLY FN (LIST X1))))
  633. Catch Throw Catch Throw The following MACROs are provided to aid in the use of Catch and Throw
  634. with a NIL tag, by examining the THROWSIGNAL!* and THROWTAG!*: PSL Manual 7 February 1983 Flow Of Control
  635. section 9.5 page 9.19
  636. Catch!-All Catch!-All __ ________ ____ ____ ___ _____ (Catch!-All FN:function [FORM:form]): any macro
  637. Catch Catch This issues a (Catch NIL ...); if a Throw was actually done, the
  638. __ function FN is applied to the two arguments THROWTAG!* and the
  639. throw Throw throw Throw value returned by the throw. Thus FN is applied only if a Throw
  640. was executed.
  641. Unwind!-All Unwind!-All __ ________ ____ ____ ___ _____ (Unwind!-All FN:function [FORM:form]): any macro
  642. Catch Catch __ This issues a (Catch NIL ...). The function FN is always called,
  643. and applied to the two arguments THROWTAG!* and the value
  644. throw Throw throw Throw __ returned by the throw. If no Throw was done then FN is called on
  645. NIL and the value returned.
  646. Unwind!-Protect Unwind!-Protect _ ____ _ ____ ___ _____ (Unwind!-Protect F:form [C:form]): any macro
  647. _ The idea is to execute the "protected" form, F, and then run some
  648. _ "clean-up" forms C even if a Throw (or Error) occurred during the
  649. Catch _ Catch evaluation of F. This issues a (Catch NIL ...), the cleanup forms
  650. are then run, and finally either the value is returned if no
  651. Throw occurred, or the Throw is "re-thrown" to the same tag.
  652. A common example is to ensure a file be closed after processing,
  653. even if an error or throw occurred:
  654. (SETQ chan (OPEN file ....))
  655. (UNWIND-PROTECT (process-file)
  656. (CLOSE chan))
  657. Note: Certain special tags are used in the PSL system, and should not be
  658. interfered with casually:
  659. Error ErrorSet Error ErrorSet !$ERROR!$ Used by Error and ErrorSet which are implemented in terms of
  660. Catch Throw Catch Throw Catch and Throw, see Chapter 14).
  661. !$UNWIND!-PROTECT!$
  662. A special TAG placed to ensure that ALL throws pause at the
  663. UNWIND-PROTECT "mark".
  664. PROG GO RETURN PROG GO RETURN !$PROG!$ Used to communicate between interpreted PROGs, GOs and RETURNs.