August 23, 2024
P08 - Eliminate consecutive duplicates of list elements.
If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.
Example:
> p08:compress([1,1,1,2,2,3,4,4,4,4,4,4]).
[1,2,3,4]
erlang
% Eliminate consecutive duplicates of list elements.
%
-module(p08).
-export([compress/1]).
compress([A, A | Tail])->
compress([A | compress(Tail)]);
compress([A, B | Tail]) ->
[A, B | compress(Tail)];
compress(Ls) ->
Ls.