[GAP Forum] A strange error
Burkhard Höfling
burkhard at hoefling.name
Mon Jul 16 21:49:24 BST 2007
Dear Mathieu,
> Dear Gap forum,
> see below a program that creates a strange error.
> ---------------------------------------------
> n:=4;
> for i in [1..n]
> do
> eMat:=4;
> H:=List([1..n], x->x*i*eMat);
> od;
> ---------------------------------------------
> running it with "gap.sh TheProg" produce the following message:
> .
> .
> Syntax error: warning: unbound global variable in BugSearch line 5
> H:=List([1..n], x->x*i*eMat);
> ^
this is absolutely normal and intended behaviour. Please note that
this is only a warning and your code is absolutely correct.
What happens is the following. GAP first reads (parses) the entire
`for' loop before executing it. While reading the GAP function x-
>x*i*eMat, it discovers that the variable `eMat' is (yet) undefined
and produces a warning. After the `for' loop has been read, execution
starts. Of course, the assignment `eMat:=4' takes place before eMat
is needed in the function, so that the code runs correctly.
> The manual seems silent on the subject, but after a bit of
> fiddling, I will guess that it's a funny sort of scoping problem.
> Although scoping seems to be limited to "global" and "local
> (function)", it might be that there is block scoping as well.
> Since you are introducing 'eMat' within the "scope" of the for
> loop, perhaps the variable name 'eMat' does not actually become
> known until the loop is (textually) complete.
There is no block scoping in GAP; in particular, any global variable
defined in a for loop remains defined outside the loop.
The reason for this behaviour is that in most cases, the warning
occurs if one mis-types the name of a variable. In this way, such an
error can be spotted before the function is being run.
In order to avoid this problem, simply define the variable before the
`for' loop (by assigning a more or less meaningful value). In your
case, you could even move the line `eMat := 4;' before the loop.
Cheers,
Burkhard.
More information about the Forum
mailing list