| Chapter 11: Phrases |

| 11.9. Repeat

The following kind of loop could if necessary be achieved just as well with a suitable "while", but occurs so often that it is convenient to have a standard form. Repeat loops also have the great advantage of being certain to finish.

> repeat with my counter running from 1 to 10 begin;
> (phrases)
> end repeat;

This is similar to the old FOR/NEXT loop from BASIC, for those with long memories, and runs through the given phrases ten times. Within those phrases, a special value called "my counter" has the value 1 the first time through, then the value 2, then 3 and so on up to 10. This value exists only within the loop, so it makes no sense to talk about "my counter" elsewhere. (It can of course be called whatever we like: this is only an example.)

The "begin" and "end repeat" are compulsory for repeat loops, in the interests of clarity.

We are allowed to "nest" loops, that is, to put one inside another.

> To plot a grid with size (S - a number):
> repeat with x running from 1 to S begin;
> say "Row [x]:";
> repeat with y running from 1 to S begin;
> say " [y]";
> end repeat;
> say ".";
> end repeat.

If we then write

> plot a grid with size 5

then the result is

> Row 1: 1 2 3 4 5.
> Row 2: 1 2 3 4 5.
> Row 3: 1 2 3 4 5.
> Row 4: 1 2 3 4 5.
> Row 5: 1 2 3 4 5.

Thus the innermost phrase, the say which mentions "y", happens 25 times.

141
| Example Wonka's Revenge A lottery drum which redistributes the tickets inside whenever the player spins it.

"Wonka's Revenge" The Caribou Lodge is a room. "Hundreds of expectant faces are turned your way from every table." A lottery drum is in the Lodge. "Before you is the lottery drum[if we have spun the drum], ready to disgorge a ticket[otherwise], waiting to be spun[end if]." In the drum are a red ticket, an orange ticket, a yellow ticket, a green ticket, a blue ticket, a purple ticket, and a ticket of pure gold. The drum is closed and openable. Understand "spin [something]" as spinning. Spinning is an action with past participle spun, applying to one thing. Check spinning: if the noun is an open container which contains something, say "[The list of things in the noun] would fly out." instead. Carry out spinning a container: shuffle the contents of the noun. Report spinning: if the noun contains something, say "You rattle [if the noun is transparent][the list of things in the noun][otherwise]the stuff[end if] in [the noun]."; otherwise say "Nothing results of your shaking [the noun]." Inform keeps track of the order in which things have been put into a container. If we want to change that order without the player's intervention, we can move the things ourselves. To shuffle the contents of (basket - a container): let moves be the number of things in the basket; repeat with counter running from 1 to moves begin; move a random thing in the basket to the basket; end repeat. After opening the drum when we have spun the drum for the first time: if something (called the pick) is in the drum begin; try searching the drum; say "[The pick] it is, then."; silently try taking the pick; if the pick is the ticket of pure gold, end the game in victory; otherwise end the game saying "Oh well, better luck next time."; end if. Test me with "open drum / look in drum / close drum / spin drum / open drum".

Previous | Contents | Next