[erlang-questions] : eep: multiple patterns

Andras Georgy Bekes <>
Thu May 15 16:24:59 CEST 2008


>   i find virding's suggestion [1] much nicer though;
>
> foo(Pat11, ...) when Guard1 ;
> foo(Pat12, ...) when Guard2 ;
> foo(Pat13, ...) when Guard3 ->
>     ...;
>
> case ... of
>     Pat1 when Guard1 ;
>     Pat2 when Guard2 -> ...;
>     ...
> end
Well, what I suggest differs only in a ; <=> | change.
I think ; is more confusing, because it already has a use.

>   as for the "~=" operator; i guess i don't see the point.
Few examples:

==================================================
Negated patterns:
---------------------------
case Expression of
    {X,Pattern} when not (X~=Pattern2) ->
	action1(X,Variables_in_Pattern);
    _ ->
	action2() %think lots of code here
end
---------------------------

The same Without ~= :
---------------------------
case Expression of
    {Pattern2,Pattern} ->
	action2(); %think lots of duplicated code here
    {X,Pattern} ->
	action1(X,Variables_in_Pattern);
    _ ->
	action2() %think lots of duplicated code here
end

==================================================
Conditional exception throwing:
---------------------------
(Pattern ~= Expression) andalso throw(Exception)
---------------------------
OR:
---------------------------
not (Pattern ~= Expression) andalso throw(Exception)
---------------------------

==================================================
Pattern-testing in short-circuit logic expressions:
---------------------------
case (Pattern1~=f1()) andalso (Pattern2~=f2()) of
   true ->
      action1();
   false ->
      action2()
end
---------------------------

Without ~= :
---------------------------
case f1() of
   Pattern1 ->
      case f2() of
         Pattern2 ->
            action1();
         _ ->
            action2()
      end;
   _ ->
     action2()
end
---------------------------

	Georgy



More information about the erlang-questions mailing list