Skip to content

Instantly share code, notes, and snippets.

@50kudos
Last active March 4, 2017 09:54
Show Gist options
  • Save 50kudos/5d21d53f2f6d975ce8f80c6e00c50131 to your computer and use it in GitHub Desktop.
Save 50kudos/5d21d53f2f6d975ce8f80c6e00c50131 to your computer and use it in GitHub Desktop.
-module(w22list).
-export([take/2, nub/1]).
-spec take(integer(), [T]) -> T.
take(0, _L) -> [];
take(I, L) when I < 0 orelse I > length(L) -> L;
take(I, [H|T]) ->
[H | take(I-1, T)].
nub(L) -> nub(lists:sort(L), []).
nub([H|[]], ACC) -> lists:reverse([H | ACC]);
nub([H|T], ACC) ->
case hd(T) of
H -> nub(T, ACC);
_ -> nub(T, [H | ACC])
end.
@pichi
Copy link

pichi commented Mar 4, 2017

[H|[]] could be written as [H]

But your code changes the order of elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment