Post Reply 
Simple programming help with the Prime
06-09-2014, 04:09 PM
Post: #1
Simple programming help with the Prime
I am new to PPL and the HP Prime. I am converting many programs from RPL on the HP 48GX and trying to move over to the HP Prime. I am having trouble accessing the "stack" in code. Does anyone know how to do this? I am sure it may be simple but I have yet to figure this one out. Any help would be greatly appreciated.

Thanks,
Ernesto
Find all posts by this user
Quote this message in a reply
06-09-2014, 04:39 PM
Post: #2
RE: Simple programming help with the Prime
There are currently no functions callable from PPL that will manipulate the stack. However, parameters to programs can be pulled from the stack. The syntax on the entry line is
Code:
function_name(n)
where "n" is the number of arguments that should be popped off the stack and provided to the program. The return value from the program also ends up on the stack. If you want to return multiple values, the only option today is to return a list.

Lack of integration between PPL and the stack is somewhat dismaying to me. In addition, the fact that the type system can't determine on its own how many arguments to pop is yet another symptom of incomplete integration.

- John
Find all posts by this user
Quote this message in a reply
06-09-2014, 05:13 PM (This post was last modified: 06-09-2014 05:14 PM by Tim Wessman.)
Post: #3
RE: Simple programming help with the Prime
(06-09-2014 04:39 PM)John R. Graham Wrote:  addition, the fact that the type system can't determine on its own how many

So how would you propose handling cases where you can have any number of arguments such as the MIN function which can take 1-16? Limit it to 2 and lose functionality? How would you avoid automatic evaluation when selecting it from a menu in RPN mode and it would automatically take 2 numbers instead of the 5 you wanted? What about in the CAS where something like sin(1,2,3,4) is a perfectly valid input?

It isn't that it wasn't possible, we just couldn't answer the questions to a level that didn't impede operation in one way or another or would cause problems with later work. I am truly happy if someone can answer everything in a way that doesn't eliminate useful capability while still making it more automated. We just didn't see a way. I'd be truly happy if more brains could figure it out and propose a path forward.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
06-09-2014, 05:55 PM
Post: #4
RE: Simple programming help with the Prime
So for example if I wanted to create a program to add the 2 recent values on the stack would be function_name(2)? When you say function_name(n) this is the actual command? Or is it some name of MY custom function?
Find all posts by this user
Quote this message in a reply
06-09-2014, 06:34 PM
Post: #5
RE: Simple programming help with the Prime
Tim,

To me, in the context of an RPN machine, the answers to these questions seem pretty obvious. Also obvious is that I don't understand all of the complexities you're operating under. In any case, fairly asked. Here are my answers:
(06-09-2014 05:13 PM)Tim Wessman Wrote:  So how would you propose handling cases where you can have any number of arguments such as the MIN function which can take 1-16? Limit it to 2 and lose functionality?
If the MIN function takes as an argument the number of objects to compare, then that number should be placed on the stack prior to invoking MIN. RPN is (almost completely) postfix and any temptation to make the operator infix under certain circumstances should be resisted. The current implementation seems to me to be more for the convenience of making the menus more similar between RPN and Algebraic entry modes.
(06-09-2014 05:13 PM)Tim Wessman Wrote:  How would you avoid automatic evaluation when selecting it from a menu in RPN mode and it would automatically take 2 numbers instead of the 5 you wanted?
Note that this is a problem that's caused because of the decision to include an operand after the operator as described above. If the case above were implemented more in the true spirit of RPN, then this problem vanishes.
(06-09-2014 05:13 PM)Tim Wessman Wrote:  What about in the CAS where something like sin(1,2,3,4) is a perfectly valid input?
Not trying to be too glib here, but since CAS is not RPN or stack oriented, this seems like it's outside the domain we were discussing. Not that I don't want a true RPN CAS mode, but then that probably obviates the needs for CAS vs. Home, doesn't it?

- John
Find all posts by this user
Quote this message in a reply
06-09-2014, 06:53 PM (This post was last modified: 06-09-2014 06:56 PM by eried.)
Post: #6
RE: Simple programming help with the Prime
(06-09-2014 06:34 PM)John R. Graham Wrote:  If the MIN function takes as an argument the number of objects to compare, then that number should be placed on the stack prior to invoking MIN. RPN is (almost completely) postfix and any temptation to make the operator infix under certain circumstances should be resisted. The current implementation seems to me to be more for the convenience of making the menus more similar between RPN and Algebraic entry modes.

100% agree with that. Using the number of args as an infix value is overly not-intuitive (not that RPN is intuitive for new users anyway). I am not sure if asking for the number of params as a stack argument is the solution either. Probably what the rpn mode needs is aliases for commands, like min takes 2, min3 takes 3, min4 ...

Right now, RPN in the prime is reduced to the same feeling has having toilet paper in the house storage. You don't use it, you don't need it right now but makes you feel ok that there is there.

