1. Appeasing the masses with code
The masses have sat through the best part of a chapter (at least) absorbing everything that we've talked about. But they're a little rowdy because we've been talking about programming languages without actually doing any programming. Well, that's completely understandable! I would be just as annoyed if I wasn't the one writing the book. I know what's coming but they don't.
OK, let's try appeasing the masses. Here's some code:
The masses are silent. Their stares harden. Their spokesman -- who they've nominated while I thought of this snippet -- says (while systematically tearing down the fourth wall): "That's just a bunch of quoted words! Just like this one that I'm speaking."
The crowd agrees.
Ok, you're right. It is some quoted words, but they're quoted words that both humans and computers can understand. That's pretty cool. Well, I thought so, at least.
The crowd of humans arrayed before us know the words and that the words have meaning behind them and form an understandable sentence. The computer knows only the individual letters or symbols in the words, and cares not that the words have a meaning or that the words form an understandable sentence. "Those things are only important to the humans", it thinks, without acknowledging the Machine Learning zeitgeist that has sprung up recently.
When we say the sentence to the masses, they can easily repeat it back to us. To speak to the computer, we need to open up a prompt for the particular language we want to use to converse. Different languages have different prompts. We can't just speak to the computer to program it. Not yet, anyway.
In this case, we want to talk Elixir and so we can open a prompt with the
iex stands for "Interactive EliXir". To open a prompt, we'll need to first open our terminal or command prompt application. Once that's open, then we can type
iex into that window and press Enter to start our
When it starts, the prompt says:
Erlang/OTP 19 [erts-8.1] [source] ... Interactive Elixir (1.5.0) - ⏎ press Ctrl+C to exit (type h() ENTER for help)
We can safely ignore the output from the prompt so far. It is just giving us some nerdy information. The last line shown here tells us that we're running Interactive Elixir "1.5.0". This tells us that we're running the 1.5.0 version of Elixir. It tells us that we can hit Ctrl+C to exit -- this will stop the
iex prompt and return us back to our terminal's prompt).
The next thing the computer says is:
This tells us that the computer is now listening for our instructions, eagerly awaiting them. It's prompting us for input. Let's give the computer our sentence again, pressing enter at the end of the line:
iex(1)> "Hello, World!" "Hello, World!"
On the first line here, we're giving the computer an instruction that looks like a regular sentence. This is because it is a regular sentence. In computer-nerd-terminology, we refer to this double-quoted collection of symbols as a string. It's easier to think of it like a string if you think of all of the sentence's parts being connected by an actual string:
The computer then takes in this string, interprets it and tells us how it interpreted the string. In this case, the computer is just parroting our sentence/string back to us. The computer can do a lot more than this, believe me. Computers wouldn't be very good if all they did was parrot back to us.
The masses are now getting fidgety. So far we've shown just the one line of code. Twice, yes, but it's still just one line of code. And the computer is prompting us with this line.
The computer hungers for more input. The masses hunger, too. Fortunately for me (and you), Elixir can do much more than parrot.
Ok, so the computer can parrot things. But what else can it do? How about we ask the computer to do some simple mathematics?
iex> 2 + 4 6 iex> 3 - 6 -3 iex> 4 * 12345 49380 iex> 1234 / 4 + 2 - 12 * 3 274.5
This appeases the masses, slightly. They're a fickle bunch. The computer is now no longer parroting things back to us. It's instead calculating the not-so-advance mathematical equations we're giving it, and then giving us the right numbers.
They realise that Elixir is now built for more things than simple parroting. Elixir can do calculations too! We've used the symbols
/ here, asking the computer to add, subtract, multiply and divide, in that order.
What we aim to achieve in this book
Okay, now that the masses are appeased, let's take a moment to talk about what this book is going to cover and what you're going to get out of it.
The first 10 chapters are going to cover some Elixir basics. Yes, that seems like a lot of chapters but most of these chapters are fairly short; just a couple of pages long. It's about 50 pages in total. These chapters will cover the things that you should know before you can write any Elixir program of a decent length, and it gives you a good tour of the language. These chapters are important becuase they will build a solid foundation for your Elixir experience.
Chapter 11 onwards will focus on building larger programs using the techniques that we've learned in the first 10 chapters. We'll combine things from earlier chapters and use them altogether and we'll even get introduced to even more things that the Elixir language does.
At the end of each chapter, there will be a set of (optional) exercises that you can do, using things that you have learned in the chapter. I strongly encourage that you try these out at least. If you're unable to do them, don't sweat it. Just try again later on.
With all that said, let's continue looking at what Elixir can do for us, starting next with making Elixir remember things for us.
- Get Elixir to calculate the number of seconds in the day by multiplying the hours in a day by 60 twice. How many seconds are there in a day?
- Calculate the average of these numbers: 4, 8, 15, 16, 23 and 42.