I want to write a function SWAP to swap 2 integers a and b in PARI. This function does not return any values but I want the variables outside the function to store the new values. I don't know how to fix this. This is my function:
swap(a,b) = { t=a; a=b; b=t;}
Then I write:
u=2; v=3;
swap(u,v);
ten u is still 2 and v is still 3. How can I rewrite this function to make u=3 and v=2? Thank you.
Unfortunately, you cannot implement such a swap
function for scalar variables. Starting from PARI/GP v2.13.0 you can pass the container variables (like list, vector, etc.) by reference to user-defined functions using ~
prefix in order to modify its components. So you can do simple work-around like the example below for PARI/GP of version >= 2.13.0:
swap(~a,~b) = {[a[1],b[1]] = [b[1],a[1]];};
u = [2]; v = [3];
swap(~u, ~v);
u
> [3]
v
> [2]