Skip to content

Instantly share code, notes, and snippets.

@jaseemabid
Created March 1, 2016 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaseemabid/03226a107b06e9f1e1ee to your computer and use it in GitHub Desktop.
Save jaseemabid/03226a107b06e9f1e1ee to your computer and use it in GitHub Desktop.
Tain truncated list with some basic tests.
%% Simple tail truncated list
-module(ttlist).
-export([new/0, new/1, insert/2, lci/1]).
-export([test/0]).
new() -> [].
new(L)
when is_list(L) ->
ordsets:from_list(L).
insert(E, L) ->
truncate(ordsets:add_element(E, L)).
lci([]) -> 0;
lci([H| _T]) -> H.
truncate([A, B | Tail])
when A + 1 == B ->
truncate([B | Tail]);
truncate(L)
when is_list(L) ->
L.
test() ->
L1 = ttlist:new([1,5,2,8,4,1]),
L1 = [1,2,4,5,8],
1 = ttlist:lci(L1),
L2 = ttlist:insert(3, L1),
[5,8] = L2,
5 = ttlist:lci(L2),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment