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

Determine if the array is a vector.

IF 1 ==

 

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 +

Roll up the element count and combine all

ROLLD →LIST

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?

Was original array a vector? If the original array

THEN OBJ→ DROP

was a vector, then drop the second dimension

→LIST

(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

Begin the error clause.

OBJ→

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

Begin local variable structure and initiate

FOR i

FOR…NEXT loop for each row.

m →LIST

Collect a group of elements into a row (a list).

'm*(n-i)+i' EVAL

Computes the number of elements to roll so

ROLLD

that the next row can be collected.

RPL Programming Examples 2-21