Erlang Central

# Traversing Multiple Lists

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Problem

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])
end,
lists:foreach(FunDay, get_current_week())
end,
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]]).
[[spanner,monday],
[spanner,tuesday],
[spanner,wednesday],
[screwdriver,monday],
[screwdriver,tuesday],
[screwdriver,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]]).
[[a,x,1],
[a,x,2],
[a,x,3],
[a,y,1],
[a,y,2],
[a,y,3],
[b,x,1],
[b,x,2],
[b,x,3],
[b,y,1],
[b,y,2],
[b,y,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]]).
[a,x,1]
[a,x,2]
[a,y,1]
[a,y,2]
[b,x,1]
[b,x,2]
[b,y,1]
[b,y,2]
[c,x,1]
[c,x,2]
[c,y,1]
[c,y,2]
ok
```

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])
end,
lists:foreach(Fun3, List3)
end,
lists:foreach(Fun2, List2)
end,
lists:foreach(Fun1, List1).

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

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

###### Getting Started
Getting Started with Erlang
Categories
Main page
Recent changes
Random page
Help
###### Edit
View source
Editing help

New section
Printable version
Page history