[erlang-questions] "Symmetrical" function

Michael Radford mrad-direct-erlang@REDACTED
Tue Feb 17 07:13:42 CET 2009


Richard O'Keefe writes:
> >Actually, that assumption was unnecessary.
> 
> Whether that is so depends on how the function is to be implemented.
> As several people pointed out, sometimes the answer is
> 
> 	f(X, Y) when X =< Y -> f1(X, Y);
> 	f(X, Y)             -> f1(Y, X).
> 
> If the f1 clauses are a mix of choices from both sides of the
> main diagonal of the matrix, that won't work.

I thought we were talking about the behavior of the "interleaved parse
transform method" vs. the "exception method."

(Of course if f1 is consistent with symmetry, and and it uses either of
those methods to generate the other half of its clauses, its behavior
will be the same, regardless of the method.  And f would be redundant.)

I think what you mean is that if you use the above "comparison method"
to generate half the clauses of f, you have to implement the f1 clauses
for which X <= Y.  Which is true.

[snip]
> 	oi([X|Xs], [Y|Ys]) ->
> 	    if X < Y -> oi(Xs, [Y|Ys])
> 	     ; X > Y -> oi([X|Xs], Ys)
> 	     ; true  -> oi(Xs, Ys)
> 	    end;
> 	oi(_, _) ->
> 	    [].
> 
> Here #A is infinite, the number of clauses is 2, and I don't see
> any good way to write half of f.

I think you can write just the diagonal...

oi(_, _) -> [].

Mike



More information about the erlang-questions mailing list