My website: erwin.ried.cl
Visit this user's website Find all posts by this user
Quote this message in a reply
06-09-2014, 07:10 PM
Post: #7
RE: Simple programming help with the Prime
(06-09-2014 05:55 PM)Ernesto Wrote:  So for example if I wanted to create a program to add the 2 recent values on the stack would be function_name(2)? When you say function_name(n) this is the actual command? Or is it some name of MY custom function?
Yes, correct. Append the "(2)" after the function name. I meant "function_name" merely as a placeholder for the name of your custom function.

- John
Find all posts by this user
Quote this message in a reply
06-09-2014, 09:11 PM
Post: #8
RE: Simple programming help with the Prime
OK. So let's assume I make a program named "ADD". In code, can I set up the program to automatically take a number to append. Such as. ADD() would automatically assume the first to vales instead of saying ADD(2).

Also when I begin my program:

Export Add(x)
Begin;

End;

Does this program just know to take the values from the "stack" automatically? It seems like I would need to store them as some variable within the program so they may be manipulated as need be...
Find all posts by this user
Quote this message in a reply
06-09-2014, 09:22 PM
Post: #9
RE: Simple programming help with the Prime
Not sure exactly what you mean, but let's assume that your add program wants to take two arguments from the stack, add them together, leaving the sum on the stack, then the code woule look like this:
Code:
EXPORT ADD(X,Y)
BEGIN
  RETURN X+Y;
END;
and would be invoked as
Code:
ADD(2)
Does that make sense?

- John
Find all posts by this user
Quote this message in a reply
06-09-2014, 09:36 PM
Post: #10
RE: Simple programming help with the Prime
Yes it makes perfect sense. I am just trying to figure a clever way to run Add(x,y) without without typing the (x,y) value if that makes sense. Ultimately I would like to create a "shortcut" and assign it to one of the tabs at the top, maybe called ADD. It refers to a custom program Add(2) that takes the 2 values and adds them.

Maybe something like this:

Export Add()
Begin;
Add(2)
End;
Find all posts by this user
Quote this message in a reply
06-09-2014, 09:46 PM (This post was last modified: 06-09-2014 09:51 PM by Didier Lachieze.)
Post: #11
RE: Simple programming help with the Prime
(06-09-2014 09:22 PM)John R. Graham Wrote:  Not sure exactly what you mean, but let's assume that your add program wants to take two arguments from the stack, add them together, leaving the sum on the stack, then the code woule look like this:
Code:
EXPORT ADD(X,Y)
BEGIN
  RETURN X+Y;
END;
and would be invoked as
Code:
ADD(2)
Does that make sense?

- John

It seems there has been some changes with the latest firmware update because now in RPN mode you don't need anymore to specify the number of arguments when calling a User program, with your program example:
Code:
2 ENTER 3 ADD() ENTER
returns 5.

However in algebraic mode you still need to call your program with:
Code:
ADD(2,3) ENTER
to get 5.
Find all posts by this user
Quote this message in a reply
06-09-2014, 09:47 PM (This post was last modified: 06-09-2014 10:01 PM by John R. Graham.)
Post: #12
RE: Simple programming help with the Prime
(06-09-2014 09:36 PM)Ernesto Wrote:  Yes it makes perfect sense. I am just trying to figure a clever way to run Add(x,y) without without typing the (x,y) value if that makes sense. Ultimately I would like to create a "shortcut" and assign it to one of the tabs at the top, maybe called ADD. It refers to a custom program Add(2) that takes the 2 values and adds them.

Maybe something like this:

Export Add()
Begin;
Add(2)
End;
Alas, such shortcut soft key assignments are not yet available to users[1], although they make so much sense, I do expect them to show up at some point.

- John

1. Except through brute force manual graphical design / touch screen interpretation. Even then, you have to be running the program that does all that display & touch work: it's not possible to make it available from Home.
Find all posts by this user
Quote this message in a reply
06-09-2014, 10:04 PM (This post was last modified: 06-09-2014 10:22 PM by John R. Graham.)
Post: #13
RE: Simple programming help with the Prime
(06-09-2014 09:46 PM)Didier Lachieze Wrote:  It seems there has been some changes with the latest firmware update because now in RPN mode you don't need anymore to specify the number of arguments when calling a User program, with your program example:
Code:
2 ENTER 3 ADD() ENTER
returns 5.
Didier, thank you. That's good to know (and a distinct improvement, in my opinion).

- John
Find all posts by this user
Quote this message in a reply
06-09-2014, 10:28 PM
Post: #14
RE: Simple programming help with the Prime
(06-09-2014 10:04 PM)John R. Graham Wrote:  Didier, thank you. That's good to know. Did you see that documented somewhere? I had to be told about the "argument count" convention by other forum members as there didn't appear to be any official documentation of the mechanism.

- John

See the HP Prime User Guide pages 49-50 "Sample calculations", it explains why the argument count is required for functions that have a variable number of arguments such as MIN. I have not seen any other reference to required argument count in RPN mode.
Find all posts by this user
Quote this message in a reply
06-09-2014, 11:26 PM
Post: #15
RE: Simple programming help with the Prime
Thanks. I must've missed that. But in a conversation I had with Han, I think he implied that the argument count was mandatory on all functions that took any arguments.

