> < ^ Date: Tue, 20 Sep 1994 22:17:00 -0700 (PDT)
> < ^ From: Martin Schoenert <martin.schoenert@math.rwth-aachen.de >
< ^ Subject: Re: MapByFunction
```Chris Wensley writes in his e-mail message of 1994/09/20
```
```It seems reasonable to hope that  MappingByFunction  can be used
inside a loop to produce a list of homomorphisms.  However, it
appears that the definition of  f(x)  may depend only on  x  and
constants, and not the loop variable or associated parameters.
Is there a way round this?
The following listing illustrates the problem.
----------------------------------------------------------------
gap> c6 := Group( (1,2,3,4,5,6) );;
gap> c6.name := "c6";;
gap> pow := 0 * [1..6];;
gap> for i in [1..6] do pow[i] := MappingByFunction( c6, c6, x->x^i ); od;
['pow' now contains 6 mappings that all raise the argument to the
same power 'i']
```

This is a tricky issue, so pardon me if I write something about it, even

You write ``... depends ... not on the loop variable''.
In the fact the problem is that all function depend on the loop variable,
with the emphasize being on the *variable*. There is only one variable,
which may refer to different values over the time, but it is always the
same variable. And since there is only one variable, all six functions
are equal (even though GAP's '=' operator does not see this).

Put in another way. When the function 'x->x^i' is evaluated, which
happens once for every iteration through the loop, the *identifier* 'i'
is mapped to the global variable with that identifier, since the
statement does not appear in a function definition with a local variable
with identifier 'i'. But the body of the function 'x->x^i' then refers
to that variable, which is the same variable on each iteration, not to
its current value, which is different for each iteration.

(Dangerous Bend! Actually the mapping from identifier to variable
happens when the function literal is read, and the function creation
during evaluation augments the reference to the variable with a so called
*context*, but this is only of technical interest).

Later when such a function is called, e.g., when one of the mappings
is applied, the value of that the global variable with identifier 'i'
refers to *at this moment* is used.

How can we solve your problem. First we must somehow force evaluation of
the loop variable 'i'. Second because we want 6 different functions, we
need 6 different *variables*, which we must somehow create dynamically.
Fortunately this is possible, because each function call creates a new
variable for each formal argument and each formal local variable.

```gap> PowerFunction := function ( p )  return x -> x^p;  end;
gap> for i  in [1..6]  do
>        pow[i] := MappingByFunction( PowerFunction(i) );
>    od;
```

Lets take a look at what happens during each loop iteration.

1) the global variable 'i' is evaluated to its value.

2) a new variable for the formal argument 'p' of 'PowerFunction' is created.

3) the value computed in step 1) is assigned to this variable.

4) the function 'x->x^p' is created, it refers to the newly created
variable, which refers to the current value of the loop variable
(and will so forever).

5) this function is passed to 'MappingByFunction', which creates the
corresponding mapping.

I hope this helps somewhat.

Martin.

```-- .- .-. - .. -.  .-.. --- ...- . ...  .- -. -. .. -.- .-
Martin Sch"onert,   Martin.Schoenert@Math.RWTH-Aachen.DE,   +49 241 804551
Lehrstuhl D f"ur Mathematik, Templergraben 64, RWTH, 52056 Aachen, Germany
```

> < [top]