[erlang-questions] Intersection of two ordered sets

Nicholas Dronen ndronen@REDACTED
Sun May 10 19:33:04 CEST 2009


Hi:

Does this look like a sane implementation for finding the intersection
of two ordered sets?  There are implementations in the standard erlang
libraries (e.g. ordsets.erl), but they're a bit different and I wanted
to make sure I'm not doing anything wildly wrong, either in terms of
correctness or performance.  (I'm not reinventing the wheel here --
just trying to learn erlang by implementing whatever comes to mind.)

    intersection(L1, L2) -> intersection(L1, L2, []).

    intersection([H1|T1], [H2|T2], Result) when H1 == H2 ->
        intersection(T1, T2, [H1|Result]);
    intersection([H1|T1], [H2|T2], Result) when H1 > H2 ->
        intersection([H1|T1], T2, Result);
    intersection([H1|T1], [H2|T2], Result) when H1 < H2 ->
        intersection(T1, [H2|T2], Result);
    intersection(_, _, Result) -> lists:reverse(Result).

Regards,

Nick



More information about the erlang-questions mailing list