3. Lovely lists
But what else can Elixir do? We've seen that it can handle strings and it can handle mathematical equations with ease and it can remember things, but what else? Well, what if we had a list of things that we wanted the computer to remember? And we wanted the computer to remember this list of things in a particular order?
We could do this by assigning each of the things to a unique variable:
iex> first = "fish" "fish" iex> second = "ham" "ham" iex> third = "eggs" "eggs"
But then we would need to remember that the things we want are stored in the variables
third. And what if we — as humans — forget what position we were up
iex> fifth = "bread" "bread"
Disasterous! This simply won't do.
A better solution to getting the computer to remember a list in Elixir is to actually tell it what we're storing is
a list. To do this, we wrap our list in square brackets (
) and separate each item in the list with a
iex> shopping_list = ["fish", "ham", "eggs", "bread"] ["fish", "ham", "eggs", "bread"]
Just like we need to start and end a string with double quotes, we need to start and end a list with square brackets
The computer will now remember our entire shopping list in one variable (
shopping_list), rather than
remembering it across multiple variables. Now we have a one-stop-shop that we can go to for our shopping list.
We can use more than just strings when we're creating lists too. Let's say we want to have a list of the last temperatures (in celsius) of the last five days. We could represent this data like this:
iex> temperatures = [15, 17, 19, 20, 20]
In fact, anything in Elixir can be an item in a list.
Izzy, impressed with Elixir's ability to keep a list of anything her heart desires, has started taking the names down of the people who are in the crowd. Her computer materialised from nowhere, or perhaps from somewhere in the throng nearby. It was too quick to really notice. She starts taking down names, fingers flying across the keyboard:
iex> those_who_are_assembled = [ "Izzy", "The Author", "The Reader", "Juliet", "Mary", "Bobalina", "Charlie", "Charlie (no relation)" ] ["Izzy", ...]
And so Izzy continues. She disappears into the crowd directly to our right, and somehow re-appears within mere seconds on our left. This is despite the density of the crowd. Amazing stuff. She then asks: "Hey Mr. Author Guy, I want to also track people's ages so that I can do some statistics on who exactly is assembled here. Oh, and I want to track their gender too. But a list doesn't seem very suitable for this. How can I do this?"
Well Izzy, there's more than one way you could do this. If all you wanted to track about a person was their name, their age and their gender, you could make each item in the list its own list:
iex> those_who_are_assembled = [ ...> ["Izzy", "30ish", "Female"], ...> ["The Author", "30ish", "Male"], ...> ]
These lists-inside-of-lists would be called sub-lists. You would then have to keep in mind that the first item in each sub-list is the name, the second is the age and the third is the gender. What if you (or someone else, using your computer) forgot the order and started adding in people, with age and gender swapping positions?
iex> those_who_are_assembled = [ ...> ["Izzy", "30ish", "Female"], ... a long time passes ... ...> ["Izzy the Younger", "Female", "20ish"], ...> ]
Pandemonium again! We need a better way of enforcing what data we're collecting for each person. We need a better data structure than lists within lists. Read on to find out what that data structure is!