HP Forums

Full Version: [Prime]Passing lists as arguments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I'm trying to create some functions to manipulate Quaternions which are complex numbers with 4 dimensions. I chose a list as my base implementation type. I'm encountering a behaviour I don't understand--passing one list works okay, but passing two lists does not.

This function works:

EXPORT Q_CAST(a)
BEGIN
CASE
IF type(a)=6 THEN RETURN a; END;
IF type(a)=0 THEN RETURN {a,0,0,0}; END;
IF type(a)=3 THEN RETURN {RE(a),IM(a),0,0}; END;
IF TYPE(a)=4 THEN RETURN {a[0],a[1],a[2],a[3]}; END;
DEFAULT
RETURN EXPR(a);
END;
END;

If I call Q_CAST(4), I get {4,0,0,0}; Q_CAST(1+2*i) = {1,2,0,0}; and QCAST({1,2,3,4}) = {1,2,3,4}. All as I would expect.

However, this function does not work.

EXPORT Q_Add(a,b)
BEGIN
LOCAL m,n;
m:=Q_CAST(a);
n:=Q_CAST(b);
RETURN {m[0]+n[0],m[1]+n[1],m[2]+n[2],m[3]+n[3]};
END;

Any time I try to pass a list to Q_Add, it fails like so:

Q_Add({1,2,3,4},{2,3,5,9})
Error:Invalid Input

Starting it in the debugger, I get two fields, a and b, but I am unable to start debugging if I attempt to pass a list as an argument to either. Debugging Q_CAST does not let me pass a list either, but it seems to work just fine from the command-line. (Which does seem like a separate type of bug--shouldn't you be able to debug programs that don't take numerical arguments?)

Both of these are subroutines with a program I've called Quaternion, so I would expect that they have the exact same environment. I have also tried using different parameter and variable names, including uppercased names--to no avail. I've also tried assigning an empty list to m or n first, but that didn't work. I don't know if I'm even getting that far.

It would seem logical that a program argument would override the scope of any global variables, so I'm not surprised that changing the names didn't solve the problem. However, the Prime seems to be full of non-orthogonal behaviour or otherwise hidden quirks that trip up programmer types like myself. For example Home vs CAS, Textbook vs RPN, Global vars with specific types, etc, etc. (For the record, I'm trying to use using Home mode with textbook entry. As non-RPN doesn't allow operator overloading, RPN would be definitely better for operating on constructing data types because you can naturally use arithmetic-type operators. I'm avoiding it for now because I don't want to introduce any more issues and RPN mode seems to have its own share of pitfalls.)

Is passing lists as arguments known to have issues, or is there some "magic" that is needed to make this work?

hello

1 indexed lists got you! the following program works fine...

EXPORT Q_Add(a,b)
BEGIN
LOCAL m,n;
m:=Q_CAST(a);
n:=Q_CAST(b);
RETURN {m[1]+n[1],m[2]+n[2],m[3]+n[3],m[4]+n[4]};
END;

have fun,

Cyrille

That's the problem!

If I might, here are a couple suggestions for the next Prime update that would tremendously help diagnosing situations like this:

1) Return "Array index error" instead of "invalid input" as an error in this situation
2) If a function fails, report the line number that it fails on
3) Allow the debugger to pass non-numeric arguments to a function.

Any one (but all three especially) would have helped me figure this out on my own.

Thank you Cyrille.