How to make multi-line nesting for loops in PARI / GP?

How can I make nested loops in PARI / GP that span multiple lines at each level? I often have to do several things inside loops, and for readability, I don't like to write my loops on one line. For a loop over a single variable, I did this:

for(i=1,10,{ printf("%u\n",i); }) 

However, for nested loops, I only managed to split the lines at the same level. It works:

 for(i=1, 10, for(j=1, 10, { printf("%2u\t%2u\n", i, j); })); 

This also works:

 for(i=1, 10, { for(j=1, 10, printf("%2u\t%2u\n", i, j)); }); 

However, this is what I really would like to do:

 for(i=1, 10, { for(j=1, 10, { printf("%2u\t%2u\n", i, j); }); }); 

This last example does not work; he gives an error:

  *** sorry, embedded braces (in parser) is not yet implemented. ... skipping file 'nested_for.gp' *** at top-level: printf("%2u\t%2u\n", *** ^-------------------- *** printf: not a t_INT in integer format conversion: i. *** Break loop: type 'break' to go back to GP 

I am using PARI / GP 2.5.3 for OS X 10.8.3. I write my scripts to the nested_for.gp file and run them with gp ./nested_for.gp in Bash.

+6
source share
1 answer

Unlike what we expect from type C syntax, curly braces do not define a block in the GP. They allow you to divide a sequence of instructions into several consecutive lines. They do not nest; on the other hand, you can set loops inside one {} block:

 { for (i = 1, 10, for (j = 1, 10, print (i+j))) } 

Multi-line commands are usually found in user-defined functions and may look more natural in this context:

 fun(a, b) = { for (i = 1, a, for (j = 1, b, print (i+j))); } 
+9
source

All Articles