Created
March 1, 2016 11:07
-
-
Save jaseemabid/03226a107b06e9f1e1ee to your computer and use it in GitHub Desktop.
Tain truncated list with some basic tests.
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
%% 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