Loop Primitives

There are a few primitives designed to perform typical loop functions that may be useful in place of one of the loop statements. For example, the following statement is functionally identical to the code above:

var total=sigma(n*n,n,1,10);

The sigma primitive performs a summation equivalent to the mathematical expression

See sigma.

That is, the expression

sigma(f(x),x,x1,x2)

is equivalent to

{
  var total=0,x;
  for(x=x1; x<=x2; x++)
    total+=f(x);
  total;
}

Another important loop primitive is makelist. While sigma returns the sum of distinct values created inside a loop, makelist returns a list containing each value.

See makelist.

For example, the expression:

makelist(x*x,x,1,10)

returns the list

[1,4,9,16,25,36,49,64,81,100]

More specifically, the expression

makelist(f(x),x,x1,x2)

is equivalent to

{
  var list=null,x;
  for(x=x1; x<=x2; x++)
    list=list!!f(x);
  list;
}

The primitive each is similar to makelist except each allows you to provide a list of specific input values for x rather than supply start and end values.

See each.

That is, the expression

makelist(f(x),x,x1,x2)

is the same as

each(f(x),x,x1..x2)

See About the For Statement.