By Stepan Mitkin
Part 8 of 10
Pattern matching is considered a very useful feature in programming languages. What are the main uses of pattern matching in Erlang?
Taking to pieces a complex value. Pattern matching is the primary way to get the elements of lists and tuples in Erlang. The following line puts the head of SomeList into First. The tail of SomeList goes to Rest.
[First | Rest] = SomeList.
Another example. Let us assume that the Person variable has this value:
Person = {person, "John", "Malkovich", {date, 1953, 12, 9}}.
The following statement extracts the first name, the second name and the year of birth from Person.
{_, FirstName, SecondName, {_, Year, _, _}} = Person.
FirstName becomes "John". SecondName becomes "Malkovich". Year becomes 1953.
Asserting assumptions. An expression with pattern matching will throw an exception if the expected value is not equal to the actual one. Erlang is very good at dealing with exceptions and recovery. That is why asserting is especially helpful in Erlang.
{person, FirstName, "Malkovich", {date, Year, _, _}} = Person.
The statement above has two effects:
Branching. Pattern matching can work as an if or switch statement in disguise.
feed({person, FirstName, SecondName, _}) ->
give_money(FirstName, SecondName);
feed({animal, Name}) ->
give_food(Name).
Here, the feed function checks whether its argument is a person or an animal and chooses what to do next based on that.
There is one more way to use pattern matching for branching.
feed(Creature) ->
case Creature of
{person, FirstName, SecondName, _} ->
give_money(FirstName, SecondName);
{animal, Name} ->
give_food(Name)
end.
How does pattern matching work in DRAKON-Erlang?
The first two application of this technique work exactly as they do in the standard Erlang.
But branching is different. DRAKON-Erlang does not use pattern matching for branching. Selecting an algorithm path based on a condition is done either with an "If" or a "Switch" icon. Choosing "If" or "Switch" depends on the nature of the condition. If the condition can be either true or false, then the "If" icon is used. If the condition is a multiple-choice question, there goes the "Switch" icon.
In the following example pattern matching is used only for element extraction and for assertion. Branching is done explicitly with a "Switch" construct.
Why is this limitation present in DRAKON-Erlang?
A picture is better then text. Branching is central to algorithms. It is also the main source of complexity. DRAKON always strives to make important and complex things immediately visible. This is why branching is represented with visual icons. The traditional text-based forms of branching may look shorter. But they are harder to read. DRAKON's philosophy is against making branching implicit and disguised.
Simplicity. DRAKON-Erlang is a simpler language than the traditional Erlang. Erlang has many ways to do branching:
DRAKON has only two: "If" and "Switch". Twice as simple.
Pattern matching can get hard to read. In some non-trivial cases it is hard to compare patterns. Especially if they have several elements which are similar.
[foo, bar, X, Y, _, Z]
[foo, dar, X, _, m, _]
Calculating the difference between patterns can become hard mental work. DRAKON believes that all unnecessary work must be eliminated.
One important exception is the receive statement. Since this statement is very special in Erlang, it should be written in the text form as usual. But if an application is based on OTP there should not be any receive statements anyway.
Branching is making decisions. Decisions are important in programming. That is why DRAKON focuses on representing decisions in a clear and consistent way.
Contact: drakon.editor@gmail.com