Learn Pattern Matching — Elixir for an absolute beginner

Puneet Tiwari
Feb 13, 2020 · 4 min read

Learning Elixir is not just about learning its syntax, it gives a whole new way of thinking.

Elixir

I expect most of you are coming from different backgrounds and learning Elixir as your second or third programming language. If you are learning it as your first programming language believe me ‘you are the chosen one! 😄’. I am a little biased here, and I think I will always be, for me Elixir was love at first sight.

It’s really interesting how a language which runs on the Erlang VM is an extremely fun and easy-to-use programming language. Developers generally avoid learning Erlang unless required as it is one of the toughest programming languages. But Writing Elixir is a joy, you can write programs that are easy-to-understand for humans and computers alike.

Unlike other programming languages, the equals(=) sign is not an assignment. Instead, it’s like an assertion. It succeeds if Elixir can find a way of making the left-hand side equal the right-hand side. Elixir calls the = symbol the match operator.

Pattern Matching

Let’s see some examples of Pattern Matching —

Match Operator

Let’s use the interactive Elixir shell. We can argue in the first line that we are assigning a value 5 to variable b.

But as we move to the next line we can see executing , is not an error which is actually an error in many programming languages.

In the third line executing is actually an error, the one and only reason for the error is that there is no matching value of 10 on the right-hand side because b is actually equals to 5. So, here argument comes to an end that it may look like an assignment operator but behaves in a totally different way. That’s called match operator.

  • *one thing to keep in mind is that a variable can only be assigned on the left side of .

Let’s do a complex match, comparing different types —

Pattern Matching

Let’s analyse above commands —

  • In the first line, we are assigning to a variable
  • The second command is actually matching both the sides and assigning values .

If you are from the Javascript background like me, when we do similar things there we call them Destructuring. But believe me its way ahead than that. Let me show you how.

Head and Tail

Now let’s analyse this one

  • the first value of the list i.e. 1 will get assign to head variable.
  • Rest all will assign to variable tail in list format.

Pattern matching allows developers to easily destructure data types such as tuples and lists.

Now let’s see something more interesting Variables in Elixir can be rebound:

The pin operator —

^ Pin Operator
  • Assigning 1 to b, seems fine.
  • Assigning 2 to b, even this is cool.
  • In third command matching b and 2, both have the same value so its a match.
  • But here comes the pin operator , now it's an error.

It's an error because pin operator binds the last assigned value of variable and matches against it rather than assigning a new value to it.

Use the pin operator when you want to pattern match against an existing variable’s value rather than rebinding the variable

Its time to meet special variable of Elixir i.e. underscore (

Yes, it is, underscore is really a special variable, it acts as a variable but immediately discards any value given to it — in a pattern match, it is like a wildcard saying, “I’ll accept any value here.” Isn’t it awesome!!

Underscore variable
  • In the first line, we are comparing both sides.
  • gets a value of 1
  • gets assign to underscore variable which accepts any value
  • So basically, with the help of underscore variable, we ignored the values which we don’t require.
  • In the last line, we tried to read the value of the underscore variable which thrown an error.

The variable is special in that it can never be read from. Trying to read from it gives a compile error.

I think now you have a better understanding of how pattern matching works and what it is. This finishes our topic pattern matching.

As always, thanks for reading😊.

Quick Code

Find the best tutorials and courses for the web, mobile…

Puneet Tiwari

Written by

Let’s learn it together

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Puneet Tiwari

Written by

Let’s learn it together

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store