Skip to content

Instantly share code, notes, and snippets.

@dhedlund
Created February 29, 2012 05:23
Show Gist options
  • Save dhedlund/1938130 to your computer and use it in GitHub Desktop.
Save dhedlund/1938130 to your computer and use it in GitHub Desktop.
PDXErlang intersection implementation w/ Nick
-module(intersection).
-compile(export_all).
intersection(ListA, ListB) ->
intersection([], ListA, ListB).
intersection(Acc, [], ListB) ->
Acc;
intersection(Acc, ListA, ListB) ->
[A|Rest] = ListA,
Acc1 = case match(A, ListB) of
true -> [A|Acc];
false -> Acc
end,
intersection(Acc1, Rest, ListB).
match(X, []) ->
false;
match(X, [Y|Rest]) when X == Y ->
true;
match(X, [Y|Rest]) ->
match(X, Rest).
@built
Copy link

built commented Mar 2, 2012

Clever. I like the decomposition. Worth noting that the given lists also have to be sorted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment