Bill Allombert on Tue, 16 Apr 2024 23:15:50 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: simple question on why indice variables are not counted as variables for gp2c-run command


On Tue, Apr 16, 2024 at 12:20:08PM -0700, American Citizen wrote:
> I have a simple question here,
> 
> Let test_fun.gp be a simple script file (basically doing nothing)
> 
> {test_fun(n)=
> local(i,j,x);
> for(i=1,n-1,
>  for(j=i+1,n,
>    x=x;
>  );
> );
> return(0);
> }
> 
> Is the c-compiler purposefully ignoring the i,j as index variables in the
> loop?

In PARI/GP, doing for(i=... actually declares i as a local variable to the loop.
so local(i) is declaring another variable i, which is not used.

Try this example;

? fun(x)=my(i=0);for(i=1,x,if(i^2>x,print(i);break));i;
? fun(100)
11
%4 = 0

Also I advise to use my() instead of local() unless you have specific reason.
my() variable are statically-scoped, which is less likely to cause bugs.

Cheers,
Bill