[erlang-questions] erlang *****
Andras Georgy Bekes
bekesa@REDACTED
Tue Mar 18 13:54:19 CET 2008
> However, if pattern matchings are done one-by-one and left-to-right,
> then there is no performance problem. The union operator becomes less
> flexible in this case, but it is still quite useful.
> Actually, if we want the pattern matching to be deterministic, we
> _must_ do such a restriction. For example using the patter {X,_} V
> {_X}, the value of X could be either element of the tuple. Using the
> left-to-right rule, X will always be the first element.
No.
My proposed pattern match -- with union -- is always deterministic. It
uses the first matching pattern found by a left-to-right
depth-first-search.
In the above example, {X,_} \/ {_,X} always matches X with the first
element. But matching {{a,b},b} with { {X,_} \/ {_,X}, X} matches the
second. It tries {X,_} first, succeeds, then tries matching b with a.
It fails, so backtracks and tries {_,X}.
Your proposed "one-by-one and left-to-right" method would fail when
matching {{a,b},b} with { {X,_} \/ {_,X}, X}.
They're completely different.
Georgy
More information about the erlang-questions
mailing list