Created
March 16, 2010 18:25
-
-
Save ddossot/334328 to your computer and use it in GitHub Desktop.
Mingle n lists by successively taking elements of each list. The lists don't need to be of the same size.
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
%% Mingle n lists by successively taking elements of each list. The lists don't need to be of the same size. | |
%% @spec mingle(ListOfLists::[List::[term()]]) -> Result::[term()] | |
mingle(ListOfLists) -> | |
mingle(ListOfLists, []). | |
mingle([], Result) -> | |
lists:reverse(Result); | |
mingle(ListOfLists, Result) -> | |
{NewListOfLists, NewResult} = | |
lists:foldl(fun(List, {ListOfListsAcc, ResultAcc}) -> | |
case List of | |
[] -> {ListOfListsAcc, ResultAcc}; | |
[Head|Rest] -> {[Rest|ListOfListsAcc], [Head|ResultAcc]} | |
end | |
end, | |
{[], Result}, | |
ListOfLists), | |
% must reverse to preserve the ordering of the list of lists | |
mingle(lists:reverse(NewListOfLists), NewResult). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment