useful.hlp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. A number of useful options can be defined by Loading Useful.
  2. Descriptions follow.
  3. BACKQUOTE and friends
  4. ------------------
  5. (Note that the special symbols decribed here will only work in LISP
  6. syntax, not RLISP. In RLISP you may simply use the functions
  7. BACKQUOTE, UNQUOTE, UNQUOTEL, and UNQUOTED)
  8. The backquote symbol "`" is a read macro which introduces a quoted
  9. expression which may contain the unquote symbols comma "," and
  10. comma-atsign ",@". Any appropriate form consisting of the unquoted
  11. expression, calls to the function cons, and quoted expressions are
  12. produced so that the resulting expression looks like the quoted one
  13. except that the values of the unquote expressions are substitued in the
  14. appropriate place. ",@" splices in the value of the subsequent
  15. expression (i.e. strips off the outer layer of parentheses). Thus
  16. `(a (b ,x) c d ,@x e f)
  17. is equivalent to
  18. (cons 'a (cons (list 'b x) (append '(c d) (append x '(e f)))))
  19. In particular, if x is bound to (1 2 3) this will evaluate to
  20. (a (b (1 2 3)) c d 1 2 3 e f)
  21. ",." is like ",@", except it may use destructive operations on its
  22. argument.
  23. DESETQ
  24. ------
  25. DESETQ is a destructuring setq. That is, the first argument is a piece
  26. of list structure whose atoms are all ids. Each is setq'd to the
  27. corresponding part of the second argument. For instance
  28. (desetq (a (b) . c) '((1) (2) (3) 4))
  29. setq's a to (1), b to 2, and c to ((3) 4).
  30. DEFMACRO
  31. --------
  32. DEFMACRO is a useful tool for defining macros. A DEFMACRO form looks
  33. like
  34. (defmacro <name> <pattern> <s1> <s2> ... <sN>)
  35. The <pattern> is an S-expression made of pairs and ids. It is matched
  36. against the arguments of the macro much like the first argument to
  37. desetq. All of the non-nil ids in <pattern> are local variables which
  38. may be used freely in the body (the <si>). When the macro is called
  39. the <si> are evaluated as in a progn with the local variables in
  40. <pattern> appropriately bound, and the value of <sN> is returned.
  41. DEFMACRO is often used with backquote.
  42. DEFLAMBDA
  43. ---------
  44. Another macro defining macro similar to DEFMACRO is DEFLAMBDA. The
  45. arguments to DEFLAMBDA are identical to those for DE. The resulting
  46. macro is simply application of a lambda expression. Thus a function
  47. defined with DEFLAMBDA will have semantics identical to that of a
  48. function defined with DE, modulo the ability to dynamically redefine
  49. the function. This is a convenient way to cause functions to be open
  50. compiled.
  51. For example, if (NEW-FOO X Y) should return (LIST X Y (LIST X Y)) we do
  52. not want it to be a simple substitution style macro, in case one of the
  53. actual arguments has side effects, or is expensive to compute. If we
  54. define it by
  55. (DEFLAMBDA NEW-FOO (X Y) (LIST X Y (LIST X Y)))
  56. then we will have the desired behaviour. In particular,
  57. (NEW-FOO (BAR) (SETQ BAZ (BOOZE)))
  58. will expand to
  59. ((LAMBDA (X Y)
  60. (LIST X Y (LIST X Y)) )
  61. (BAR)
  62. (SETQ BAZ (BOOZE)) )
  63. PROG1
  64. -----
  65. PROG1 evaluates its arguments in order, like PROGN, but returns the
  66. value of the first.
  67. LET and LET*
  68. ------------
  69. LET is a macro giving a more perspicuous form for writing lambda
  70. expressions. The basic form is
  71. (let ((v1 i1) (v2 i2) ...(vN iN))
  72. s1
  73. s2
  74. ...
  75. sN)
  76. The i's are evaluated (in an unspecified order), and then the v's are
  77. bound to these values, the s's evaluated, and the value of the last
  78. is returned. Note that the i's are evaluated in the outer environment
  79. before the v's are bound.
  80. LET!* is just like LET, except that it makes the assignments
  81. sequentially. That is, the first binding is made before the value
  82. for the second one is computed.
  83. MACROEXPAND
  84. -----------
  85. MACROEXPAND is a useful tool for debugging macro definitions. If given
  86. one argument, MACROEXPAND will all expand all the macros in that form.
  87. Often we wish more control over this process. For example, if a macro
  88. expands into a let, we may not wish to see the LET itself expanded to a
  89. lambda expression. Therefor additional arguments may be given to
  90. MACROEXPAND. If these are supplied, only they should be macros, and
  91. only those specified will be expanded.
  92. PUSH and POP
  93. ------------
  94. These are convenient macros for adding and deleting things from the
  95. head of a list. (push item stack) is equivalent to (setq stack (cons
  96. item stack)), and (pop stack) does (setq stack (cdr stack)) and
  97. returns the item popped off stack. An additional argument may be
  98. supplied to pop, in which case it is a variable which is setq'd to the
  99. popped value.
  100. INCR and DECR
  101. -------------
  102. These are convenient macros for incrementing and decrementing numeric
  103. variables. (incr i) is equivalent to (setq i (add1 i)) and (decr i) to
  104. (setq i (sub1 i)). Additional arguments may be supplied, which are
  105. summed and used as the amounts by to increment or decrement.
  106. DO, DO*, DO-LOOP, and DO-LOOP*
  107. ------------------------------
  108. The DO macro is a general iteration construct similar to that of LISPM
  109. and friends. However, it does differ in some details; in particular it
  110. is not compatible with the "old style DO" of MACLISP (which is a crock
  111. anyway), nor does it support the "no end test means once only"
  112. convention (which was just an ugly kludge to get an initialized prog).
  113. DO has the form
  114. (do (i1 i2 ... iN)
  115. (test r1 r2 ... rK)
  116. s1
  117. s2
  118. ...
  119. sM)
  120. where there may be zero or more i's, r's, and s's. In general the
  121. i's will have the form
  122. (var init step)
  123. On entry to the DO form, all the inits are evaluated, then the
  124. variables are bound to their respective inits. The test is evaluated,
  125. and if non-nil the form evaluates the r's and returns the value of the
  126. last one. If none are supplied it returns nil. If the test evaluates
  127. to nil the s's are evaluated, the variables are assigned the values of
  128. their respective steps in parallel, and the test evaluated again. This
  129. iteration continues until test evaluates to a non-nil value. Note that
  130. the inits are evaluated in the surrounding environment, while the steps
  131. are evaluated in the new environment. The body of the DO (the s's) is
  132. a prog, and may contain labels and GO's, though use of this is
  133. discouraged. It may be changed at a later date. RETURN used within a
  134. DO will return immediately without evaluating the test or exit forms
  135. (r's).
  136. There are alternative forms for the i's: If the step is omitted, the
  137. variable's value is left unchanged. If both the init and step are
  138. omitted or if the i is an id it is initialized to nil, and left
  139. unchanged. This is particularly useful for introducing dummy variables
  140. which will be setq'd inside the body.
  141. DO* is like DO, expcept the variable bindings and updatings are done
  142. sequentially instead of in parallel.
  143. DO-LOOP is like Do, except that it takes an additional argument, a
  144. prologue. The general form is
  145. (do-loop (i1 i2 ... iN)
  146. (p1 p2 ... pJ)
  147. (test r1 r2 ... rK)
  148. s1
  149. s2
  150. ...
  151. sM)
  152. This is executed just like the corresponding DO, except that after the
  153. bindings are established and initial values assigned, but before the
  154. test is first executed the pi's are evaluated, in order. Note that the
  155. pi's are all evaluated exactly once (assuming that none of the pi's err
  156. out, or otherwise throw to a surrounding context). DO-LOOP* does the
  157. variable bindings and undates sequentially instead of in parallel.
  158. IF, WHEN, and UNLESS for If and Only If Statements
  159. --------------------------------------------------
  160. IF is a macro to simplify the writing of a common form of COND where
  161. there are only two clauses and the antecedent of the second is t.
  162. (if <test> <then-clause> <else1>...<elseN>)
  163. The <then-clause> is evaluated if and only if the test is non-nil,
  164. otherwise the elses are evaluated, and the last returned. There may be
  165. zero elses.
  166. Related macros for common COND forms are WHEN and UNLESS.
  167. (when <test> s1 s2 ... sN)
  168. evaluates the si and returns the value of sN if and only if <test> is
  169. non-nil. Otherwise WHEN returns nil.
  170. (unless <test> s1 s2 ... sN) <=> (when (not <test>) s1 s2 ... sN).
  171. PSETQ and PSETF
  172. ---------------
  173. (psetq var1 val1 var2 val2 ... varN valN) setq's the vars to the
  174. corresponding vals. The vals are all evaluated before any assignments
  175. are made. That is, this is a parallel setq.
  176. PSETF is to SETF as PSETQ is to SETQ.
  177. SETF
  178. ----
  179. USEFUL contains an expanded version of the standard SETF macro. The
  180. principal difference from the default is that it always returns the
  181. the thing assigned (i.e. the right hand side). For example,
  182. (setf (cdr foo) '(x y z))
  183. returns '(x y z). In the default SETF the return value is
  184. indeterminate.
  185. USEFUL also makes several more functions known to SETF. All the c...r
  186. functions are included. LIST and CONS are also include, and are
  187. similar to desetq. For example,
  188. (setf (list (cons a b) c (car d)) '((1 2) 3 4 5))
  189. sets a to 1, b to (2), c to 3, and rplaca's the car of d to 4. It
  190. returns ((1 2) 3 4 5).
  191. SHARP-SIGN MACROS
  192. ------------------
  193. USEFUL defines several MACLISP style sharp sign read macros. Note that
  194. these only work with the LISP reader, not RLISP. Those currently
  195. included are
  196. #' : this is like the quote mark ' but is for FUNCTION instead of
  197. QUOTE.
  198. #/ : this returns the numeric form of the following character
  199. read without raising it. For example #/a is 97 while
  200. #/A is 65.
  201. #\ : This is a read macro for the CHAR macro, described in the PSL
  202. manual. Not that the argument is raised, if *RAISE it non-nil.
  203. For example, #\a = #\A = 65, while #\!a = #\(lower a) = 97.
  204. Char has been redefined in USEFUL to be slightly
  205. more table driven -- users can now add new "prefixes" such as
  206. META or CONTROL: just hang the appropriate function (from integers
  207. to integers) off the char-prefix-function property of the "prefix".
  208. A LARGE number of additional alias for various characters have been
  209. added, including all the "standard" ASCII names like NAK and DC1.
  210. #. : this causes the following expression to be evaluated at read
  211. time. For example, `(1 2 #.(plus 1 2) 4) reads as (1 2 3 4)
  212. #+ : this reads two expressions, and passes them to the if_system
  213. macro. That is, the first should be a system name, and if
  214. that is the current system the second argument is returned by
  215. the reader. If not, nil is returned. #- is similar, but
  216. causes the second arg to be returned only if it is NOT the
  217. current system. Note that this does NOT use splice macros,
  218. since PSL doesn't have them (I don't really know why not -- at
  219. the very least there ought to be a way to tell the reader
  220. "ignore this", even if splice macros are thought to be a
  221. kludge).
  222. FOR
  223. ---
  224. FOR is a general iteration construct similar in many ways to the Lisp
  225. Machine LOOP construct, and the earlier InterLISP CLISP iteration
  226. construct. FOR, however, is considerably simpler, far more "lispy",
  227. and somewhat less powerful. FOR will only work in LISP syntax. In
  228. fact, loading FOR will, for the time being, "break" RLISP, as it
  229. redefines the FOR macro. It is hoped that eventually the RLISP parser
  230. will be modified to emit calls on this new FOR macro instead of the old
  231. one.
  232. The arguments to FOR are clauses; each clause is itself a list of a
  233. keyword and one or more arguments. The clauses may introduce local
  234. variables, specify return values, have side-effects, when the iteration
  235. should cease, and so on. Before going further, it is probably best to
  236. give an example. The following function will zip together three lists
  237. into a list of three element lists.
  238. (de zip3 (x y z) (for (in u x) (in v y) (in w z) (collect (list u v w))))
  239. The three IN clauses specify that their first argument should take
  240. successive elements of the respective lists, and the COLLECT clause specifies
  241. that the answer should be a list built out of its argument. For
  242. example, (zip3 '(1 2 3 4) '(a b c d) '(w x y z)) is
  243. ((1 a w)(2 b x)(3 c y)(4 d z)).
  244. Following are described all the possible clauses. The first few
  245. introduce iteration variables. Most of these also give some means of
  246. indicating when iteration should cease. For example, when a list being
  247. mapped over by an IN clause is exhausted, iteration must cease. If
  248. several such clauses are given in FOR expression, iteration will cease
  249. whenever on of the clauses indicates it should, whether or not the
  250. other clauses indicate that it should cease.
  251. (in v1 v2) assigns the variable v1 successive elements of the list v2.
  252. This may take an additional, optional argument:
  253. a function to be applied to the extracted element or sublist before
  254. it is assigned to the variable. The following returns the sum of the
  255. lengths of all the elements of L. [rather a kludge -- not sure why this
  256. is here. Perhaps it should come out again.]
  257. (de SumLengths (L) (for (in N L length) (sum N)))
  258. For example, (SumLengths '((1 2 3 4 5)(a b c)(x y))) is 10.
  259. (on v1 v2) assigns the varaible v1 successive cdrs of the list v2.
  260. (from var init final step) is a numeric clause. The variable is first
  261. assigned init, and then incremented by step until it is larger than
  262. final. Init, final, and step are optional. Init and step both default
  263. to 1, and if final is omitted the iteration will continue until
  264. stopped by some other means. To specify a step with init or final
  265. omitted, or a final with init omitted place nil (the constant -- it
  266. cannot be an expression) in the appropriate slot to be omitted.
  267. Final and step are only evaluated once.
  268. (for var init next) assigns the variable init first, and subsequently
  269. the value of the expression next. Init and next may be omitted. Note
  270. that this is identical to the behaviour of iterators in a DO.
  271. (with v1 v2 ... vN) introduces N locals, initialized to nil. In
  272. addition, each vi may also be of the form (var init), in which case it
  273. will be initialized to init.
  274. There are two clauses which allow arbitrary code to be executed before
  275. the first iteration, and after the last. (initially s1 s2 ... sN) will
  276. cause the si's to be evaluated in the new environment (i.e. with the
  277. iteration variables bound to their initial values) before the first
  278. iteration. (finally s1 s2 ... sN) causes the si's to be evaluated just
  279. before the function returns.
  280. (do s1 s2 ... sN) causes the si's to be evaluated at each iteration.
  281. The next few clauses build up return types. Except for the
  282. RETURNS/RETURNING clause, they may each take an additional argument
  283. which specifies that instead of returning the appropriate value, it is
  284. accumulated in the specified variable. For example, an unzipper might
  285. be defined as
  286. (de unzip3 (L)
  287. (for (in u L) (with X Y Z)
  288. (collect (car U) X)
  289. (collect (cadr U) Y)
  290. (collect (caddr U) Z)
  291. (returns (list X Y Z))))
  292. This is essentially the opposite of zip3. Given a list of three element
  293. lists, it unzips them into three lists, and returns a list of those
  294. three lists. For example, (unzip '((1 a w)(2 b x)(3 c y)(4 d z)))
  295. is ((1 2 3 4)(a b c d)(w x y z)).
  296. (returns exp) causes the given expression to be the value of the FOR.
  297. Returning is synonymous with returns. It may be given additional
  298. arguments, in which case they are evaluated in order and the value of
  299. the last is returned (implicit PROGN).
  300. (collect exp) causes the succesive values of the expression to be
  301. collected into a list.
  302. (adjoin exp) is similar, but only adds an element to the list if it is
  303. not equal to anything already there.
  304. (adjoinq exp) is like adjoin, but uses eq instead of equal.
  305. (conc exp) causes the succesive values to be nconc'd together.
  306. (join exp) causes them to be appended.
  307. (union exp) forms the union of all the exp
  308. (unionq exp), (intersection exp), (intersectionq exp) are similar, but
  309. use the specified function instead of union.
  310. (count exp) returns the number of times exp was non-nil.
  311. (sum exp), (product exp), (maximize exp), and (minimize exp) do the obvious.
  312. Synonyms are summing, maximizing, and minimizing.
  313. (always exp) will return t if exp is non-nil on each iteration. If exp
  314. is ever nil, the loop will terminate immediately, no epilogue code,
  315. such as that introduced by finally will be run, and nil will be
  316. returned. (never exp) is equivlent to (always (not exp)).
  317. Explicit tests for the end of the loop may be given using (while exp).
  318. The loop will terminate if exp becomes nil at the beginning of an
  319. iteration. (until exp) is equivalent to (while (not exp)).
  320. Both while and until may be given additional arguments;
  321. (while e1 e2 ... eN) is equivalent to (while (and e1 e2 ... eN))
  322. and (until e1 e2 ... eN) is equivalent to (until (or e1 e2 ... eN)).
  323. (when exp) will cause a jump to the next iteration if exp is nil.
  324. (unless exp) is equivalent to (when (not exp)).
  325. Unlike MACLISP and clones' LOOP, FOR does all variable binding/updating
  326. in parallel. There is a similar macro, FOR*, which does it
  327. sequentially. All variable binding/updating still preceeds any tests
  328. or other code. Also note that all WHEN or UNLESS clauses apply to all
  329. action clauses, not just subsequent ones. This fixed order of
  330. evaluation makes FOR less powerful than LOOP, but also keeps it
  331. considerably simpler. The basic order of evaluation is
  332. 1) bind variables to initial values (computed in the outer environment)
  333. 2) execute prologue (i.e. INITIALLY clauses)
  334. 3) while none of the termination conditions are satisfied:
  335. 4) check conditionalization clauses (WHEN and UNLESS), and start next
  336. iteration if all are not satisfied.
  337. 5) perform body, collecting into variables as necessary
  338. 6) next iteration
  339. 7) (after a termination condition is satisfied) execute the epilogue (i. e.
  340. FINALLY clauses)
  341. DEFSWITCH
  342. ---------
  343. Defswitch provides a convenient machanism for declaring variables whose
  344. values need to be set in a disciplined manner. It is quite similar to
  345. T's DEFINE-SWITCH. The form of a defswitch expression is
  346. (defswitch <name> <var> [<read-action> {<set-action>}])
  347. This declares <name> to be a function of no arguments for deterimining
  348. the value of the variable <var>. <var> is declared fluid. SETF
  349. will set the value of <var> when given a call on <name> as its first
  350. argument. When <name> is called <read-action> will be evaluated
  351. (after the value of the variable is looked up). When it is set the
  352. <set-action>s will be evaluated (before the value is set). <name> may
  353. be used as a "free" variable in the <read-action> and <set-action>s, in
  354. which case it will hold the current value and new value, respectively.
  355. If <var> is nil an uninterned id will be used for the variable.
  356. Suppose we wish to keep a list in a variable, FOO, but also want to
  357. always have it's length available in FOOLENGTH. We can do this by
  358. always accessing FOO by a function as follows:
  359. (defswitch FOO nil nil (setq FOOLENGTH (length FOO)))