2-14 RPL Programming Examples
Expanding and Collecting Completely
This section contains two programs:
! MULTI repeats a program until the program has no effect on its argument.
! EXCO calls MULTI to completely expand and collect an algebraic.
MULTI (Multiple Execution)
Given an object and a program that acts on the object, MULTI applies the program to the object repeatedly until
the program no longer changes the object.
"
object «program»
"
object
result
Techniques used in MULTI
! DO…UNTIL…END (indefinite loop). The DO clause contains the steps to be repeated. The UNTIL
clause contains the test that repeats both clauses again (if false) or exits (if true).
! Programs as arguments. Although programs are commonly named and then executed by calling their
names, programs can also be put on the stack and used as arguments to other programs.
! Evaluation of local variables. The program argument to be executed repeatedly is stored in a local variable.
It's convenient to store an object in a local variable when you don't know beforehand how many copies you'll
need. An object stored in a local variable is simply put on the stack when the local variable is evaluated.
MULTI uses the local variable name to put the program argument on the stack and then executes EVAL to
execute the program.
MULTI program listing
Program: Comments:
"!
#!q!
Creates a local variable p that contains the
program from level 1.
"!
4L!
452!
!
!
q!<NBE!
!
452!
KL:!
5=:6E!
DBM<!
!
!
<=4!
»!
Begins the defining procedure.
Begins the DO loop clause.
Makes a copy of the object, now in level 1.
Applies the program to the object, returning
its new version.
Makes a copy of the new object.
Moves the old version to level 1.
Begins the DO test clause.
Tests whether the old version and the new
version are the same.
Ends the DO structure.
Ends the defining procedure.
»
`OMULTI K
Stores the program in MULTI.
Checksum: # 22693d
Bytes: 56