[erlang-questions] Bug ?!

Richard Carlsson richardc@REDACTED
Tue Oct 3 14:47:46 CEST 2006


Mats Cronqvist wrote:
> perhaps richard has an example of such an "unreadable guard"?

Take a clause like the following:

     {frob,Xyzzy#plugh.ouch*4711+length(Stuff),Fred} when Fred > 0 ->

First of all, it gets pretty hard to see what the _structure_ of the 
thing is (a tuple with three elements). If you rewrite it:

     {frob,N,Fred} when N =:= Xyzzy#plugh.ouch*4711+length(Stuff),
                        Fred > 0 ->

it is easier on the eyes, I hope you agree. But that might not be the 
best argument, since it is quite possible to write complicated patterns 
using only lists and tuples.

But there is also the question of variable bindings: in which cases 
should it be required that a variable is already bound, and in which 
cases can it be a new variable?

Compare:
     X * 4711
     length(Stuff)
     R#r.a
and
     [X|Y]
     {foo, X}
     "abc" ++ Xs

(We already see this problem in binary-patterns, where the size 
parameter must be bound - if it were a new variable, the pattern 
matching would have to do a linear search to find the first match.)

If we said that a pattern could be any expression, but new variables can 
only occur as arguments of primitive data constructors, not of any other 
operators or functions, it might work, but I feel that it gets too hard 
for the reader (including myself) to follow what is going on in a 
pattern when you mix computation and matching.

	/Richard




More information about the erlang-questions mailing list