%mathpiper,def="Product"

Function("Product",{sumvar,sumfrom,sumto,sumbody})
[
   Local(sumi,sumsum);
   sumsum:=1;
   For(sumi:=sumfrom,sumi<=sumto And sumsum!=0,sumi++)
       [
        MacroLocal(sumvar);
        MacroBind(sumvar,sumi);
        sumsum:=sumsum*Eval(sumbody);
       ];
   sumsum;
];
UnFence("Product",4);
HoldArgument("Product",sumvar);
HoldArgument("Product",sumbody);

Product(sumlist_IsList) <--
[
   Local(sumi,sumsum);
   sumsum:=1;
   ForEach(sumi,sumlist)
   [
     sumsum:=sumsum*sumi;
   ];
   sumsum;
];

%/mathpiper



%mathpiper_docs,name="Product",categories="User Functions;Series"
*CMD Product --- product of a list of values
*STD
*CALL
	Product(list)
	Product(var, from, to, body)

*PARMS

{list} -- list of values to multiply

{var} -- variable to iterate over

{from} -- integer value to iterate from

{to} -- integer value to iterate up to

{body} -- expression to evaluate for each iteration

*DESC

The first form of the {Product} command simply
multiplies all the entries in "list" and returns their product.

If the second calling sequence is used, the expression "body" is
evaluated while the variable "var" ranges over all integers from
"from" up to "to", and the product of all the results is
returned. Obviously, "to" should be greater than or equal to
"from".

*E.G.

In> Product({1,2,3,4});
Result: 24;
In> Product(i, 1, 4, i);
Result: 24;

*SEE Sum, Apply
%/mathpiper_docs