[Prime]Passing lists as arguments



#2

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?


#3

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


#4

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.


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP Prime: Lists in programs Alberto Candel 7 535 12-04-2013, 02:16 AM
Last Post: Alberto Candel
  hp prime - programming with lists giancarlo 3 274 11-10-2013, 02:13 AM
Last Post: Giancarlo
  HP Prime SIZE and OBJ-> with matrices/vectors/lists Helge Gabert 8 419 09-27-2013, 05:44 PM
Last Post: Helge Gabert
  Nothing like passing the time away... Eddie W. Shore 2 182 03-21-2013, 08:23 AM
Last Post: Eddie W. Shore
  HP28C/S ROM entry point lists update Christoph Giesselink 0 152 01-31-2013, 02:21 PM
Last Post: Christoph Giesselink
  Invisible message passing Jim Yohe 4 244 04-04-2011, 02:15 PM
Last Post: Ken Shaw
  HP41 Mcode: SELPF arguments Meindert Kuipers 3 176 01-28-2010, 03:14 PM
Last Post: PeterP
  50g vector or complex arguments in exact form? Hal Bitton in Boise 2 150 01-30-2008, 03:25 AM
Last Post: George Bailey (Bedford Falls)
  Testing UserRPL arguments for numerosity? Sleazey 2 141 05-18-2007, 02:20 PM
Last Post: Sleazey
  Another example of passing off of MoHPC pictures on ebay Noname 1 133 02-22-2005, 05:59 PM
Last Post: hugh steers

Forum Jump: