Note: This guide is a work in progress. I'm partly writing it to help me remember things, and partly to help anyone who comes across it while searching for the basics of Elixir. This is not meant to be a comprehensive guide. If you go through this and need more, I'll list some learning material at the end. I will continue to add to this as I go. As it is, it's not currently complete. Thanks for reading.
If you're reading this, you're probably interested in learning Elixir. Maybe You've had some experience programming, and maybe you haven't. Maybe you've decided to try it out but you bought a book or a course that assumes you already know something, and you wanted something a little more simpler to start with. There's already a good book you can read free here. Consider this guide getting in the kiddie pool without any adults around. I'm not going to bore you with a bunch of information, just straight to the point. I also won't bore you with details of the history of Erlang, the BEAM etc. Elixir was written by José Valim and made to run on the BEAM, the Erlang virtual machine that was made to support millions of connections at once. You can get more info on it at (link)There. Now, on to the good stuff.
Elixir has a lot of other features that other languages do. However, since I'm writing this as if you've never programmed before. Hopefully when you're done with this guide, you can go off and learn from any Elixir course or book or academy and know enough to not get lost out of the gate.
Before we dive in, caveat: I'm not an Elixir expert. I'm still learning. This is just a super basics guide that I would have wanted to get rolling without too much fuss.
Here we go!
Setup
Setting up Elixir and the Erlang Virtual Machine(from here out referred to as the BEAM) is stupidly easy compared to a lot of other languages. It's even easy to install on Windows. Go here for walkthrough instructions. Seriously. Installing is easy.
Once you're done setting up, open a terminal and type:
Drew-desktop-$ iex
In the above, the Drew-desktop -$ is just what I see at my terminal prompt. Yours will be different. From here out I'll tell you what to write and omit that part. You'll just type iex and that will start an environment that looks like this:
Interactive Elixir (1.14.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
This guide will only use this interactive environment throughout, and when you see iex> as we go further, you'll type just what is on the right side of it.
The number contained within parentheses (1) is just a line number. You'll continue to see those in your terminal as you type, but I won't be showing them as your line numbers may different from mine and I don't want to confuse you.
If you're following this to learn, I expect you to type in every single example. Repetition builds muscle/brain memory. Otherwise, feel free to skim if you're just using it for reference.
Maths
As with every other language, Elixir can do math. You love math, right? Even if you don't, that's okay too. You don't need to be an expert. Math was my least favorite subject in school. Here's some math:
iex(1)> 50 + 85
135
iex(2)> 359 * 95847
34409073
iex(3)> 4459 / 2245
1.9861915367483296
iex(4)> 225 - 50
175
Notice the number in () ex. iex(3)> that is just a line number the iex puts there. From here on out I'll omit the numbers in the prompt all together to make it easier for me to write.
As you do the exercises it shows the output underneath:
iex> 50 + 85
135
Some more math exercises:
Multiply 3743 by 2.6
Divide 38473.4 by 88
Add 64422 and 998223
Elixir Math particulars:
Dividing two integers using the / operator will result in a float:
iex> 3456 / 45
76.8
If want an int instead of a float when dividing, you can use div():
iex> div(3456, 45)
76
The result is an integer. If you want the remainder you can use rem():
iex> rem(3456,45)
36
Some more maths. Use both / and div() and rem() and see what you get:
divide 2089504 into 22.
divide 482039489 by 4444444
Through Erlang, you have an entire math library to use.
Strings
A string is just some text. Any text. Text is surrounded by " " marks.
iex> "This is some text."
"This is some text."
iex> "this is a sentence I forgot to capitalize the first letter of."
"this is a sentence I forgot to capitalize the first letter of."
iex> "Where did I put my keys?"
"Where did I put my keys?"
iex> "Oh, I left them over there."
"Oh, I left them over there."
Make sure to always type the " " marks when writing text or you will get errors.
More strings:
"Where is my cheese?"
"That was a really good show."
"Oh, you have seen that one?"
Concatenation is where strings can be linked together to form one string. concatenation can be done with a greater than and less than symbol, like so: <>
iex> "John " <> "Brown"
"John Brown"
You can't concatenate anything but strings using the <> operator.
concatenate these strings:
I love
jumping on the bed
some time in june
but I hit my head.
This climb is tough
but I've not had enough
so I'll keep on climbing
while the sun is up.
Don't forget the quotes around the strings. you can insert a space at either the beginning of a string "like this " or inserting a space by itself " " like this.
This guide is not complete. I will add more as I go along.
Top comments (0)