Inform 7 Home Page / Documentation


Chapter 21: Lists

§21.1. Lists and entries; §21.2. Constant lists; §21.3. Saying lists of values; §21.4. Testing and iterating over lists; §21.5. Building lists; §21.6. Lists of objects; §21.7. Lists of values matching a description; §21.8. Sorting, reversing and rotating lists; §21.9. Accessing entries in a list; §21.10. Lengthening or shortening a list; §21.11. Variations: arrays, logs, queues, stacks, sets, sieves and rings

arrow-up-left.png Contents of Writing with Inform
arrow-left.png Chapter 20: Advanced Text
arrow-right.png Chapter 22: Advanced Phrases
arrow-down-right.png Indexes of the examples

§21.1. Lists and entries

Many sections in this book begin by introducing a new kind of value. Reading through in order, the possibilities mount up: numbers, times, texts, and so on. (See the Kinds page of the Index for a convenient list of the options.) This section is a little different: rather than showing a single new kind of value, it shows how to make a new kind out of any existing one.

If K is any kind of value, then "list of K" is also a kind of value. For instance, we could write:

let L be a list of numbers;

and this would create a new "let" variable, called L, whose kind of value is "list of numbers". On the other hand, we are not allowed to write:

let L be a list;

because "list" by itself is not a kind of value. (Inform always needs to know what kinds the values entered in a list are going to have.)

Lists are like flexible-length table columns, but that probably makes them sound more mysterious than they really are. A list is simply a sequence of values, called its "entries", numbered from 1 upwards. The number of entries is called its "length". If we try

let L be a list of numbers;
say "L has [the number of entries in L] entries.";

then we find

L has 0 entries.

This is because all lists start out empty when created: that is, they initially have 0 entries. Inform has two built-in adjectives "empty" and "non-empty" which can apply to lists, and they mean just what they ought to mean: a list is empty if its length is 0, and otherwise non-empty.

We can add entries very easily:

add 2 to L; add 3 to L; add 5 to L;

We can now, for instance, try saying the list:

say "L is now [L].";

with the result

L is now 2, 3 and 5.

Note that only numbers can be added to L: if we try

add "clock" to L;

Inform will produce a problem message, because L has kind "list of numbers", whereas "clock" is text. In this way, Inform ensures that a list always contains values of the same kind throughout. So it's not possible to construct a list whose entries are:

2, "fish", 4 and the Entire Game

Such a list would be very hazardous to deal with, in any case. If what we need is a combination of different kinds of values, tables are a better option.

Finally, note that since "list of numbers" is a kind of value in its own right, so is "list of lists of numbers", and so on - though such lists are trickier to deal with, they are sometimes handy.


arrow-up.png Start of Chapter 21: Lists
arrow-left.png Back to Chapter 20: Advanced Text: §20.9. Summary of regular expression notation
arrow-right.png Onward to §21.2. Constant lists