[erlang-questions] "Symmetrical" function
Fri Feb 13 09:34:52 CET 2009
Maybe try this one:
f(P1, P2) when P1 > P2 ->
f(P1, P2) ->
f1(P1, P1) -> expr11;
f1(P1, P2) -> expr12;
f1(P1, P3) -> expr13;
f1(P2, P3) -> expr23;
f1(Pm, Pn) -> expr_m_n;
----- "Boris Okner" <> wrote:
> 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!
> erlang-questions mailing list
More information about the erlang-questions