Table of Contents
1. repeat
Executes a procedure a specified number of times.
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
(varies) |
Results from procedure executions |
1.3. Description
repeat executes proc exactly int times, where int must be a non-negative integer.
The operator removes both operands before executing proc for the first time. If int is 0, proc is not executed at all.
If proc executes exit, the loop terminates prematurely.
1.5. Examples
Simple repetition
4 { (abc) } repeat
% Stack: (abc) (abc) (abc) (abc)
With side effects
10 { (.) print } repeat
% Prints: ..........
Zero repetitions
mark 0 { (won't happen) } repeat
% Stack: mark (procedure never executed)
Removing values
1 2 3 4
3 { pop } repeat
% Stack: 1 (removed top 3 values)
1.7. Common Pitfalls
| Stack Accumulation - If proc pushes values without consuming them, they accumulate. |
10 { 42 } repeat
% Stack has 10 copies of 42!
5 { % No way to know which iteration } repeat
Use for if Index Needed - If you need to know the iteration number, use for instead:
|
0 1 4 { % Iteration index on stack } for
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
int is negative |
[ |
Fewer than 2 operands on stack |
[ |
int not integer, or proc not procedure |
1.9. Implementation Notes
-
Efficient for simple fixed repetition
-
No control variable overhead
-
Very fast for small counts
-
Procedure executed in current context