Matching elements of records

Joe Armstrong joe@REDACTED
Fri Oct 29 12:47:27 CEST 2004


...

> OK.  First he notes that there are three uses of "=" in Erlang.
> (1) <pattern> = <expression>		"LET"
> (2) <pattern> = <pattern>		"AS-pattern"
> (3) #<record>(<field> = <whatever>)	"field"
>
> Let's dispose of (3) first.  I firmly believe that it is a mistake to
> use the same symbol for (3) as for the other purposes.

I agree - when we made the record syntax we all agreed that the syntax was
terrible but we couldn't at the time think of anything better.

...

> (C) CONSISTENCY WITH USE (1) OF = IN ERLANG.
>
>    To me it is simply inconsistent to bind X in the body of a function
>    by writing
> 	X = [Y|Ys]
>    and then to turn around and do the exact OPPOSITE in the head of
>    a function by writing
> 	[Y|Ys] = X
>
>    When he wrote
>
> 	Writing it this way means you have the same semantics as for a normal
> 	explicit match, take the value of "A" and match it with the record ...
> 	This then only gives two uses.
>
>    Robert was, to my mind, confused.  Because writing the variable to be
>    bound on the right of the = is *NOT* "the same semantics as for a
>    normal match" but in fact the very direct opposite.
>
>    (This is the new reason I promised.)
>
>    I agree 100% with Robert Virding that consistency in the uses of =
>    is a good thing, but that is a reason why variable-on-the-right is
>    a BAD thing and should never have been allowed.

I don't really see it like that. In Erlang you can write

 	Pattern1 = Pattern2 = Pattern3 = .... = X

Where X is a ground term or something which evaluates to a ground term.

I view writing

 	...
 	foo(X)
 	...

and

 	foo(Pattern1 = Pattern2 = Pattern3...)

as a kind of syntactic sugar for

 	Pattern1 = Pattern2 = Pattern3... = X

so if I call a function with a list

 	foo([hi,joe])

and write a head pattern like this:

 	foo(X=[H|T])

I pattern match X = [H|T] = [hi,joe] which is the same as
[H|T] = X = [hi,joe]

So from that point of view I don't really care about the order of the
patterns.

So in function bodies X1 = X2 = ... = Xn all the X(i)'s except Xn are patterns
and Xn is an expression, whereas in function head arguments of the form
foo(A1, A2, ...,An) -> all the Ai's are patterns.

I personally don't find this usage confusing.

Cheers

/Joe







More information about the erlang-questions mailing list