Erlang Central

Traversing Multiple Lists

From ErlangCentral Wiki


You want to traverse multiple source lists in a nested fashion, but do not want to define nested functions.

For example, given two lists [a,b,c] and [1,2], you want to apply a function to each of [a,1], [a,2], [b,1], [b,2], [c,1], and [c,2].

In English, this problem might be something like "for each product, for each day of the week, produce a report of sales".

Using nested functions, a solution might look like this:

FunProd = fun(Product) ->
    FunDay = fun(Date) ->
        print_sales([Product, Date])
    lists:foreach(FunDay, get_current_week())
lists:foreach(FunProd, get_product_list()).

While looping through two lists is not excessively bad, looping through four or five lists rapidly becomes very ugly.

Solution 1

Convert a source list of lists into a target list of lists, then apply one of the map, foreach, foldl (etc) functions to it.

The new loop_lists function should behave as follows:

1> loop_lists([[spanner, screwdriver],[monday,tuesday,wednesday]]).

The function should be generic enough to handle any number of source lists; the number of elements in each target list will be equal to the number of source lists.

2> loop_lists([[a,b],[x,y],[1,2,3]]).

The function would be used with the map/foreach/fold functions, or with list comprehensions.

> lists:map(MyFunction, loop_lists([ProductList,DayList])).
> [MyFunction(ProdDay) || ProdDay <- loop_lists([ProductList,DayList])].


loop_lists([]) -> [[]];
loop_lists([H|[]]) -> [[X] || X <- H];
loop_lists([H|T]) -> [[X|Y] || X <- H, Y <- loop_lists(T)].

This version works, but is not tail-recursive.


loop_lists(L) -> loop_lists(L, []).
loop_lists([], Acc) -> Acc;
loop_lists([H|T], []) -> loop_lists(T, [[X] || X <- H]);
loop_lists([H|T], Acc) -> loop_lists(T, [lists:append(X, [Y]) || X <- Acc, Y <- H]).

This version is tail-recursive, but uses lists:append, which may have a significant performance impact with larger lists.


loop_lists(L) -> loop_lists(lists:reverse(L), []).
loop_lists([], Acc) -> Acc;
loop_lists([H|T], []) -> loop_lists(T, [[X] || X <- H]);
loop_lists([H|T], Acc) -> loop_lists(T, [[X|Y] || X <- H, Y <- Acc]).

This version is tail-recursive and does not use lists:append. It does use lists:reverse once on the initial source list so that the target lists are accumulated in the correct order.

This version is probably the best of attempts 1-3, but still requires the same amount of memory to hold the resulting looping list.

Solution 2

Recursively apply one of the map, foreach (etc) functions from the lists module to each source list, passing the remaining source lists and the accumulated argument list through to each call.

loop_lists(Fun, Lists) -> loop_lists(foreach, Fun, Lists, []). % Use foreach by default.
loop_lists(Method, Fun, Lists) -> loop_lists(Method, Fun, Lists, []). % User-selected list function.
loop_lists(_Method, Fun, [], Args) -> Fun(Args);
loop_lists(Method, Fun, [H|T], Args) ->
    FunLoop = fun(Elem) -> loop_lists(Method, Fun, T, Args ++ [Elem]) end,
    apply(lists, Method, [FunLoop, H]).

1> loop_lists(fun(X) -> io:format("~w~n", [X]) end, [[a,b,c],[x,y],[1,2]]).

I am not sure if this is properly tail-recursive (it would depend on the underlying implementation of lists:map, lists:foreach, etc).

Solution 2 does seem to be a bit more readable than the Solution 1 attempts, but that is probably only my inexperience with list comprehensions.

I have yet to actually analyse and compare these pieces of code for performance, memory usage, etc.

Using loop_lists (either version), looping through multiple lists becomes more concise and - hopefully - clearer:

% Using Nested Functions:
Fun1 = fun(Elem1) ->
    Fun2 = fun(Elem2) ->
        Fun3 = fun(Elem3) ->
            do_something([Elem1, Elem2, Elem3])
        lists:foreach(Fun3, List3)
    lists:foreach(Fun2, List2)
lists:foreach(Fun1, List1).

% Solution 1:
    fun(X) ->
    loop_lists([List1, List2, List3])).

% Solution 2:
    fun(X) ->
    [List1, List2, List3]).