There is however a more comfortable way to compute the product of a list of numbers or permutations.
gap> Product([1..15]);
1307674368000
gap> Product(pp);
(1,8,4,2,3,6,5)
The function Product takes a list as its argument and computes the
product of the elements of the list. This is possible whenever a
multiplication of the elements of the list is defined. So Product is
just an implementation of the loop in the example above as a function.
There are other often used loops available as functions. Guess what the
function Sum does. The function List may take a list and a function
as its arguments. It will then apply the function to each element of the
list and return the corresponding list of results. A list of cubes is
produced as follows with the function cubed from About Functions.
gap> List([2..10], cubed);
[ 8, 27, 64, 125, 216, 343, 512, 729, 1000 ]
To add all these cubes we might apply the function Sum to the last
list. But we may as well give the function cubed to Sum as an
additional argument.
gap> Sum(last) = Sum([2..10], cubed);
true
The primes less than 30 can be retrieved out of the list primes from
section About Lists by the function Filtered. This function takes
the list primes and a property as its arguments and will return the
list of those elements of primes which have this property. Such a
property will be represented by a function that returns a boolean value.
In this example the property of being less than 30 can be reresented by
the function x- x < 30 since x < 30 will evaluate to true for
values x less than 30 and to false otherwise.
gap> Filtered(primes, x-> x < 30);
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
Another useful thing is the operator { } that forms sublists. It takes
a list of positions as its argument and will return the list of elements
from the original list corresponding to these positions.
gap> primes{ [1 .. 10] };
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]
In this section you have seen some functions which implement often used
for loops. There are functions like Product to form the product of
the elements of a list. The function List can apply a function to all
elements of a list and the functions Filtered and Sublist create
sublists of a given list.
You will find more predefined for loops in chapter Lists.
GAP 3.4.4