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:

 

 

«

 

OBJ→

Take the expression apart.

IF OVER

If the argument count is nonzero, then

THEN → n f

store the count and the function.

«

Begins local variable defining procedure.

1 n

Begins FOR…NEXT loop, which

FOR i

converts any algebraic arguments to lists.

IF DUP TYPE 9. SAME

Tests whether argument is an algebraic.

THEN →RPN

If argument is an algebraic, convert it to a

 

list first.

END n ROLLD

Roll down the stack to prepare for the

 

next argument.

NEXT

Repeat the loop for the next argument.

IF DUP TYPE 5. ‹

Tests to see if level 1 object is a list.

THEN 1 →LIST

If not a list, then convert it to one.

END

Ends the IF…THEN…END structure.

IF n 1 >

Tests to see if there is more than one

 

argument.

THEN 2 n

Combine all of the arguments into a list.

START +

 

NEXT

 

END f +

Append the function to the end of the list.

»

End the local variable defining procedure.

ELSE 1 →LIST SWAP DROP

For functions with no arguments,

 

converts to a simple list.

END

End the IF…THEN… ELSE…END

 

structure.

»

 

 

 

`O→RPN K

Stores the program in →RPN.

 

 

2-28 RPL Programming Examples