Pattern Matching
Table of Contents

Today, we're going to explore one of Erlang's coolest features: pattern matching.

Remember this line:

3> A = 20.
** exception error: no match of right hand side value 20

That's a strange error message. We know that variables may only be assigned to once (the second rule of variables) so why doesn't Erlang just say "hey bub, A is already in use" instead? What's the point of all that nonsense about binding.

It turns out that Erlang variables do something really cool besides holding values. Erlang allows you to compare expressions on one side of an equal sign with an expression on the other side of the equal sign to see if they match. It sounds hard but it's easy in practice. Just watch."

Let's pretend that we're programming something to handle temperature. This is the classic celcius-to-Fahrenheit conversion program, which has been boring programmers for decades. Here, take a look:

Eshell V5.7.2  (abort with ^G)
1> Temp = 75.
75
2> Ideal = 50 + 25.
75
3> Ideal = Temp.
75
4> Temp = 25 * 3.
75
5> Ideal = Temp = 75.
75
6> Cold = 32.
32
7> Ideal = Cold.
** exception error: no match of right hand side value 32
8> b().
Cold = 32
Ideal = 75
Temp = 75
ok
9> q().
ok
10> clay@Hal $

Do you understand what just happened?

In line 1, we assign a value to Temp and Erlang tells us it worked by returning the value we just assigned: 75. It's important to note that the Erlang term for this operation is binding a variable. Hmmmm....binding...

In line 2, we bind a value to another variable named Ideal and Erlang once again tells us it worked by returning the value assigned. Notice how we gave Erlang a math problem to solve first. Erlang doesn't care. It solved the math problem and then returned the result to be bound to Ideal as easy as you please.

In line 3, we do something that looks like assignment, but isn't. What we do is COMPARE THEM INSTEAD.

This brings us to the third rule of variables:

VARIABLES WITH BINDINGS GET COMPARED TO SEE IF THEY MATCH

Remember, Temp and Ideal are already bound to values. That's what we did in lines 1 and 2. Since they both have values Erlang compares them instead of trying to assign them new values. In this case, Temp and Ideal match, so Erlang returns their value: 75.

In line 4, we compare Temp to the equation 25 * 3 and Erlang once again informs us that there is a match.

In line 5, we compare three values and Erlang tells us they all match. The lesson here is that you aren't just limited to two values at a time. You can chain comparisons. If they all match, Erlang returns their matching value. If something doesn't match, Erlang returns the value that doesn't match.

In line 6 and 7, we see what happens when variables don't match.

In line 8, I show you once again how valuable the b(). built-in is for keeping track of your variables, and then we quit the shell in line 9. Note how Erlang tells us the q(). command was successfully received.

Alright, we've covered variable assignment and basic pattern matching. The last thing I want to touch on are something called funs. We'll do that next time.

Next Stop: Funs