<div>Here is one that I use. My comments say that it comes from Haskell, but I seem to remember that I got the Erlang translation from this mailing list.</div><div><br></div><div><br></div><div>% transpose a matrix represented as list of lists</div>
<div>% aka "zip" list of lists</div><div>% This implementation originates from Haskell</div><div>transpose([[X | Xs] | Xss]) -></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>[[X | [H || [H | _] <- Xss]]</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> | transpose([Xs | [T || [_ | T] <- Xss]])];</div><div>transpose([[] | Xss]) -> transpose(Xss);</div><div>transpose([]) -> [];</div><div><br></div>
<div>transpose(Tuple) when is_tuple(Tuple) -> % wrapper to emulate zip</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Xs = transpose(tuple_to_list(Tuple)),</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>[list_to_tuple(X) || X <- Xs].</div>
<div><br></div><div><br></div><div>Dan.</div><div><br></div><div><br></div><div><br></div><br><div class="gmail_quote">On Fri, Oct 19, 2012 at 4:17 AM, Ivan Uemlianin <span dir="ltr"><<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear All<br>
<br>
Erlang provides lists:zip/2 and /3 for zipping together two or three lists. Is there anything ready available for zipping together arbitrarily many lists? e.g., taking a list of lists as its input,<br>
<br>
zipN([[1,2,3],[4,5,6],[7,8,9],<u></u>[10,11,12]]) -><br>
[[1,4,7,10],[2,5,8,11],[3,6,9,<u></u>12]].<br>
<br>
If not, I cooked up the following:<br>
<br>
zip_lol([[]|_]) -><br>
[];<br>
zip_lol(Lols) -><br>
{Lohs, Lots} = lists:foldl(fun([H|T], {Hs,Ts}) -><br>
{[H|Hs],[T|Ts]}<br>
end,<br>
{[],[]},<br>
Lols),<br>
[lists:reverse(Lohs) | zip_lol(Lots)].<br>
<br>
This doesn't quite work, as only every other Lohs seems to need reversing:<br>
<br>
zip_lol([[1,2,3],[4,5,6],[7,8,<u></u>9],[10,11,12]]) -><br>
[[1,4,7,10],[11,8,5,2],[3,6,9,<u></u>12]].<br>
<br>
Actually, it's enough for my purposes, as I only need the sum of each loh, so I can use:<br>
<br>
zipwith_lol(_, [[]|_]) -><br>
[];<br>
zipwith_lol(Fun, Lols) -><br>
{Lohs, Lots} = lists:foldl(fun([H|T], {Hs,Ts}) -><br>
{[H|Hs],[T|Ts]}<br>
end,<br>
{[],[]},<br>
Lols),<br>
[Fun(Lohs) | zipwith_lol(Fun, Lots)].<br>
<br>
zip_lol([[1,2,3],[4,5,6],[7,8,<u></u>9],[10,11,12]]) -><br>
[22,26,30].<br>
<br>
What concerns me is performance. My lols are likely to be quite large: 10 to 20 lists, and each list could be 100k+ integers long (if it were Haskell each list would be one of those lazy infinite list stream things).<br>
<br>
Is the fold the right way to do this, or have I missed something that would do the job faster?<br>
<br>
With thanks and best wishes<br>
<br>
Ivan<br>
<br>
<br>
-- <br>
==============================<u></u>==============================<br>
Ivan A. Uemlianin PhD<br>
Llaisdy<br>
Speech Technology Research and Development<br>
<br>
<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a><br>
<a href="http://www.llaisdy.com" target="_blank">www.llaisdy.com</a><br>
<a href="http://llaisdy.wordpress.com" target="_blank">llaisdy.wordpress.com</a><br>
<a href="http://github.com/llaisdy" target="_blank">github.com/llaisdy</a><br>
<a href="http://www.linkedin.com/in/ivanuemlianin" target="_blank">www.linkedin.com/in/<u></u>ivanuemlianin</a><br>
<br>
"hilaritas excessum habere nequit"<br>
(Spinoza, Ethica, IV, XLII)<br>
==============================<u></u>==============================<br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
</blockquote></div><br>