Skip to content

Instantly share code, notes, and snippets.

@paucus
Created March 6, 2017 21:53
Show Gist options
  • Save paucus/e147b47981deebcf222caf33768157a2 to your computer and use it in GitHub Desktop.
Save paucus/e147b47981deebcf222caf33768157a2 to your computer and use it in GitHub Desktop.
Assignment 2.13
%%% assignment 2.13
-module(a213).
-include_lib("eunit/include/eunit.hrl").
-created_by("Marcelo Ruiz Camauër").
-export([nub/1]).
%% contains() copied from Jeremiah B.:
contains([], _Y) ->
false;
contains([X|_Xs], Y) when X == Y ->
true;
contains([_X|Xs], Y) ->
contains(Xs, Y).
%% Define a function nub to remove all the duplicate elements from a list.
nub(X)->nub(X,[]).
nub([],Unique)->Unique; % nothing left to process, return list of Unique as the result.
nub([H|T],Unique)-> % is current H in list of Unique?
case contains(Unique,H) of
true -> nub(T,Unique); % H is not in Unique, so add it to Unique
false -> nub(T,lists:append(Unique,[H])) % present in Unique, so skip it and continue
end.
%%%%%%%%%%%%%%%
% invoke with 'a213:tests().' or "eunit:test(a213)."
nub_test()->
[ ?_assert(nub([2,4,1,3,3,1])==[2,4,1,3]),
?_assert(nub([])==[])
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment