Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bresson
Created June 3, 2020 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bresson/6fba32586f5ff3c3f4f36bce673d38e6 to your computer and use it in GitHub Desktop.
Save bresson/6fba32586f5ff3c3f4f36bce673d38e6 to your computer and use it in GitHub Desktop.
% 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