Immediate algebraic expressions in RPL+
#1

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 ...
to increase readability.

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
instead of
'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.

#2

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

#3

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.
#4

Quote:
You may write
expr
instead of
'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.

#5

David, thanks for the encouragement!

#6

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

#7

Thanks for your feedback, Garth.

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.

#8

Hi Crawl.

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

#9

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.



Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing RPL programs on OS X Sean Freeman 18 5,037 11-30-2013, 03:59 PM
Last Post: Sean Freeman
  48G vs 49G+ User RPL Speed Comparison John Colvin 7 2,536 11-16-2013, 10:07 PM
Last Post: Han
  RPL 32 David Hayden 4 2,027 11-11-2013, 11:34 AM
Last Post: David Hayden
  HP Prime - Saved formulas, expressions CR Haeger 6 2,032 10-07-2013, 08:25 PM
Last Post: CR Haeger
  HP-Prime: issues in entering expressions fhub 30 7,664 10-02-2013, 12:32 AM
Last Post: Tim Wessman
  HHC / HP Museum Programming Contest for RPN and RPL machines Gene Wright 18 5,449 09-22-2013, 09:39 AM
Last Post: Miguel Toro
  Product Series and Limit Expressions on the 50G Matt Agajanian 13 3,590 09-03-2013, 02:02 PM
Last Post: Simone Cerica
  OT: Google evaluates mathematic expressions (with a twist) DavidShenk 4 1,987 02-13-2013, 09:05 AM
Last Post: Juan J
  RPL long vs. short names peacecalc 5 1,983 10-30-2012, 01:25 PM
Last Post: peacecalc
  Mini-challenge: HHC2012 RPL programming contest with larger input David Hayden 14 3,536 10-05-2012, 10:36 PM
Last Post: David Hayden

Forum Jump: