Created
March 5, 2017 22:49
-
-
Save paucus/4210c8449baedb4ff7bac4b5f9a2b18e to your computer and use it in GitHub Desktop.
assignment 2.11
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
% 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