Skip to content

Instantly share code, notes, and snippets.

@vjache
Created November 28, 2017 14:59
Show Gist options
  • Save vjache/c9dbacbf11f86fbb2e43f4037b809eab to your computer and use it in GitHub Desktop.
Save vjache/c9dbacbf11f86fbb2e43f4037b809eab to your computer and use it in GitHub Desktop.
Flatten algorithm on Erlang.
%%
%% Util module which contains a simple 'flatten list'
%% implementation which is not fastest but most comprehent.
%%
-module(util).
-export([flatten/1]).
%% Flatten empty list is an empty list.
flatten([]) ->
[];
%% If the first element of a list is an also list then
%% flatten that element and concatinate with the flattened tail.
flatten([L|Tail]) when is_list(L) ->
flatten(L) ++ flatten(Tail);
%% If the first element is not a list then just cons that element with the flattened tail.
flatten([E|Tail]) ->
[E|flatten(Tail)].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment