First Steps with Elixir

It seems like this month a lot of friends and coworkers had started working with or learning about Elixir, and I think it’s cool to get more people interested in the language and the community, so I’ll start some posts about Elixir basics.

For starters you can install Elixir following the official documentation.

Once you have Erlang and Elixir up and running you can start an interactive shell by typing iex in your terminal

$ iex
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Interactive Elixir (1.1.1) — press Ctrl+C to exit (type h() ENTER for help)

By running iex you have started a BEAM (Erlang’s VM) instance with an Elixir shell inside it. So you can start playing around with some statements like:

iex(1)> 1 + 1

Pretty neat.

One thing that you should notice is that everything in Elixir is an expression that returns a value, this apply not only to functions but also constructs like if and case.

Playing with variables

Now that we know how to start the iex let’s get started storing some values inside variables:

iex(1)> first_variable = 1

Pretty easy right?, on the first line we bind the 1 value to the first_variable variable and the second line is the result of that expression.

Now we can ask for the value:

iex(2)> first_variable

In the first line we are writing the expression that returns the value of first_variable, and in the second one we are getting the value.

Here are some examples of different kind of names for your variables ( what you can and can’t do)

iex(10)> test_variable = 1
iex(11)> notRecomendedWay = 2
iex(12)> CantDoThis = 1
** (MatchError) no match of right hand side value: 1
iex(12)> this_is_ok? = ‘yes’
iex(13)> this_is_ok_too! = ‘yes’

That’s it for now. I’ll be writing about Modules and Functions on the next post.

Constructive feedback is always welcome

Like what you read? Give Lucas Aragno a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.