
Defining Program Structure
Developing TACL Programs
107365 Tandem Computers Incorporated
2–3
You can define a macro that increments a loop variable (passed as the argument); for
example:
[#DEF next MACRO |BODY|
#SET %1% [#COMPUTE %1% + 1]
]
Use the macro in Figure 2-2,
bubble
, with its nested #LOOP statements, to perform a
bubble sort. A bubble sort compares numbers and switches their places until the
numbers are stored in ascending order.
To run
bubble
, load its file and supply the number of sort elements as an argument:
bubble
num
Figure 2-2. Performing a Bubble Sort With Nested #LOOP Statements (Page 1 of 2)
?SECTION bubble MACRO
#FRAME
#PUSH i j max ocount element prompt temp
#SETMANY i j max ocount element, 0 0 0 0 0
#SET prompt Enter Next Number to be Sorted...
== Request the number of elements specified in argument 1 and
== store them in levels of STACK
[#LOOP |WHILE| (element < %1%) |DO|
#PUSH stack
#INPUTV stack prompt
#SET element [#COMPUTE e 1]
] {end of INPUT loop}
== Loop once for each element
#SET ocount 1
[#LOOP |WHILE| (ocount < %1%) |DO|
#SET i 1
#SET max [#COMPUTE %1% - 1]
== Compare each element to its adjacent number; switch places
== if we encounter a smaller number.
[#LOOP |WHILE| (i < max) |DO|
#SET j [#COMPUTE i + 1]
[#IF ([stack.i] > [stack.j]) |THEN|
#SET temp [stack.i]
#SET stack.i stack.j
#SET stack.j temp
]
#SET i [#COMPUTE i + 1]
] {end of inner loop}
#SET ocount [#COMPUTE 1]
] == end of outer loop