[erlang-questions] "Symmetrical" function

Hynek Vychodil vychodil.hynek@REDACTED
Fri Feb 13 11:04:13 CET 2009


You can try something like this

f(A,B) -> try f1(A,B) catch error:function_clause -> f1(B,A) end.

f1(P1, P1) -> expr11;
f1(P1, P2) -> expr12;
f1(P1, P3) -> expr13;
f1(P2, P3) -> expr23;
.......
f(Pm, Pn) -> expr_m_n.

On Fri, Feb 13, 2009 at 9:27 AM, Boris Okner <boris.okner@REDACTED> 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
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill your
boss.  Be a data hero!
Try Good Data now for free: www.gooddata.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090213/957483dd/attachment.htm>


More information about the erlang-questions mailing list