CASCADE endtest template startvalue
(CASCADE endtest template1 startvalue1 template2 startvalue2 ...)
(CASCADE endtest template1 startvalue1 template2 startvalue2 ... finaltemplate)

Outputs the result of applying a template (or several templates, see the section entitled Template-Based Iteration) repeatedly, with a given value filling the slot the first time, and the result of each application filling the slot for the following application.

In the simplest case, CASCADE has three inputs. The template is a one-slot expression template. That template is evaluated some number of times (perhaps zero). On the first evaluation, the slot is filled with the startvalue input; on subsequent evaluations, the slot is filled with the result of the previous evaluation. The number of evaluations is determined by the endtest input. This can be either a nonnegative integer, in which case the template is evaluated that many times, or a predicate expression template, in which case it is evaluated (with the same slot filler that is used for the evaluation of the second input) repeatedly, and the CASCADE evaluation continues as long as the predicate value is FALSE. (In other words, the predicate template indicates the condition for stopping.)

If the template is evaluated zero times, CASCADE outputs startvalue. Otherwise, CASCADE outputs the value produced by the last template evaluation.

CASCADE templates may include the symbol # to represent the number of times the template has been evaluated. This slot is filled with 1 for the first evaluation, 2 for the second, and so on.

[1 2 3 4 5]


SHOW CASCADE 5 [# * ?] 1

Several cascaded results can be computed in parallel by providing additional template-startvalue pairs as inputs to CASCADE. In this case, all templates (including the endtest template, if used) are multi-slot, with the number of slots equal to the number of pairs of inputs. In each round of evaluations, ?2 represents the result of evaluating the second template in the previous round. If the total number of inputs (including the first endtest input) is odd, then the output from CASCADE is the final value of the first template. If the total number of inputs is even, then the last input is a template that is evaluated once, after the end test is satisfied, to determine the output from CASCADE.

  OUTPUT (CASCADE :n [?1 + ?2] 1 [?1] 0)

                  [WORD BUTFIRST ? FIRST ?]
                  [WORD ? "ay])
END Logo