up previous next
foreach    --    loop command


Syntax
foreach  X  in  L  do  CMDS  endforeach
where X is a loop variable, L is a LIST

Description
The loop variable X is assigned the value of each component of L in turn. After each assignment the command sequence CMDS is executed.

NOTE: the variable X contains a copy of the value in the list; changing X will not change the list L.

Example
/**/  foreach N in 1..10 do  -- recall that 1..10 gives the list [1,2,...,10].
/**/    print N^2, " ";
/**/  endforeach;
1 4 9 16 25 36 49 64 81 100

/**/  use P ::= QQ[x,y,z];
/**/  f := x^2*y + 3*y^2*z - z^3;
/**/  J := [deriv(f, x) | x in indets(P)];
/**/  J;
[2*x*y, x^2 +6*y*z, 3*y^2 -3*z^2]

/**/  foreach g in J do
/**/    println g^2;
/**/  endforeach;
4*x^2*y^2
x^4 +12*x^2*y*z +36*y^2*z^2
9*y^4 -18*y^2*z^2 +9*z^4

See Also