Created
July 6, 2017 10:25
-
-
Save Ceroce/17ea78688b11ed700143e6393f843c1c to your computer and use it in GitHub Desktop.
The nub() function removes duplicates from a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module (nub). | |
-export ([nub/1, containsElement/2]). | |
% Remove duplicates in a list, keeping the order of the last occurences | |
-spec nub([T]) -> [T]. | |
nub([]) -> []; | |
nub([X|Xs]) -> | |
case containsElement(Xs, X) of | |
true -> nub(Xs); | |
false -> [X|nub(Xs)] | |
end. | |
% Whether a list contains a particular element | |
containsElement([], _Elmt) -> | |
false; | |
containsElement([X|Xs], Elmt) -> | |
(X == Elmt) orelse containsElement(Xs, Elmt). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment