# HP Forums

Full Version: Immediate algebraic expressions in RPL+
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

In RPN/RPL, you work from the "inside out" or from the "bottom up". In algebraic notation, you take a "top down" approach.

Whether you write

Algebraic: total(squared(divs(x)))
or
RPL+: x divs squared total (RPL: x DIVIS SQ SIGMALIST)
comes arguably down to preference.

Algebraic has in its favor, that it matches natural language:

English: "Sum of squares of the divisors of a number."

RPN has in its favor, that it matches recipe-style step-by-step processing:

English: "Compute divisors for x. Square them. Sum them up."

Let's suggest, that no one is better or worse than the other.

When it comes to algebraic expressions involving operators, RPL doesn't look so good, though, I'd say.

Can you comprehend

1 3 a * 4 b * + 5 c * +
or
1 3 a * 4 b * 5 c * + +
at a glance?

How about the algebraic equivalent of

3*a+4*b+5*c
?

The situation becomes worse when expressions are consumed by a function or construct that takes multiple arguments.

Consider

1 b 4 + x - FOR i ...

You can't glance where start and end value computations begin and end, without backtracking.

Consider these variations of using a comparison operator:

IF x SQ y == THEN
x SQ y == IF THEN
x SQ y IF == THEN
x SQ IF y == THEN

It's not hard to argue that, in these cases, the RP notation comes at the expense of readability.

Now. RPL calcs support algebraic expressions via a data type.
You could write

IF 'SQ(x)==y' EVAL THEN ...

What if you could type

IF SQ(x)==y THEN ...
?

I'm working on supporting "immediate algebraic expressions" in RPL+, which permit just that.

You may write

expr
'expr' EVAL

This pulls all of algebraic notation into RPL, and (gasp!) merges them.

An example:

1: 1 size(arr)-n FOR i
2: arr[i-n/2] divs squared total =x
3: IF SQ(x)==y THEN BREAK END
4: NEXT

This is not a particularly instructive example, but here're some notes:

Line 1: The algebraic expression size(arr)-n segments what comes before FOR into two blocks, and makes the calculation more readable

Line 2: pure RPL+ with an algebraic in the array-access operator (=x is equivalent to 'x' STO)

You could write this line also entirely in algebraic notation:

x=total(squared(divs(arr[i-n/2])))
Line 3: Algebraic makes the comparison more readable

As with every other RPL+ feature, you may use it, or not.

Am I instigating a forbidden love here, that should better remain unconsummated? Your opinions are welcome.

Hi Oliver,

I'd say do it. To me, RPN is good for keying in the solution to problems, but when it comes to *reading* expressions, I'd much rather see algebraic since that's how they always appear in books. To me, the ability to read a program is just as important as the ability to have the program produce the correct answer. After all, if you can't understand the code, then bugs are highly likely. So I think algebraic expressions in the programming language are a good thing.

Dave

Quote:
In RPN/RPL, you work from the "inside out" or from the "bottom up". In algebraic notation, you take a "top down" approach.
The comparison I made to a co-worker was that algebraic tells what you get, whereas RPN tells how you get there. That little piece of info got him interested in the HP-50g. He bought one and now he's absolutely nuts about it. He can't say enough good about it.

Quote:
Can you comprehend

1 3 a * 4 b * + 5 c * +

or

1 3 a * 4 b * 5 c * + +

at a glance?

Instead of separating them all with the same one space, group the parts of it, separating groups with, say, three spaces instead of one, to make it more readable. I would even use spaces to group your algebraic version of the same, again to make it more readable since the stars have to be there so you can't just say 3A+4B+5C+1. You can also put the algebraic in the comments if you want, to have both.

Quote:
What if you could type

IF SQ(x)==y THEN ...

?

I speak RPN but not RPL, but I use Forth which has a lot in common with RPL. I definitely like the way it does:

<test this expression, argument, flag, whatever>

<IF true, do the following>

<ELSE do this>

<THEN, when you're done with either of those, continue on here.>

Quote:
You could write this line also entirely in algebraic notation:

x=total(squared(divs(arr[i-n/2])))

Line 3: Algebraic makes the comparison more readable

Uh, yeah, whatever. To each his own I suppose. I sure don't find that readable.

Quote:
You may write
expr
'expr' EVAL

The only thing I would suggest is that if you take this route, you may want to have an "unEVAL" command, so, for example, if you had

UNEVAL expr

it would work just like expr alone does now.

The reason is that sometimes you do NOT want an expression to be evaluated (eg., evaluating it might be time consuming); you just want to push it onto the stack. It would be good to retain that ability (in contrast, the TI89 does not have that ability; it tries to evaluate every entered expression)

But switching the default to automatically evaluate expressions seems fine to me.

David, thanks for the encouragement!

Just leave the expression in 'quotes'. Omitting the quotes will trigger evaluation.

Quote:
x=total(squared(divs(arr[i-n/2])))

was not meant to be the readable version of line 2. This is where I, too, prefer RPN. Just wanted to illustrate how far you can push this, and that it allows you to go fully algebraic, if you must.

(Except, I should add, for control structures. You cannot say "FOR(0, 100, ...)".)

Edited: 19 Nov 2011, 4:31 a.m.

Hi Crawl.

Yes, just as Marcus says. Everything remains the same in respect to handling of expressions when quoted.

I don't really know the TI-89, but I suspect this would allow the calc to be used very similarly. (Again, except for CONTROL structures, which could be addressed, too.)

With one interesting side-effect:

Consider

a=4; 3+4; b=3+a;
in C-like languages.

The middle statement is valid. It's just "dead", inconsequential code.

In this merged world, "unconsumed" expression results, such as this one, are pushed onto a FIFO stack. And they're pulled from it when you specify a function or operator without arguments.

So, these are the two mods you need to transform an algebraic environment into a RPL environment. Not that weird sounding, perhaps.