Created
November 28, 2017 14:59
-
-
Save vjache/c9dbacbf11f86fbb2e43f4037b809eab to your computer and use it in GitHub Desktop.
Flatten algorithm on Erlang.
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
%% | |
%% 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