Jump to content

Cartesian product of two or more lists: Difference between revisions

→‎{{header|Tailspin}}: Use new cartesian product feature
(Added Wren)
(→‎{{header|Tailspin}}: Use new cartesian product feature)
Line 3,840:
=={{header|Tailspin}}==
<lang tailspin>
'{1,2}x{13,24} = $:[[],by [1,2]]..., ->by cartesianProduct[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
 
'{13,24}x{31,42} = $:[by [13,24]..., by [31,42]...] -> cartesianProduct;
' -> !OUT::write
 
'{31,42}x{1,2} = $:[by [31,42]..., by [1,2]...] -> cartesianProduct;
' -> !OUT::write
 
'{}x{1,2}x{} = $:[by []..., by [1,2],[...]] -> cartesianProduct;
' -> !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
 
'{17761, 1789} × {72, 123} × {4, 14, 2330} × {0500, 1100} = $:[by [17761, 1789]2, [7, 123], [4..., 14,by 23[30]..., by [0500, 1100]...] -> cartesianProduct;
' -> !OUT::write
 
'{1, 2, 3} × {30} × {500, 100} = $:[by [1, 2, 3]..., by [30]..., by [500, 100]...] -> cartesianProduct;
' -> !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
</lang>
{{out}}
<pre>
{1,2}x{3,4} = [[1, 3][2, 3][1, 4], [2, 3], [2, 4]]
{3,4}x{1,2} = [[3, 1][4, 1][3, 2], [4, 1], [4, 2]]
{1,2}x{} = []
{}x{1,2} = []
{1776, 1789} × {7, 12} × {4, 14, 23} × {0, 1} = [[1776, 7, 4, 0], [17761789, 7, 4, 10], [1776, 712, 144, 0], [17761789, 712, 144, 10], [1776, 7, 2314, 0], [17761789, 7, 2314, 10], [1776, 12, 414, 0], [17761789, 12, 414, 10], [1776, 127, 1423, 0], [17761789, 127, 1423, 10], [1776, 12, 23, 0], [17761789, 12, 23, 10], [17891776, 7, 4, 01], [1789, 7, 4, 1], [17891776, 712, 144, 01], [1789, 712, 144, 1], [17891776, 7, 2314, 01], [1789, 7, 2314, 1], [17891776, 12, 414, 01], [1789, 12, 414, 1], [17891776, 127, 1423, 01], [1789, 127, 1423, 1], [17891776, 12, 23, 01], [1789, 12, 23, 1]]
{1, 2, 3} × {30} × {500, 100} = [[1, 30, 500], [12, 30, 100500], [23, 30, 500], [21, 30, 100], [32, 30, 500100], [3, 30, 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>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.