[GAP Forum] Input stream inside a function

Max Neunhoeffer neunhoef at mcs.st-and.ac.uk
Fri Apr 18 12:03:58 BST 2008


Dear Anvita,

On Fri, Apr 18, 2008 at 04:24:28PM +0700, Anvita wrote:
> Dear Forum,
> 
> Please, explain to me why the following function does not work:
> 
> -------------------------------
> F:=function(a)
> local b;
> Read(InputTextString("b:=a;"));
> return b;
> end;
> -------------------------------
The Read statement in GAP always evaluates the things read in the
global context (and not in the context of the function where it is
called). This means, that your file will try to access the *global*
variable called "a" and assign things to the *global* variable
called "b".

This is documented in the manual section about "Read" in the little
sentence:

"The reading and evaluations happens exactly as described for the main
loop (see 6.1)."

Maybe we should make this clearer in the manual.

Best regards,
  Max.
> 
> I expected that, for example, F(1) would equal 1.
> However, GAP returns an error:
> 
> ----------------------------------------------------
> gap> F(1);
> Variable: 'a' must have a value
> 
> Variable: 'b' must have an assigned value at
> return b;
>  called from
> <function>( <arguments> ) called from read-eval-loop
> Entering break read-eval-print loop ...
> you can 'quit;' to quit to outer loop, or
> you can 'return;' after assigning a value to continue
> -----------------------------------------------------
> 
> The code does work in the main read-eval-print loop, though:
> 
> ------------------------------------
> gap> a:=1;
> 1
> gap> Read(InputTextString("b:=a;"));
> gap> b;
> 1
> ------------------------------------
> 
> Thank you,
> Anvita
> 
> _______________________________________________
> Forum mailing list
> Forum at mail.gap-system.org
> http://mail.gap-system.org/mailman/listinfo/forum

-- 
Max Neunhoeffer              http://www-groups.mcs.st-and.ac.uk/~neunhoef/
> > > > > > > > > > >  May the Source be with you! < < < < < < < < < < < < 
The University of St Andrews is a registered Scottish charity: No SC013532



More information about the Forum mailing list