20.10. Lengthening or shortening a list

| Chapter 20: Lists |

| 20.10. Lengthening or shortening a list

We can explicitly change the length of a list like so:

change (list of values) to have (number) entries/entry

This phrase alters the given list so that it now has exactly the number of entries given. Example: change L to have 21 entries; If L previously had more than 21 entries, they are thrown away (and lost forever); if L previously had fewer, then new entries are created, using the default value for whatever kind of value L holds. So extending a list of numbers will pad it out with 0s, but extending a list of texts will pad it out with the empty text "", and so on.

We can also write the slightly different phrases:

truncate (list of values) to (number) entries/entry

This phrase alters the given list so that it now has no more than the number of entries given. Example: truncate L to 8 entries; shortens L to length 8 if it is currently longer than that, trimming entries from the end, but would (for instance) leave a list of length 3 unchanged. Note that truncate L to 0 entries; empties it to { }, the list with nothing in.

truncate (list of values) to the first (number) entries/entry

This phrase alters the given list so that it now consists only of the initial part of the list with the given length. Example: truncate L to the first 4 entries; turns {1, 3, 5, 7, 9, 11} to {1, 3, 5, 7}.

truncate (list of values) to the last (number) entries/entry

This phrase alters the given list so that it now consists only of the final part of the list with the given length. Example: truncate L to the last 4 entries; turns {1, 3, 5, 7, 9, 11} to {5, 7, 9, 11}.

But we don't have to truncate: we can also -

extend (list of values) to (number) entries/entry

This phrase pads out the list with default values as needed so that it now has at least the given length. (If the list is already at least that length, nothing is done.) Example: extend L to 80 entries; lengthens L to length 80 if it is currently shorter than that.

For example,

> To check sorting (N - a number):
> let L be a list of numbers;
> extend L to N entries;
> repeat with X running from 1 to N:
> now entry X of L is X;
> say "L unrandomised is [L].";
> sort L in random order;
> say "L randomised is [L].";
> sort L;
> say "L in ascending order is [L]."

builds a list of N numbers (initially all 0), fills it with the numbers 1, 2, 3, ..., N, then randomly reorders them, then sorts them back again, recovering the original order. The text produced by "check sorting 10" depends partly on chance but might for instance be:

> L unrandomised is 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.
> L randomised is 6, 2, 9, 3, 10, 1, 7, 4, 8 and 5.
> L in ascending order is 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.

As with indexed text in the previous chapter, a project which needs really long lists should use the Glulx virtual machine - "check sorting 10000", for instance, would break the default memory environment on the Z-machine, which is very tight, but works fine (if not very rapidly) on Glulx.

419
| Example Leopard-skin A maze that the player can escape if he performs an exact sequence of actions. |
RB

Suppose (as in Infocom's Leather Goddesses of Phobos) that we have a maze that the player can escape only by performing the correct sequence of actions in the correct order. One way to do this would be to keep a list of the player's most recent actions, and see whether these match up with the combination we have established as the maze's solution. For instance: "Leopard-skin" The Fur-Lined Maze is a room. "This seemingly endless sequence of rooms is decorated in a tasteful selection of exotic furs and gilded fixtures." Clapping is an action applying to nothing. Understand "clap" as clapping. Kweepaing is an action applying to nothing. Understand "kweepa" as kweepaing. Carry out clapping: say "You clap." Carry out kweepaing: say "You holler 'KWEEPA!' triumphantly." The maze-sequence is a list of stored actions that varies. When play begins: add the action of jumping to the maze-sequence; add the action of clapping to the maze-sequence; add the action of kweepaing to the maze-sequence. The attempted-sequence is a list of stored actions that varies. Every turn when the player is in the Fur-Lined Maze: truncate the attempted-sequence to the last two entries; add the current action to the attempted-sequence; if the attempted-sequence is the maze-sequence: say "That does it! You are instantly transported from the maze!"; end the story finally. Test me with "hop / clap / clap / hop / kweepa / hop / clap / kweepa".

420
| Example The Facts Were These Creating a variant GIVE action that lets the player give multiple objects simultaneously with commands like GIVE ALL TO ATTENDANT or GIVE THREE DOLLARS TO ATTENDANT or GIVE PIE AND HAT TO ATTENDANT. The attendant accepts the gifts only if their total combined value matches some minimum amount. |
RB

Previous | Contents | Next