Search code examples
erlangerlang-shellerlang-escript

Best way to convert list of lists to list of tuples?


What's the best way to convert a list such as [[1,2,3],[a,b,c],[4,5,6]] to a list of tuples like this:

[{1,a,4},{2,b,5},{3,c,6}]

where tuple N is composed of the Nth element from each of the three sublists? Should I use a tail recursive function, a list comprehension, or some other approach?


Solution

  • One elegant solution can be

    lol2lot([[]|_]) -> [];
    lol2lot(LoL) ->
        [ list_to_tuple([hd(L) || L <- LoL]) | lol2lot([tl(L) || L <- LoL]) ].