[erlang-questions] "Symmetrical" function

Boris Okner boris.okner@REDACTED
Fri Feb 13 09:27:29 CET 2009


This must be a trivial question, and it's probably more related to  
functional programming in general, rather then to Erlang.
I have a function f(A,B) for which f(A,B) is ALWAYS equivalent to  
f(B,A).  So I call it "symmetrical".
Arguments A and B  are patterns, and f is legal for some of  
combinations of A and B.
So I have a (long) list of clauses like so:
f(P1, P1) -> expr11;
f(P1, P2) -> expr12;
f(P1, P3) -> expr13;
f(P2, P3) -> expr23;
.......
f(Pm, Pn) -> expr_m_n;

%Other pairs are illegal
f(_, _) ->throw(illegalPairException).

My problem that I don't want to manually write  clauses like f(P2,  
P1), f(P3, P1) etc., because symmetrical cases would have been already  
described (i.e. f(P1, P2) is equivalent to f(P2,P1)).
And I can't use
f(A, B) -> f(B,A)
because "illegal"  clause already covers this case.

Thank you for suggestions!




More information about the erlang-questions mailing list