Skip to content

Instantly share code, notes, and snippets.

@i7an
Created June 28, 2017 08:51
Show Gist options
  • Save i7an/8de54678f5b75400a510a25e5c0dcae7 to your computer and use it in GitHub Desktop.
Save i7an/8de54678f5b75400a510a25e5c0dcae7 to your computer and use it in GitHub Desktop.
-module(main).
-export([take/2, take_test/0]).
-spec take(integer(), [T]) -> [T].
take(N, List) when N >= 0 -> take(N, List, []).
take(0, _List, Acc) -> Acc;
take(_N, [], Acc) -> Acc;
take(N, [X|Xs], Acc) -> take(N-1, Xs, Acc ++ [X]).
take_test() ->
[] = take(0, [1]),
[] = take(1, []),
[1,2] = take(2, [1,2,3]),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment