What is RPL?

Discussions about the RPL and RPL+ programming languages

What is RPL?

Postby oliver » Wed Jan 19, 2011 9:01 am

RPL is the principal programming language built into the hp 50g calculator, and three lines of classic HP calculators: HP-28, HP-48, HP-49.

RPL is available in ND1 (and upcoming MorphEngine-based CalcPad) as a supported language, alongside JavaScript.

If you know how to use the calculator in RPN mode, you already know how to do some RPL programming. The language, in its simplest form, is a plain textual sequence of RPN commands, as manually performed, with two characters ('≪' and '≫') to delimit a "program". In that simplest sense, RPL is a kind of replay mechanism that allows you to automate manual steps.

The language offers more than that, though. It adds the control structures of most programming languages: conditional execution (IF-THEN-ELSE), and loops (START, FOR, DO-UNTIL, WHILE-REPEAT). It also adds local variables, execution of itself, and a number of powerful "list processing functions" (DOLIST, DOSUBS, MAP, SEQ, STREAM).
RPL is built around the concept of a stack, where the inputs to commands (often called "functions" in other languages) come from, and results are deposited. Another important concept is that all data is untyped. RPL is, in part, "object-oriented".

For more information, see
http://en.wikipedia.org/wiki/RPL_%28programming_language%29
http://naivedesign.com/ND1/ND1_Reference__RPL.html

Examples:

Code: Select all
≪ 5 5 + 5 / ≫

enters two numbers on the stack, adds them, enters another number and divides. The result, 2, will be placed on the stack.

Code: Select all
≪ "APPL" Stocks.quote ≫

enters a string ("APPL") on the stack and then executes the command/function "quote" from the Stocks folder.

Code: Select all
≪ 1 + ≫

enters "1" on the stack and then adds it to whatever is at position one of the stack, and places the result on the stack. The stack item comes in implicitly because "+" is an operator that happens to take two inputs, and all inputs come from the stack. If the stack item were a number (Real, Complex, BigNum, or Binary), the number plus one would be returned. If it were a Vector (or List/Array), the number "1" would be appended as a new element. If it were another kind of object, "1" would be added in whatever way the "+" operator for that object specifies, or an error would be issued, if the object had no support for this operator.

If you're a beginning programmer, RPL is a very easy language to pick up. Know, though, that it's a language unique to calculators and that you won't be getting use out of it outside of MorphEngine and HP calculators.

In MorphEngine, RPL and JavaScript can call each other seamlessly and share the same data types. This permits using each language to its strengths.

If you're coming from JavaScript, you may want to think of RPL as a sometimes higher level language, that's good for two things:
- scripting JavaScript: write short "scripts" that call upon built-in commands or your own JavaScript functions
- writing more compact and sometimes more readable code by exploiting the untyped characteristics of the language; for example, "1 +" adds "1" to any kind of object, whereas in JavaScript you need to know what type of object you're adding to and write appropriate code. (To add "1" to a BigNum "x", you'd have to write "BigNum["+"](x, 1)", for example.)

RPL is only more high-level than JavaScript when it comes to types. For general algorithmic development, RPL may feel like a bare-bones stack machine, with JavaScript being a far more expressive (and readable) choice.

Note on speed:

In MorphEngine, RPL is implemented through a byte-code compiler that will eventually execute JavaScript code. The implementation is faster than emulation of the original Saturn hardware, an approach taken by emulators and HP's own 49-line and the 50g. Yet, RPL is usually quite a bit slower than JavaScript.
The speed difference ranges anywhere from a factor of 1 to 1000, depending how you use the languages.

RPL programs that spend most of their time in commands, vs. control structures of the language and stack operations, run almost as fast as equivalent JavaScript code.
For example, this code
Code: Select all
≪ INV ≫

to invert a matrix from the stack, runs just as fast as the equivalent JavaScript code
Code: Select all
function(m) { return matrix.inv(m); }

for a larger matrix.
Even complex code may run at comparable speed, as long as the expense is in commands.
Know, though, that long programs are typically easier to read and maintain in JavaScript.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to RPL / RPL+

Who is online

Users browsing this forum: No registered users and 1 guest

cron