20.5. Building lists

| Chapter 20: Lists |

| 20.5. Building lists

We have already seen "add... to...". This in fact comes in two forms:

add (value) to (list of values)

This phrase adds the given value to the end of the list. Example: let L be {60, 168}; add 360 to L; results in L being {60, 168, 360}. Note that the value is added even if it already occurs somewhere in L; this can be avoided with "if absent". So: add 168 to L, if absent; would do nothing - it is already there.

add (list of values) to (list of values)

This phrase adds the first list to the end of the second. Example: let L be {2, 3, 5, 7}; add {11, 13, 17, 19} to L; results in L being {2, 3, 5, 7, 11, 13, 17, 19}.

If we don't want to add new entries at the end, we can instead say where they should go:

add (value) at entry (number) in (list of values)

This phrase adds the given value so that it becomes the entry with that index number in the list. Example: let L be {1, 2, 3, 4, 8, 24}; add 12 at entry 6 in L; sets L to {1, 2, 3, 4, 8, 12, 24}. If there are N entries in L, then we can add at any of entries 1 up to N+1: adding at entry N+1 means adding at the end. The phrase option "if absent" makes the phrase do nothing if the value already exists anywhere in L.

add (list of values) at entry (number) in (list of values)

This phrase adds the first list to the second so that it begins at the given position. Example: let L be {1, 2, 3, 4}; add {4, 8, 12} at entry 3 in L; results in L being {1, 2, 4, 8, 12, 3, 4}.

A list is allowed to contain duplicates, and the order matters. For instance:

> let L be {2, 2, 3};

makes L into "2, 2 and 3". This is a different list to the one made by:

> let M be {2, 3, 2};

even though L and M have the same values, repeated the same number of times - for two lists to be equal, they must have the same kind of entry, the same number of entries, and the same entries in each position.

We can also strike out values:

remove (value) from (list of values)

This phrase removes every instance of the given value from the list. Example: let L be {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; remove 1 from L; results in L being {3, 4, 5, 9, 2, 6, 5, 3}. Ordinarily "remove 7 from L" would produce a run-time problem, since L does not contain the value 7, but using the "if present" option lets us off this: the phrase then does nothing if L does not contain the value to be removed.

remove (list of values) from (list of values)

This phrase removes every instance of any value in the first list from the second. Example: let L be {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; remove {0, 2, 4, 6, 8} from L; results in L being {3, 1, 5, 9, 5, 3}. If both lists are large, this can be a slow process, and we might do better by sorting them and trying a more sophisticated method. But this is convenient for anything reasonable-sized.

Again, we can also remove from specific positions:

remove entry (number) from (list of values)

This phrase removes the entry at the given position, counting from 1 as the first entry. (Once it is removed, the other entries shuffle down.) Example: let L be {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; remove entry 3 from L; results in L being {3, 1, 1, 5, 9, 2, 6, 5, 3}.

remove entries (number) to (number) from (list of values)

This phrase removes the entries at the given range of positions, counting from 1 as the first entry. (Once they are removed, the other entries shuffle down.) Example: let L be {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; remove entries 3 to 6 from L; results in L being {3, 1, 2, 6, 5, 3}.

415
| Example Robo 1 A robot which watches and records the player's actions, then tries to repeat them back in the same order when he is switched into play-back mode. |
RB

"Robo" The Experimentation Chamber is a room. Robo is a man in the Experimentation Chamber. "Robo, your prototype tin companion, stands awkwardly beside you. In the middle of his chest is a red enamel button[if the red button is switched on], currently depressed[otherwise], currently un-depressed[end if]." The red button is a device. It is part of Robo. Instead of pushing the red button: if the red button is switched off, try switching on the red button; otherwise try switching off the red button. After switching on the red button: say "CLICK! Robo is now in play-back mode." After switching off the red button: say "CLACK! Robo is now in observation mode." Definition: Robo is watching if the red button is switched off. The current instruction set is a list of stored actions that varies. After doing something when Robo is watching and Robo can see the player: now the actor is Robo; add the current action to the current instruction set; now the actor is the player; say "Robo watches you [the current action][one of], his yellow eyes lamp-like and observant[or]. In his metal head, gears whirr[or], his brushed-copper handlebar moustaches twitching[or] impassively[at random]."; continue the action. Every turn when Robo is not watching: if the number of entries in the current instruction set is 0: say "Robo has run out of behavior and grinds to an (expectant) halt."; now the red button is switched off; otherwise: let the next task be entry 1 of the current instruction set; try the next task; remove entry 1 from the current instruction set. The red block and the blue cylinder are things in the Experimentation Chamber. The counter is a supporter in the Experimentation Chamber. The counter is scenery. Report Robo examining Robo: say "Robo examines each of his hands in turn, then each of his legs (bending over mostly double in the middle to do this)." instead. Report Robo examining the player: say "Robo stares at you, unblinkingly, for several seconds together[if a random chance of 1 in 7 succeeds]. His left moustache-bar twitches infinitesimally upward[end if]." instead. Report Robo taking the cylinder: say "[one of][Robo] needs several attempts to get his metal fingers around [the cylinder] -- they are not designed for grasping small objects elegantly. But at last he succeeds[or]Once again, Robo struggles a bit before picking up [the cylinder][stopping]." instead. Test me with "z / take cylinder / take block / put cylinder on counter / put block on counter / x robo / x me / get block / drop block / press red button / z / z / z / z / z / z / z / z / z / z".

Previous | Contents | Next