Cartesian product of two or more lists: Difference between revisions

Content added Content deleted
(Added Wren)
(→‎{{header|Tailspin}}: Use new cartesian product feature)
Line 3,840: Line 3,840:
=={{header|Tailspin}}==
=={{header|Tailspin}}==
<lang tailspin>
<lang tailspin>
'{1,2}x{3,4} = $:[by [1,2]..., by [3,4]...];
templates cartesianProduct
' -> !OUT::write
{ product: [$(1)... -> [$]], rest: $(2..last) } -> #
when <{ rest: <[](0)> }> do $.product !
otherwise def m: $.rest(1);
{ product: [$.product... -> \(def n: $; $m... -> [$n..., $] !\)], rest: $.rest(2..last) } -> #
end cartesianProduct


'{1,2}x{3,4} = $:[[1,2],[3,4]] -> cartesianProduct;
'{3,4}x{1,2} = $:[by [3,4]..., by [1,2]...];
' -> !OUT::write
' -> !OUT::write


'{3,4}x{1,2} = $:[[3,4],[1,2]] -> cartesianProduct;
'{1,2}x{} = $:[by [1,2]..., by []...];
' -> !OUT::write
' -> !OUT::write


'{1,2}x{} = $:[[1,2],[]] -> cartesianProduct;
'{}x{1,2} = $:[by []..., by [1,2]...];
' -> !OUT::write
' -> !OUT::write


'{1776, 1789} × {7, 12} × {4, 14, 23} × {0, 1} = $:[by [1776, 1789]..., by [7, 12]..., by [4, 14, 23]..., by [0, 1]...];
'{}x{1,2} = $:[[],[1,2]] -> cartesianProduct;
' -> !OUT::write
' -> !OUT::write


'{1776, 1789} × {7, 12} × {4, 14, 23} × {0, 1} = $:[[1776, 1789], [7, 12], [4, 14, 23], [0, 1]] -> cartesianProduct;
'{1, 2, 3} × {30} × {500, 100} = $:[by [1, 2, 3] ..., by [30]..., by [500, 100]...];
' -> !OUT::write
' -> !OUT::write


'{1, 2, 3} × {30} × {500, 100} = $:[[1, 2, 3], [30], [500, 100]] -> cartesianProduct;
'{1, 2, 3} × {} × {500, 100} = $:[by [1, 2, 3]..., by []..., by [500, 100]...];
' -> !OUT::write
' -> !OUT::write


// You can also generate structures with named fields
'{1, 2, 3} × {} × {500, 100} = $:[[1, 2, 3], [], [500, 100]] -> cartesianProduct;
'year {1776, 1789} × month {7, 12} × day {4, 14, 23} = $:{by [1776, 1789]... -> (year:$), by [7, 12]... -> (month:$), by [4, 14, 23]... -> (day:$)};
' -> !OUT::write
' -> !OUT::write
</lang>
</lang>
{{out}}
{{out}}
<pre>
<pre>
{1,2}x{3,4} = [[1, 3], [1, 4], [2, 3], [2, 4]]
{1,2}x{3,4} = [1, 3][2, 3][1, 4][2, 4]
{3,4}x{1,2} = [[3, 1], [3, 2], [4, 1], [4, 2]]
{3,4}x{1,2} = [3, 1][4, 1][3, 2][4, 2]
{1,2}x{} = []
{1,2}x{} =
{}x{1,2} = []
{}x{1,2} =
{1776, 1789} × {7, 12} × {4, 14, 23} × {0, 1} = [[1776, 7, 4, 0], [1776, 7, 4, 1], [1776, 7, 14, 0], [1776, 7, 14, 1], [1776, 7, 23, 0], [1776, 7, 23, 1], [1776, 12, 4, 0], [1776, 12, 4, 1], [1776, 12, 14, 0], [1776, 12, 14, 1], [1776, 12, 23, 0], [1776, 12, 23, 1], [1789, 7, 4, 0], [1789, 7, 4, 1], [1789, 7, 14, 0], [1789, 7, 14, 1], [1789, 7, 23, 0], [1789, 7, 23, 1], [1789, 12, 4, 0], [1789, 12, 4, 1], [1789, 12, 14, 0], [1789, 12, 14, 1], [1789, 12, 23, 0], [1789, 12, 23, 1]]
{1776, 1789} × {7, 12} × {4, 14, 23} × {0, 1} = [1776, 7, 4, 0][1789, 7, 4, 0][1776, 12, 4, 0][1789, 12, 4, 0][1776, 7, 14, 0][1789, 7, 14, 0][1776, 12, 14, 0][1789, 12, 14, 0][1776, 7, 23, 0][1789, 7, 23, 0][1776, 12, 23, 0][1789, 12, 23, 0][1776, 7, 4, 1][1789, 7, 4, 1][1776, 12, 4, 1][1789, 12, 4, 1][1776, 7, 14, 1][1789, 7, 14, 1][1776, 12, 14, 1][1789, 12, 14, 1][1776, 7, 23, 1][1789, 7, 23, 1][1776, 12, 23, 1][1789, 12, 23, 1]
{1, 2, 3} × {30} × {500, 100} = [[1, 30, 500], [1, 30, 100], [2, 30, 500], [2, 30, 100], [3, 30, 500], [3, 30, 100]]
{1, 2, 3} × {30} × {500, 100} = [1, 30, 500][2, 30, 500][3, 30, 500][1, 30, 100][2, 30, 100][3, 30, 100]
{1, 2, 3} × {} × {500, 100} = []
{1, 2, 3} × {} × {500, 100} =
year {1776, 1789} × month {7, 12} × day {4, 14, 23} = {day=4, month=7, year=1776}{day=4, month=7, year=1789}{day=4, month=12, year=1776}{day=4, month=12, year=1789}{day=14, month=7, year=1776}{day=14, month=7, year=1789}{day=14, month=12, year=1776}{day=14, month=12, year=1789}{day=23, month=7, year=1776}{day=23, month=7, year=1789}{day=23, month=12, year=1776}{day=23, month=12, year=1789}
</pre>
</pre>