Skip to content

Instantly share code, notes, and snippets.

@paucus
Created March 5, 2017 22:49
Show Gist options
  • Save paucus/4210c8449baedb4ff7bac4b5f9a2b18e to your computer and use it in GitHub Desktop.
Save paucus/4210c8449baedb4ff7bac4b5f9a2b18e to your computer and use it in GitHub Desktop.
assignment 2.11
% Marcelo Ruiz Camauër
% assignment 2.11
% Define a function take that takes the first N elements from a list.
-module(a211).
-export([take/2]).
-include_lib("eunit/include/eunit.hrl").
-spec take(integer(),[L]) -> [L].
take(_,[]) -> [];
take(0,[_]) -> [];
take(N,[H|T]) ->
case N of
0 -> [];
1 -> [H];
N -> [H]++take(N-1,T)
end.
%%%%%%%%%%%%%%% TESTS %%%%%%%%%%
% invoke with 'a211:tests().' or "eunit:test(a211)."
take_test_()->
[?_assert(take(0,"hello")==[]),
?_assert(take(4,"hello")=="hell"),
?_assert(take(5,"hello")=="hello"),
?_assert(take(9,"hello")=="hello")
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment