Created
June 3, 2020 01:05
-
-
Save bresson/6fba32586f5ff3c3f4f36bce673d38e6 to your computer and use it in GitHub Desktop.
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
% Erlang "take" function in direct and tail recursion | |
-module(take). | |
-export([take/2, tctake/2, test_tctake/0]). | |
take(0,_) -> []; | |
take(_, []) -> []; | |
take(N, [X | _Xs]) -> | |
[X | take(N-1, _Xs)]. | |
tctake_helper(0, _, Acc) -> Acc; | |
tctake_helper(_N, [], Acc) -> Acc; | |
tctake_helper(N, [X | Xs], Acc) -> tctake_helper(N-1, Xs, [Acc | X]). | |
tctake(0, _) -> []; | |
tctake(N, [X | Xs]) -> tctake_helper(N, Xs, X). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment