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 to?
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 comma:
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.
Roberto, impressed with Elixir's ability to keep a list of anything his heart desires, has started taking the names down of the people who are in the crowd. His computer materialised from nowhere, or perhaps from somewhere in the throng nearby. It was too quick to really notice. He starts taking down names, fingers flying across the keyboard:
iex> those_who_are_assembled = [ "Roberto", "The Author", "The Reader", "Juliet", "Mary", "Bobalina", "Charlie", "Charlie (no relation)" ] ["Roberto", ...]
And so Roberto continues. He 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. He 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 Roberto, 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 = [ ...> ["Roberto", "30ish", "Male"], ...> ["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 sublist 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 = [ ...> ["Roberto", "30ish", "Male"], ... a long time passes ... ...> ["Young Roberto", "Male", "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!