2-28 RPL Programming Examples
Techniques used in "RPN
! Recursion. The "RPN program calls itself as a subroutine. This powerful technique works just like calling
another subroutine as long as the stack contains the proper arguments before the program calls itself. In this
case the level 1 argument is tested first to be sure that it is an algebraic expression before "RPN is called
again.
! Object Type-Checking. "RPN uses conditional branching that depends on the object type of the level 1
object.
! Nested program Structures. "RPN nests IF…THEN…END structures inside FOR…NEXT loops inside a
IF…THEN… ELSE…END structure.
! List Concatenation. The result list of objects in RPN order is built by using the ability of the + command to
sequentially append additional elements to a list. This is a handy technique for gathering results from a
looping procedure.
"RPN program listing
Program: Comments:
"!
LS_#!
67!LN<K!
:;<=!#!G!*!
Take the expression apart.
If the argument count is nonzero,
then store the count and the
function.
"!
Begins local variable defining
procedure.
3!G!
7LK!t!
Begins FOR…NEXT loop, which
converts any algebraic arguments to
lists.
67!452!:P2<!Xd!DBM<
Tests whether argument is an
algebraic.
:;<=!#K2=!
If argument is an algebraic, convert
it to a list first.
<=4!G!KLEE4!
Roll down the stack to prepare for
the next argument.
=<T:!
Repeat the loop for the next
argument.
67!452!:P2<!Qd!C!
Tests to see if level 1 object is a list.
:;<=!3!#E6D:!
If not a list, then convert it to one.
<=4!
Ends the IF…THEN…END
structure.
67!G!3!9!
Tests to see if there is more than
one argument.
:;<=!H!G!
D:BK:!'!
=<T:!
Combine all of the arguments into a
list.