[erlang-questions] How about a new warning? Was: Re: trouble with erlang or erlang is a ghetto

Richard Carlsson carlsson.richard@REDACTED
Thu Aug 4 00:05:20 CEST 2011


On 08/03/2011 07:05 PM, Ulf Wiger wrote:
> On 3 Aug 2011, at 18:17, Richard Carlsson wrote:
>
>>> And did it extend to pattern matching?
>>
>> No, there was never any discussion about that. If the match
>> succeeds then it would succeed no matter what the order, and if a
>> you get a 'badmatch' error, the entire matched term is blamed, not
>> just the first subterm that doesn't match, so I can't see that
>> there would be any observable difference depending on the order of
>> matched subterms.

I forgot to clarify this. As I said in the previous mail, for an 
expression such as {f(), g()}, Erlang defines the evaluation order to be 
f() first, then g(). But in the case of pattern matching:

   {P1, P2} = SomeExpression

(where P1 and P2 can be unbound variables, bound variables, or any other 
patterns large or small, including where P1 and P2 are exactly the same) 
- if the match works, then the resulting bindings (if any) from the 
pattern as a whole must be the same regardless of the order you matched 
the subpatterns against the value of SomeExpression. And if the match 
fails, then you just see a {badmatch, SomeExpression} error which 
doesn't reveal in what part of SomeExpression the match first discovered 
that the value couldn't be matched against the pattern, even though 
there could be more than one place, as in {1,2,3}={3,2,1}. Hence, 
whether the matching tries P1 or P2 first is an internal implementation 
detail.

    /Richard



More information about the erlang-questions mailing list