for.hlp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. FOR is a general iteration construct similar in many ways to the Lisp
  2. Machine LOOP construct, and the earlier InterLISP CLISP iteration
  3. construct. FOR, however, is considerably simpler, far more "lispy",
  4. and somewhat less powerful. FOR is loaded as part of the USEFUL
  5. package. It is hoped that eventuall the RLISP parser will be modified
  6. to emit calls on this new FOR macro instead of the old one.
  7. The arguments to FOR are clauses; each clause is itself a list of a
  8. keyword and one or more arguments. The clauses may introduce local
  9. variables, specify return values, have side-effects, when the iteration
  10. should cease, and so on. Before
  11. going further, it is probably best to give an example. The following
  12. function will zip together three lists into a list of three element
  13. lists.
  14. (de zip3 (x y z) (for (in u x) (in v y) (in w z) (collect (list u v w))))
  15. The three IN clauses specify that their first argument should take
  16. successive elements of the respective lists, and the COLLECT clause specifies
  17. that the answer should be a list built out of its argument. For
  18. example, (zip3 '(1 2 3 4) '(a b c d) '(w x y z)) is
  19. ((1 a w)(2 b x)(3 c y)(4 d z)).
  20. Following are described all the possible clauses. The first few
  21. introduce iteration variables. Most of these also give some means of
  22. indicating when iteration should cease. For example, when a list being
  23. mapped over by an IN clause is exhausted, iteration must cease. If
  24. several such clauses are given in FOR expression, iteration will cease
  25. whenever on of the clauses indicates it should, whether or not the
  26. other clauses indicate that it should cease.
  27. (in v1 v2) assigns the variable v1 successive elements of the list v2.
  28. This may take an additional, optional argument:
  29. a function to be applied to the extracted element or sublist before
  30. it is assigned to the variable. The following returns the sum of the
  31. lengths of all the elements of L. [rather a kludge -- not sure why this
  32. is here. Perhaps it should come out again.]
  33. (de SumLengths (L) (for (in N L length) (sum N)))
  34. For example, (SumLengths '((1 2 3 4 5)(a b c)(x y))) is 10.
  35. (on v1 v2) assigns the varaible v1 successive cdrs of the list v2.
  36. (from var init final step) is a numeric clause. The variable is first
  37. assigned init, and then incremented by step until it is larger than
  38. final. Init, final, and step are optional. Init and step both default
  39. to 1, and if final is omitted the iteration will continue until
  40. stopped by some other means. To specify a step with init or final
  41. omitted, or a final with init omitted place nil (the constant -- it
  42. cannot be an expression) in the appropriate slot to be omitted.
  43. Final and step are only evaluated once.
  44. (for var init next) assigns the variable init first, and subsequently
  45. the value of the expression next. Init and next may be omitted. Note
  46. that this is identical to the behaviour of iterators in a DO.
  47. (with v1 v2 ... vN) introduces N locals, initialized to nil. In
  48. addition, each vi may also be of the form (var init), in which case it
  49. will be initialized to init.
  50. There are two clauses which allow arbitrary code to be executed before
  51. the first iteration, and after the last. (initially s1 s2 ... sN) will
  52. cause the si's to be evaluated in the new environment (i.e. with the
  53. iteration variables bound to their initial values) before the first
  54. iteration. (finally s1 s2 ... sN) causes the si's to be evaluated just
  55. before the function returns.
  56. (do s1 s2 ... sN) causes the si's to be evaluated at each iteration.
  57. The next few clauses build up return types. Except for the
  58. RETURNS/RETURNING clause, they may each take an additional argument
  59. which specifies that instead of returning the appropriate value, it is
  60. accumulated in the specified variable. For example, an unzipper might
  61. be defined as
  62. (de unzip3 (L)
  63. (for (u in L) (with X Y Z)
  64. (collect (car U) X)
  65. (collect (cadr U) Y)
  66. (collect (caddr U) Z)
  67. (returns (list X Y Z))))
  68. This is essentially the opposite of zip3. Given a list of three element
  69. lists, it unzips them into three lists, and returns a list of those
  70. three lists. For example, (unzip '((1 a w)(2 b x)(3 c y)(4 d z)))
  71. is ((1 2 3 4)(a b c d)(w x y z)).
  72. (returns exp) causes the given expression to be the value of the FOR.
  73. Returning is synonymous with returns. It may be given additional
  74. arguments, in which case they are evaluated in order and the value of
  75. the last is returned (implicit PROGN).
  76. (collect exp) causes the succesive values of the expression to be
  77. collected into a list.
  78. (union exp) is similar, but only adds an element to the list if it is
  79. not equal to anything already there.
  80. (conc exp) causes the succesive values to be nconc'd together.
  81. (join exp) causes them to be appended.
  82. (count exp) returns the number of times exp was non-nil.
  83. (sum exp), (product exp), (maximize exp), and (minimize exp) do the obvious.
  84. Synonyms are summing, maximizing, and minimizing.
  85. (always exp) will return t if exp is non-nil on each iteration. If exp
  86. is ever nil, the loop will terminate immediately, no epilogue code,
  87. such as that introduced by finally will be run, and nil will be
  88. returned. (never exp) is equivlent to (always (not exp)).
  89. Explicit tests for the end of the loop may be given using (while exp).
  90. The loop will terminate if exp becomes nil at the beginning of an
  91. iteration. (until exp) is equivalent to (while (not exp)).
  92. Both while and until may be given additional arguments;
  93. (while e1 e2 ... eN) is equivalent to (while (and e1 e2 ... eN))
  94. and (until e1 e2 ... eN) is equivalent to (until (or e1 e2 ... eN)).
  95. (when exp) will cause a jump to the next iteration if exp is nil.
  96. (unless exp) is equivalent to (when (not exp)).
  97. Unlike MACLISP and clones' LOOP, FOR does all variable binding/updating
  98. in parallel. There is a similar macro, FOR*, which does it
  99. sequentially. All variable binding/updating still preceeds any tests
  100. or other code. Also note that all WHEN or UNLESS clauses apply to all
  101. action clauses, not just subsequent ones. This fixed order of
  102. evaluation makes FOR less powerful than LOOP, but also keeps it
  103. considerably simpler. The basic order of evaluation is
  104. 1) bind variables to initial values (computed in the outer environment)
  105. 2) execute prologue (i.e. INITIALLY clauses)
  106. 3) while none of the termination conditions are satisfied:
  107. 4) check conditionalization clauses (WHEN and UNLESS), and start next
  108. iteration if all are not satisfied.
  109. 5) perform body, collecting into variables as necessary
  110. 6) next iteration
  111. 7) (after a termination condition is satisfied) execute the epilogue (i. e.
  112. FINALLY clauses)