> < ^ Date: Thu, 13 Mar 2003 11:00:26 -0500 (EST)
< ^ From: Sven Reichard <reichard@math.udel.edu >
< ^ Subject: Re: Programs

GAP-forum

On Wed, 12 Mar 2003, Greg Gamble wrote:

GAP-forum
On Wed, Mar 12, 2003 at 10:31:56AM -0500, Vahid Dabbaghianabdoly wrote:
> Dear Gap-Froum,
>
> I have written a recursive program on GAP which contains two
> subroutines A and B such that A calls B, and B calls A.
> When I define A at the first, GAP does not know B.
> and when I define B, it does not know A. And I receive
> "Syntax error: warning: unbound global variable".
> Do you know How I can fix that?

Interactively, you can safely ignore it ... it is just a warning.

However to define two functions f and g that call each other in
such a way that avoids the warning use DeclareGlobalFunction and
InstallGlobalFunction to define each function like this:

DeclareGlobalFunction( "f" );
DeclareGlobalFunction( "g" );

InstallGlobalFunction( f,
function(...)
# code here can refer to g
end );

InstallGlobalFunction( g,
function(...)
# code here can refer to f
end );

Regards,
Greg Gamble

The problem with that approach occurs when you incrementally develop and
debug you code, since Install... makes f and g read-only variables. If I
remember correctly, if you make changes to your code, you need to restart
Gap.

Another work-around that I have employed is the following:

A := fail; # or any other value

B := function()
  # uses A
end;

A := function()
  # uses B
end;

This avoids the warning, and still lets you overwrite A and B. In
production code, you might want to replace this by Greg's code.

Regards,
Sven.

--
Sven Reichard
Dept. of Math. Sci.
University of Delaware
reichard@math.udel.edu


> < [top]