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

Previous | Contents | Next