Created
July 25, 2017 07:52
-
-
Save Ehbraheem/e0e43c2c6a2919336c3e1299da2ff7ac to your computer and use it in GitHub Desktop.
'nub' function to remove all duplicate element from a list
This file contains 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 (where2). | |
-export ([nub/1]). | |
-include_lib("eunit/include/eunit.hrl"). | |
% Specification for nub | |
-spec nub([T]) -> [T]. | |
% List comprehension solution | |
nub([H|T]) -> | |
[H] ++ nub([X || X <- T, X =/= H]); | |
nub([]) -> []. | |
% Run test with where2:test(). | |
nub_test_() -> | |
[?_assertEqual([2,4,1,3], nub([2,4,1,3,3,1])), | |
?_assertEqual([1,5,8], nub([1,1,5,8,5,5,5,5])), | |
?_assertNotEqual([1,5,8], nub([1,1,5,8,2,4,5,5,5,5])), | |
?_assertNotEqual([2,4,1,3], nub([2,4,5,7,1,3,3,1]))]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment