Post Reply 
How much has RPN/RPL spoiled you?
06-05-2015, 11:54 PM
Post: #32
RE: How much has RPN/RPL spoiled you?
(06-04-2015 01:16 PM)Dave Britten Wrote:  RPN (and RPL) is fantastic for number crunching, or small programs that augment the built-in functions, but for building a large, sophisticated application, it's painful at best, especially with no GOTO in RPL. Compared to a symbolic language, the code is almost always opaque and barely maintainable, with no immediately obvious connection to what's getting passed as a parameter to what, and a lack of any code commenting ability.

Solving a quadratic equation is probably something all of us are familiar with:

\( ax^2 + bx + c = 0 \)

However I'm not using the quadratic formula but transform the equation into this reduced form:

\( x^2 - 2px + q = 0 \)

By comparing the coefficients we find:

\( p = \frac{b}{-2a} \)

\( q = \frac{c}{a} \)

The solutions for this equation are:

\( x_{1,2} = p \pm D \)

where \( D = \sqrt{p^2-q} \)

Compare these two solutions:

Code:
\<<
    \-> a b c \<<
        b a / -2 /
        c a /
        \-> p q \<<
            p SQ q - \v/
            \-> D \<<
                p D -
                p D +
            \>>
        \>>
    \>>
\>>

Code:
\<<
    ROT ROT OVER / -2 /
    ROT ROT /
    OVER SQ SWAP - \v/
    DUP2 - 
    ROT ROT +
\>>

Which one is easier to understand?

But we can split the calculation into three tasks:
  • TRANSFORM ( a b c -- p q )
  • DISCRIMINANT ( p q -- D )
  • PLUSMINUS ( p D -- x1 x2 )

Code:
%%HP: T(3)A(R)F(.);
DIR
  SOLVE \<<
        TRANSFORM
        DISCRIMINANT
        PLUSMINUS
  \>>
  UNROT \<<
        ROT ROT
  \>>
  TRANSFORM \<<
        UNROT OVER / -2 /
        UNROT /
  \>>
  DISCRIMINANT \<<
        OVER SQ SWAP - \v/
  \>>
  PLUSMINUS \<<
        DUP2 -
        UNROT +
  \>>
END

This also allows to test each of them individually.

Quote:(RPL is often called a write-only language, and I think I've heard the same epithet applied to perl and/or regular expressions.)

Whether your code is maintainable has little to do with the programming language you use.
It is more related to how the code is organized.
Using subroutines or local variables is a way to give meaningful names. This helps to understand and thus maintain the code.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: How much has RPN/RPL spoiled you? - Thomas Klemm - 06-05-2015 11:54 PM



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