[erlang-questions] "Symmetrical" function
Adam Lindberg
<>
Fri Feb 13 13:37:38 CET 2009
A macro perhaps?
-define(f(Pm, Pn, Expr_m_n), f(Pm, Pn) -> Expr_m_n; f(Pn, Pm) -> f(Pm, Pn)).
So for the clause where the same patter exists twice you would write normally:
f(P1, P1) -> expr11;
And for the other clauses where you need symmetry you would write:
?f(P1, P2, expr12);
Cheers,
Adam
----- "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
>
> http://www.erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list