Skip to content

Commit

Permalink
feat: implement list manipulation operations
Browse files Browse the repository at this point in the history
  • Loading branch information
yagogarea committed Jun 11, 2024
1 parent 879737d commit 126518a
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions apps/04-manipulacion-de-listas/src/manipulating.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,70 @@
%% @spec filter(List :: [integer()], N :: integer()) -> [integer()]
%% @end
%%--------------------------------------------------------------------
filter(List, N) when is_integer(N) ->
filter(List, N, []);
filter(_List, _N) ->
erlang:throw(not_implemented).
erlang:throw("NAN").

filter([], _N, Acc) ->
Acc;
filter([H|T], N, Acc) when H > N ->
filter(T, N, Acc);
filter([H|T], N, Acc) ->
filter(T, N, Acc ++ [H]).


%%--------------------------------------------------------------------
%% @doc Reverse function.
%% @spec reverse(List :: [integer()]) -> [integer()]
%% @end
%%--------------------------------------------------------------------
reverse(_List) ->
erlang:throw(not_implemented).
reverse(List) ->
reverse(List, []).
reverse([], Acc) ->
Acc;
reverse([H|T], Acc) ->
reverse(T, [H|Acc]);
reverse(_, _) ->
erlang:throw("Not a list").

%%--------------------------------------------------------------------
%% @doc Concatenate function.
%% @spec concatenate(ListOfLists :: [[integer()]]) -> [integer()]
%% @end
%%--------------------------------------------------------------------
concatenate(_ListOfLists) ->
erlang:throw(not_implemented).
concatenate(List)->
Result = concatenate(List, []),
reverse(Result).

concatenate([], Acc) ->
Acc;
concatenate([H|T], Acc) when is_list(H)->
concatenate(T, aux(H, Acc));
concatenate(_ListOfLists, _) ->
erlang:throw("Not a list of lists").

aux([], Acc) ->
Acc;
aux([H|T], Acc) ->
aux(T, [H|Acc]).

%%--------------------------------------------------------------------
%% @doc Flatten function.
%% @spec flatten(DeepList :: [integer() | list()]) -> [integer()]
%% @end
%%--------------------------------------------------------------------
flatten(_DeepList) ->
erlang:throw(not_implemented).

flatten(List) ->
Result = flatten(List, []),
reverse(Result).

flatten([], Acc) ->
Acc;
flatten([H|T], Acc) when is_list(H)->
flatten(T, flatten(H, Acc));
flatten([H|T], Acc) ->
flatten(T, [H|Acc]);
flatten(_List, _) ->
erlang:throw("Not a list").

0 comments on commit 126518a

Please sign in to comment.