Loop over multiple arrays simultaneously: Difference between revisions
Content deleted Content added
Line 2,903: | Line 2,903: | ||
This implementation throws an exception if the arrays are not all the |
This implementation throws an exception if the arrays are not all the |
||
same length. |
same length. |
||
=={{header|Picat}}== |
|||
Picat has a built-in zip/n and only works with lists (not arrays). It returns an array tuple ({A,B,C}) and not a list ([A,B,C]), which is a typical gotcha. |
|||
For this task, a foreach loop and list comprenhension are shown. |
|||
If the lists/arrays are of uneven lengths, then the elements in the longer arrays are skipped. |
|||
<lang Picat>import util. |
|||
go => |
|||
L1 = ["a","b","c"], |
|||
L2 = ["A","B","C"], |
|||
L3 = ["1","2","3"], |
|||
println("foreach loop:"), |
|||
foreach({A,B,C} in zip(L1,L2,L3)) |
|||
println([A,B,C].join('')) |
|||
end, |
|||
nl, |
|||
println("list comprehension/n:"), |
|||
println( [[A,B,C].join('') : {A,B,C} in zip(L1,L2,L3)].join("\n")), |
|||
nl, |
|||
% With uneven lengths the last elements in the longer lists are skipped. |
|||
println("Uneven lengths:"), |
|||
L4 = ["P","Q","R","S"], % longer than the other |
|||
foreach({A,B,C,D} in zip(L1,L2,L3,L4)) |
|||
println([A,B,C,D].join('')) |
|||
end, |
|||
nl.</lang> |
|||
Output: |
|||
<pre>foreach loop: |
|||
aA1 |
|||
bB2 |
|||
cC3 |
|||
list comprehension/n: |
|||
aA1 |
|||
bB2 |
|||
cC3 |
|||
Uneven lengths: |
|||
aA1P |
|||
bB2Q |
|||
cC3R</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |