FOR _ _ _ _ _ _ _ _ _ _ _ _ command
The for command is used for iterative loops. There are many possible forms it can take.
/ / |STEP <number> UNTIL| |<var>:=<number>| |<number>| FOR| | : | |<action> <exprn> | / | |EACH <var> IN <list> | / where <action> ::= DO|PRODUCT|SUM|COLLECT|JOIN.
<var> can be any valid REDUCE identifier except t o r nil, <inc>, <start> and <stop> can be any expression that evaluates to a positive or negative integer. <list> must be a valid list structure. The action taken must be one of the actions shown above, each of which is followed by a single REDUCE expression, statement or a group (<<...>>) or block ( begin... end) statement.
for i := 1:10 sum i; 55 for a := -2 step 3 until 6 product a; -8 a := 3; A := 3 for iter := 4:a do write iter; m := 0; M := 0 for s := 10 step -1 until 3 do <<d := 10*s;m := m + d>>; m; 520 for each x in {q,r,s} sum x**2; 2 2 2 Q + R + S for i := 1:4 collect 1/i; 1 1 1 {1,-,-,-} 2 3 4 for i := 1:3 join list solve(x**2 + i*x + 1,x); SQRT(3)*I + 1 {{X= --------------, 2 SQRT(3)*I - 1 X= --------------} 2 {X=-1}, SQRT(5) + 3 SQRT(5) - 3 {X= - -----------,X=-----------}} 2 2
The behavior of each of the five action words follows:
Action Word Behavior Keyword Argument Type Action do statement, command, group Evaluates its argument once or block for each iteration of the loop, not saving results collect expression, statement, Evaluates its argument once for command, group, block, list each iteration of the loop, storing the results in a list which is returned by the for statement when done join list or an operator which Evaluates its argument once for produces a list each iteration of the loop, appending the elements in each individual result list onto the overall result list product expression, statement, Evaluates its argument once for command, group or block each iteration of the loop, multiplying the results together and returning the overall product sum expression, statement, Evaluates its argument once for command, group or block each iteration of the loop, adding the results together and returning the overall sum
For number-driven for statements, if the ending limit is smaller than the beginning limit (larger in the case of negative steps) the action statement is not executed at all. The iterative variable is local to the for statement, and does not affect the value of an identifier with the same name. For list-driven for statements, if the list is empty, the action statement is not executed, but no error occurs.
You can use nested for statements, with the inner for statement after the action keyword. You must make sure that your inner statement returns an expression that the outer statement can handle.