Matching elements of records

Richard A. O'Keefe <>
Fri Oct 29 03:53:47 CEST 2004


"Ulf Wiger (AL/EAB)" <> wrote:
	After some digging, I was able to locate a previous post
	by Robert Virding, who also has strong opinions on style. ;-)
	On this particular issue, the two of you seem to be in 
	strong disagreement.
	
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.  The Ada designers
were _right_ to use different symbols for associating a value with a
field (=>) and assignment (:=).  The designer of S similarly got it
right with "=" for (the analogue of) field binding and "<-" for assignment,
and it's a crying shame that the C monoglots have forced current
implementations of the S language to accept "=" for assignment as well,
to the great detriment of readability.

That leaves (1) and (2).
Now Virding doesn't actually give a *reason* for preferring
<pattern> = <variable> to <variable> = <pattern> in the message

http://www.erlang.org/ml-archive/erlang-questions/200011/msg00200.html
	
cited.  You might think otherwise, but bear with me.  The main thing is
that he says

	"I personally can't conceive why anyone would write aliases
	 in patterns in that [var=[at] direction".

Well, I've given two reasons, and I'm about to give a third.

(A) Consistency with other functional programming languages, specifically
    ML		
	fun foo (x as (_::_)) = length x	(* legal *)
	  | foo []            = ~1

	fun bar ((_::_) as x) = length x	(* illegal *)
	  | foo []            = ~1

    Haskell
	foo x@(_:_) = length x			-- legal
	foo []      = -1

	bar (_:_)@x = length x			-- illegal
	bar []      = -1

    Clean
	foo x=:[_:_] = length x			// legal
	foo []       = -1
	
	bar [_:_]=:x = length x			// illegal
	bar []	     = -1

(B) The linguistic argument.  In fact this has two versions.  One is
    "heavy constituent comes later".  The other is "most salient    
    constituent comes first".  To me, when you have an as-pattern,
    the most salient part is the name.  That's the "topic"; the
    restrictive pattern is the "comment".

(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.




More information about the erlang-questions mailing list