hermann on Sun, 13 Aug 2023 20:44:02 +0200


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

Re: New gp features


On 2020-02-12 20:14, Bill Allombert  wrote:
Dear PARI developers,

We added two GP language features:

1) A new command foreach:
foreach(V,X,seq):
...

2) holes in multi assignement:
? [a,,c]=[1,2,3]
...

I searched for "foreach" and found it in above email.
It was not in my PARI refcard (I had old version, now I use newest).

Regarding holes in multi assignments, nice concept!
Even more in that holes can be on the right side with missing commas.
The vectors in vector "rsa" are of length 6 (factored RSA number
[l,n,p,q,pm1,qm1] for n=RSA-l=p*q, Xm1 prime factorizaton of X-1).
Or length 2 for not yet factored RSA numbers [l,n].

pi@pi400-64:~/RSA_numbers_factored/pari $ gp -q
? \r RSA_numbers_factored.gp
validate(rsa): ✓
? foreach(rsa,t,[l,n]=t;printf("%d ",l))
59 79 100 110 120 129 130 140 150 155 160 170 576 180 190 640 200 210 704 220 230 232 768 240 250 260 270 896 280 290 300 309 1024 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 1536 470 480 490 500 617 2048
?


Conditional statement does allow to deal with too short vectors:

? foreach(rsa,t,if(#t<4,next);[l,n,p,q]=t;if(p%4==1 && q%4==1,printf("%d ",l)))
59 129 180 230 768
?


Few questions:

A) foreach cannot be redefined?

foreach2(a,b,c) = foreach(a,b,c);
? foreach(rsa,t,printf("%d ",t[1]));
59 79 100 110 120 129 130 140 150 155 160 170 576 180 190 640 200 210 704 220 230 232 768 240 250 260 270 896 280 290 300 309 1024 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 1536 470 480 490 500 617 2048
? foreach2(rsa,t,printf("%d ",t[1]));
  ***   at top-level: ...ach2(rsa,t,printf("%d ",t[1]))
  ***                                             ^-----
  ***   incorrect type in _[_] OCcompo1 [not a vector] (t_POL).
  ***   Break loop: type 'break' to go back to GP prompt
break>


B) foreach variable cannot be multi assignment tuple?

? foreach(rsa,[l,n],printf("%d ",l));
  ***   variable name expected: foreach(rsa,[l,n],printf("%d ",l));
  ***                                       ^-----------------------
?


C) holes only allowed on LHS, not on RHS?

? [a,b,c] = [1,2]
  ***   at top-level: [a,b,c]=[1,2]
  ***                      ^--------
  ***   nonexistent component: index > 2
  ***   Break loop: type 'break' to go back to GP prompt
break>

workaround:

? rhs=[1,2];
? [a,b,c]=concat(rhs,vector(3,x,-1));
? c
-1
?


Regards,

Hermann.