Reference material on pattern matching precedence order?

Yes, this is how it works. It means that each pattern will be matched against the term. There is an implicit grouping (Pattern0 = (Pattern2 = … (PatternN = Term)…)) so if variables occur in many the binding behaves as the matching goes from the inside out. Fro example:

iex(1)> {a,b} = {b,a} = {3,4}
{3, 4}
iex(2)> a
3
iex(3)> b
4
iex(4)> {b,a} = {a,b} = {3,4}
{3, 4}
iex(5)> a
4
iex(6)> b
3

If you pin (’^’ ) a variable in any of the patterns then this importing of the value occurs before any matching:

iex(7)> {^x,y} = {y,x} = {3,4}
** (CompileError) iex:7: unbound variable ^x
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
iex(7)> {x,y} = {y,^x} = {3,4}
** (CompileError) iex:7: unbound variable ^x
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    (stdlib) lists.erl:1355: :lists.mapfoldl/3

(Erlang does not have this “issue” of ordering as variables can’t be rebound.)

6 Likes