- John
Find all posts by this user
Quote this message in a reply
06-10-2014, 09:52 AM
Post: #16
RE: Simple programming help with the Prime
(06-09-2014 11:26 PM)John R. Graham Wrote:  Thanks. I must've missed that. But in a conversation I had with Han, I think he implied that the argument count was mandatory on all functions that took any arguments.

- John

It's mandatory only on those functions that take a variable number of arguments. If a function always takes n arguments, then n need not be specified.

It's kinda similar to RPL: DUP2 always takes 2 arguments, so you don't need to tell it to take 2 arguments. But DUPN takes n arguments, so you must specify n. In PPL, ABS always takes 1 argument, so you don't need to tell it to take 1 argument. But the MAX command can take many arguments, so MAX should be thought of as MAXN when you're in RPL mode, because you have to specify n.

Hope that helps!

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
06-10-2014, 09:59 AM (This post was last modified: 06-10-2014 11:23 AM by Angus.)
Post: #17
RE: Simple programming help with the Prime
I tried to program an adoption of dealing with different number bases. Inspired by the hp42c menu.
What I ended up with about stack manipulations is something like the following as a return value:

In a program
(...)
RETURN " 32 4 R→B(3)"; //hex interpretation
(...)

That works. But not really satisfying. Behaviour seems to have changed after the latest patch. I opened a post, but there were no answers. Maybe I will split it up into smaller subposts.

If you enter 123 <enter> and press the user key (actually I'm working on soft-menu hence my request for static softkey assignment) that does the 2hex command you have 123 on the stack and 32 4 R->B(3) on the entry line. So you have to confirm with enter again and it is done.

You can in a way 'put' data on the stack as a string like this. But that is far from RPN entry. :-(
How can you pass the entry line to the parser from a program? Having to press enter is nonsense rpn-wise?
Find all posts by this user
Quote this message in a reply
06-10-2014, 11:49 AM (This post was last modified: 06-10-2014 12:04 PM by Didier Lachieze.)
Post: #18
RE: Simple programming help with the Prime
(06-10-2014 09:59 AM)Angus Wrote:  In a program
(...)
RETURN " 32 4 R→B(3)"; //hex interpretation
(...)

I may not understand what your program is trying to accomplish but maybe you can use something like that:
Code:
EXPORT tohex(X)
BEGIN
  RETURN (R→B(X,32,4));
END;

then in RPN interactive mode: 123 [ENTER]* tohex() will return #7Bh
in algebraic interactive mode: tohex(123) will return #7Bh
at the end of a user program: RETURN tohex(123); will return #7Bh

* you may skip ENTER, 123tohex() on the command line works as well
Find all posts by this user
Quote this message in a reply
06-10-2014, 02:10 PM
Post: #19
RE: Simple programming help with the Prime
I was trying to use the Stack.
Your code works nicely when calling tohex() as written, thank you! I will forget my way in the future.
I try to call tohex() from within an other program I have problems. I want to launch a menufor conversion but suppose I just wanted to have a user key on shift+minus for hex conversion.

KEY KS_Minus()
BEGIN
LOCAL reval;
retval:=tohex(1); //this gives an error when called without number of pops retval();
RETURN retval;
END;

However if I hit shift+minus the function app is started... Any ideas what I am doing wrong?
Find all posts by this user
Quote this message in a reply
06-10-2014, 04:39 PM (This post was last modified: 06-10-2014 09:46 PM by Didier Lachieze.)
Post: #20
RE: Simple programming help with the Prime
(06-10-2014 02:10 PM)Angus Wrote:  KEY KS_Minus()
BEGIN
LOCAL reval;
retval:=tohex(1); //this gives an error when called without number of pops retval();
RETURN retval;
END;

However if I hit shift+minus the function app is started... Any ideas what I am doing wrong?

What happens is that tohex(1) returns #1h as 1 is not considered as an argument count but as a parameter for tohex().

Normally, according to the HP Prime User Guide when you define a USER Key the RETURN statement should be followed by a string containing the command to be put on the command line. (Btw it seems there is no way to execute automatically the string once it’s on the command line).

What’s interesting in your example is that retval is not a string but the number '1' and RETURN retlval; is activating the Symb function as if you had pressed the Symb key.

I’ve done a few tests and each key has a corresponding number which reproduces the key press including the execution of the function:
  • 0 to 50 for the unshifted key
    • 0 for the Apps key
    • 1 for the Symb key
    • 5 for the Home key
    • 6 for the Plot key
    • ..
    • 10 for the CAS key
    • 14 for the Vars key
    • ..
    • 50 for the + key,
  • 51 to 101 for the shifted keys
    • 51 for the Info key
    • 101 for the Ans key
  • 102 to 152 for the Alpha keys (upperscript)
    • 116 for the A key
    • ….
    • 152 for the ; key
  • 153 to 203 for the Alpha keys (lowerscript)
    • 167 for the a key
    • ….
    • 203 for the ; key

I don't know if that can be useful in any way ...
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)