A Gentle Introduction to Elixir

Home / Developer Tools / A Gentle Introduction to Elixir
A Gentle Introduction to Elixir

Functional programming languages are one of the programming trends nowadays. We see several imperative languages influenced by functional programming, being this javascript, ruby, python and even php.

The problem is that the majority of programmers don’t take the initiative to learn a full functional programming language, missing the opportunity to expand their technical knowledge and add more tools to their problem solving capabilities.

Well I am not going to lie to you, you need to put some effort here in order to do something useful with a functional programming language. But I must say that learning a functional programming language can be a fun and enriching experience. It will going to change the way you solve the problems.

1. Why Elixir?

When I was analysing all the possibilities to enter this world, I felt in love with Haskell idea. Haskell is a full functional programming language with very interesting concepts, but it has a academic connotation and I wanted to learn something useful to apply at my work.

Don’t get me wrong, you can do something useful with Haskell, I even remember a freelancer that made a lot of money programming API’s with Haskell.

From all the other programming languages the one that excited me more was Elixir. It was developed by José Valim, a Brazilian programmer that came from the Ruby community. The good thing about Elixir is that it haves a mature web framework called Phoenix, and after you get in the concepts you can start developing real world applications.

With the execution of a command, you install a full functional framework to develop a web application.

Well this post is getting big quickly so let’s explore the basics of the language.

2. Types and Pattern Matching

As expected elixir allows common operations with the same types we are used to see in programming languages:

iex(1)> 1 + 1 + 1 #int
3
iex(2)> "Stupid Gopher" #string
"Stupid Gopher"
iex(3)> [1, 2, 3, 4, 5, 6, 7] #list
[1, 2, 3, 4, 5, 6, 7]

Next, I want to highlight a type that you may not be used to, unless you come from ruby:

iex(4)> :atom
:atom

An atom is a constant whose name is its own value.

Next let’s do a simple operation:

iex(5)> a = 4
4

What is this operation? If you come from a imperative language you would say that it is an assignment. But it’s wrong.

This is called pattern matching, but what is pattern matching, wtf there is an = in that operation.

Don’t worry I’m going to show you what is pattern matching, to understand better let’s see another example:

iex(6)> [1, 2] = [1, 2]
[1, 2]
iex(7)> [1, 3] = [1, 2]
** (MatchError) no match of right hand side value: [1, 2]

In the first instruction the left operand is equal to the right operand and Elixiraccepts this returning in the end the value of both operands.

In the second instruction there is a difference and Elixir complaints that there is no match between the operands.

So the next example:

iex(8)> [1, x] = [1, 2]
[1, 2]
iex(9)> x
2

I think you realised what pattern matching is about.

  • First elixir matches the left hand operator with the right operator
  • If it matches and there is a variable it will match the variable with the correspondent value in the right side

So after x assume the value 2 if we do this:

iex(10)> [1, x] = [1, 3]
iex(11)> x
3

And what if we want just to match the left operand with the right operand maintaining the x value? ^x to the rescue:

iex(11)> [1, ^x] = [1, 3]
iex(12)> x
3
iex(10)> [1, ^x] = [1, 2]
** (MatchError) no match of right hand side value: [1, 2]

To end this introduction, I just want to leave you with one more example to see how powerful pattern matching is.

In elixir if we want to concatenate strings we use <> operator:

iex(11)> "Stupid"<>" "<>"Gopher"
"Stupid Gopher"

Let’s do pattern matching with strings =)

iex(12)> "Stupid"<>" "<>x = "Stupid Gopher"
"Stupid Gopher"
iex(13)> x
"Gopher"

Source: medium