Anonymous user
Loop over multiple arrays simultaneously: Difference between revisions
Loop over multiple arrays simultaneously (view source)
Revision as of 23:31, 9 August 2009
, 14 years agoAdded Erlang
(→E: new example) |
(Added Erlang) |
||
Line 69:
(This will stop when the end of the shortest collection is reached.)
=={{header|Erlang}}==
Shortest option:
<lang erlang>
lists:zipwith3(fun(A,B,C)-> io:format("~s~n",[[A,B,C]]) end, "abc", "ABC", "123").
</lang>
However, as every expression in Erlang has to return something, printing text returns 'ok'. A list with as many 'ok's as there are lines printed will thus be created.
The technically cleanest way to do things would be with <tt>lists:foreach/2</tt>, which also guarantees evaluation order:
<lang erlang>
lists:foreach(fun({A,B,C}) -> io:format("~s~n",[[A,B,C]]) end,
lists:zip3("abc", "ABC", "123")).
</lang>
If the lists are not all the same length, an error is thrown.
=={{header|Haskell}}==
|