Reference Guide
RPL Programming Examples 2-21
Program: Comments:
1 CF
Make sure the flag 1 is clear to begin the
procedure.
a DUP SIZE
Retrieve the dimensions of the array.
DUP SIZE
IF 1 ==
Determine if the array is a vector.
THEN 1 SF 1 +
If array is a vector, set flag 1 and add a second
dimension by treating the vector as an n × 1
matrix.
SWAP OBJ→ OBJ→ DROP
Disassemble the original vector, leaving the
element count, n, in level 1.
1 + ROLL
Roll the elements up the stack and bring the
“matrix” dimensions of the vector to level 1.
ELSE DROP2 a OBJ→
If array is a matrix, clean up the stack and
decompose the matrix into its elements, leaving
its dimension list on level 1.
END DUP OBJ→ DROP *
Duplicate the dimension list and compute the
total number of elements.
SWAP OVER 2 +
ROLLD →LIST
Roll up the element count and combine all
elements into a list. Note that the elements in
the list are in row-major order.
1 p DOSUBS
Recalls the program and uses it as an argument
for DOSUBS (DOLIST works in this case as
well). Result is a list of transformed elements.
OBJ→ 1 + ROLL
Disassembles the result list and brings the array
dimensions to level 1.
IFERR
Begins the error-trapping structure. Its purpose
is to find and handle the cases when the result
list contains symbolic elements.
IF 1 FS?
THEN OBJ→ DROP
→LIST
Was original array a vector? If the original array
was a vector, then drop the second dimension
(1) from the dimension list.
END →ARRY
Convert the elements into an array with the
given dimensions. If there are symbolic elements
present, an error will be generated and the error
clause which follows will be executed.
THEN
OBJ→
Begin the error clause.
Put the array dimensions on levels 2 and 1. If
the array is a vector, level 1 contains a 1.
IF 1 FC?C
Is original array a matrix? Clear flag 1 after
performing the test.
THEN DROP
Drop the number of matrix elements.
END → n m
Store the array dimensions in local variables.
« 1 n
FOR i
Begin local variable structure and initiate
FOR…NEXT loop for each row.
m →LIST
Collect a group of elements into a row (a list).
'm*(n-i)+i' EVAL
ROLLD
Computes the number of elements to roll so
that the next row can be collected.