Pattern Matching in Elixir is one of those things that can be vexxing, interesting, or obvious. Ultimately, however, it will be confusing if you have never dealt with it before.
Let's dive right in.
In programming, you typically assign a variable with an
= sign. In math,
= means something else entirely. It means the two terms on either side of the sign are equivalent. That's the way it is with Elixir.
When Elixir sees an
= it will try to make both sides equivalent. When you call a function, Elixir will do the same thing with the arguments you pass: pattern matching against the function's argument list.
You've already seen a little of this in the previous chapters. Let's take a look at a few more examples (feel free to open up iex and type these examples in):