text
stringlengths
0
897
: a data type that is an ordered sequence of other data
Lists in MiniScript appear in square brackets, with their elements separated with commas. You can assign these to variables, pass them into functions, and generally handle them just like you would numbers and strings.
{caption:"List Example 1"}
```terminal
> a = [1, 2, "x"]
> b = ["foo", 42]
> c = a + b
> print c
[1, 2, "x", "foo", 42]
```
In this example, line 1 creates a list with three elements — the numbers 1 and 2, and the string "x" — and assigns this to variable `a`. Line 2 creates a similar list `b` with two elements. Then line 3 adds these together and stores the result in `c`. Lists support addition (`+`), replication (`*`), and division (`/...
At this point, you should put the book down for a moment and go play with this using the command-line REPL. Experiment! Can you put a list inside another list? Does it matter whether you put a space after each comma? Can you make a list with no elements at all? Lists are a really important type of data that will c...
Of course just being able to create a list and print it isn't very useful. You also need to be able to pull an element out of a list. We can do this with *list indexing*.
list index
: a numeric value, starting at 0, that indicates a particular element of a list
You use a list index by simply putting it in square brackets after a variable that contains a list. The first element is index 0, so you can print just the first element of a list this way:
{i:"list indexing;indexing, list"}
{caption:"List Indexing"}
```miniscript
a = ["foo", 42, "XYZZY"]
print a[0]
```
D> Counting in MiniScript, and most other modern languages, generally starts at 0. Just think of the index as "how many elements come *before* the one I want" and you'll be fine.
Experiment with that last example, too. Make sure you can print out any element you want. If you try to use an index that doesn't exist, such as 7 when there are only three elements, MiniScript will report the problem as a "MiniScript::IndexException".
There's one more trick with list indexing that is super handy: by using negative numbers, you can count backwards from the end of the list. The last element is -1, the second-to-last is -2, and so on.
{caption:"List Indexing, Backwards!"}
```miniscript
a = ["foo", 42, "XYZZY"]
print a[-1]
```
Again, experiment with this, and see that if you go too far, you get an `IndexException`.
Just as there are a number of intrinsic (built-in) functions for working with numbers and strings, there are intrinsic functions for lists, too. These are shown in the table on the next page.
{gap:12}
{i:"functions, list;list functions"}
{caption:"Intrinsic list functions", colWidths:"170,*"}
| `.hasIndex(i)` | 1 if i is a valid list index; otherwise 0 |
| `.indexes` | a list with numbers from 0 to `self.len-1` |
| `.indexOf(x, after=null)` | 0-based position of first element matching x in `self`, or null if not found; optionally begins the search after the given position |
| `.insert index, value` | inserts value into `self` at the given index (in place) |
| `.join(delimiter=" ")` | builds a string by joining elements by the given delimiter |
| `.len` | length (number of elements) of `self` |
| `.pop` | removes and returns the last element of `self` (like a stack) |
| `.pull` | removes and returns the first element of `self` (like a queue) |
| `.push x` | appends the given value to the end of `self`; often used with pop or pull |
| `.shuffle` | randomly rearranges the elements of `self` (in place) |
| `.sort key=null` | sorts list in place, optionally by value of the given key (e.g. in a list of maps) |
| `.sum` | total of all numeric elements of `self` |
| `.remove i` | removes element at index i from `self` (in place) |
| `.replace oldval, newval, maxCount=null` | replaces (in place) up to maxCount occurrences of oldval in the list with newval (if maxCount not specified, then all occurrences are replaced) |
| `slice(list, from, to)` | equivalent to list[from:to] |
This is the complete set of list intrinsics, and it includes some stuff we haven't talked about yet. So just as on Day 2, please don't worry about understanding everything in this table today. Let's just highlight a few of the most important functions:
- `a.len` gives you the length (number of elements) in list a.
- `a.push "x"` adds a new element "x" to the end of the list.
- `a.insert 2, "y"` inserts a new element "y" after the first 2 elements in the list.
- `a.remove 1` removes element 1 (that is, the item with 1 element to its left).
With these four functions, you have ways to find out how big a list is, and modify it in various ways: add a new item to the end, insert a new item at any position, and remove any item. You're probably beginning to see why lists are so important in programming; they let you hold and work with any amount of data.
{pageBreak}
Let's exercise these important list functions with an example.
{caption:"List Example 2"}
```miniscript
a = []
while a.len < 5
a.push input("Next item?")
end while
print "You entered: " + a
print "Element 2 is " + a[2]
print "But I can change that..."
a[2] = "HAHA!"
print "Now we have: " + a
print "But I feel bad. Let's just get rid of it."
a.remove 2
print "Now it's just: " + a
```
That's one of the longer examples, but go ahead and type it in. It's going to ask you to enter five strings; enter the names of the numbers in Spanish, or your favorite pizza toppings, or whatever. The program then does some shenanigans with the middle element (which, in list of length 5, is element 2). As always, p...
{i:"string indexing;indexing, string;immutable"}
D> You can index into strings, too! The elements of a string are characters. `s[2]` is a perfectly cromulent way to get character 2 (the one with 2 characters to its left) of a string. The main difference is, strings are *immutable*, which means you can't assign a new value to an element of a string, nor can you ins...
## `range` and `for` revisited