Inform 7 Home Page / Documentation
§21.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:
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:
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:
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/from (list of values)
This phrase adds the given value so that it becomes the entry with that index number in the list. Example:
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/from (list of values)
This phrase adds the first list to the second so that it begins at the given position. Example:
results in L being {1, 2, 4, 8, 12, 3, 4}.
A list is allowed to contain duplicates, and the order matters. For instance:
makes L into "2, 2 and 3". This is a different list to the one made by:
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) in/from (list of values)
This phrase removes every instance of the given value from the list. Example:
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) in/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, 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) in/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) in/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}.