hit counter

Timeline

My development logbook

Erlang Syntax 2 - Pattern Matching and Assignment

I think it is probably a typical experience for any beginner of Erlang: The equal sign in Erlang actually works differently comparing most other imperative mainstream application. I were a little bit surprised when I first realized the subtle difference.


In erlang, the equal sign is actually an pattern matching operator. For example,

[H|T] = [0, 1, 2, 3, 4]

In the case, H is actually matched with the head of the list [0..4]. Although it effectively initialised the values of variable H and T, it is fundamentally a different concept.

Another application of this language feature is that it serves as a mean to enforce assertion. For example,

ok = module:func().

If func return an atom failure instead of ok, an exception will be automatically generated.

Isn’t it neat?