Jump to content

Cartesian product of two or more lists: Difference between revisions

m
(Added QBasic and Gambas)
(13 intermediate revisions by 5 users not shown)
Line 1,747:
 
[]</pre>
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight>
proc cart2 a[] b[] . p[][] .
p[][] = [ ]
for a in a[]
for b in b[]
p[][] &= [ a b ]
.
.
.
cart2 [ 1 2 ] [ 3 4 ] r[][]
print r[][]
cart2 [ 3 4 ] [ 1 2 ] r[][]
print r[][]
cart2 [ 1 2 ] [ ] r[][]
print r[][]
cart2 [ ] [ 1 2 ] r[][]
print r[][]
</syntaxhighlight>
 
=={{header|Erlang}}==
Can do this with list comprehensions.
Line 1,766 ⟶ 1,787:
[{3,1},{3,2},{4,1},{4,2}]
</pre>
 
=={{header|F Sharp|F#}}==
===The Task===
Line 2,974 ⟶ 2,996:
</pre>
=={{header|langur}}==
<syntaxhighlight lang="langur">val .X = fn(... .x) { .x }
We could use mapX() to map each set of values to a function, but this assignment only requires an array of arrays, so we use the X() function.
 
writeln mapX(.X, [1, 2], [3, 4]) == [[1, 3], [1, 4], [2, 3], [2, 4]]
{{works with|langur|0.8.3}}
<syntaxhighlight lang="langur">writeln XmapX(.X, [13, 24], [31, 42]) == [[13, 31], [13, 42], [24, 31], [24, 42]]
writeln mapX(.X([3, 4], [1, 2]) == [[3, 1], [3, 2],) [4, 1],== [4, 2]]
writeln XmapX([1.X, 2[], [1, 2]) == []
writeln X([], [1, 2]) == []
writeln()
 
writeln mapX .X, [1776, 1789], [7, 12], [4, 14, 23], [0, 1]
writeln()
 
writeln mapX .X, [1, 2, 3], [30], [500, 100]
writeln()
 
writeln mapX .X, [1, 2, 3], [], [500, 100]
writeln()</syntaxhighlight>
 
Line 3,004 ⟶ 3,025:
[]
</pre>
 
=={{header|Lua}}==
=== Functional ===
Line 3,257 ⟶ 3,279:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">cartesianProduct[args__] := Flatten[Outer[List, args], Length[{args}] - 1]</syntaxhighlight>
 
=={{header|Maxima}}==
Using built-in function cartesian_product
<syntaxhighlight lang="maxima">
cartesian_product({1,2},{3,4});
/* {[1,3],[1,4],[2,3],[2,4]} */
cartesian_product({3,4},{1,2});
/* {[3,1],[3,2],[4,1],[4,2]} */
cartesian_product({1,2},{});
/* {} */
cartesian_product({},{1,2});
/* {} */
</syntaxhighlight>
Using built-in function cartesian_product_list
<syntaxhighlight lang="maxima">
cartesian_product_list([1,2],[3,4]);
/* [[1,3],[1,4],[2,3],[2,4]] */
cartesian_product_list([3,4],[1,2]);
/* [[3,1],[3,2],[4,1],[4,2]] */
cartesian_product_list([1,2],[]);
/* [] */
cartesian_product_list([],[1,2]);
/* [] */
</syntaxhighlight>
Using built-in function create_list
<syntaxhighlight lang="maxima">
create_list([i,j],i,[1,2],j,[3,4]);
/* [[1,3],[1,4],[2,3],[2,4]] */
create_list([i,j],i,[3,4],j,[1,2]);
/* [[3,1],[3,2],[4,1],[4,2]] */
create_list([i,j],i,[1,2],j,[]);
/* [] */
create_list([i,j],i,[],j,[1,2]);
/* [] */
</syntaxhighlight>
Extra credit
<syntaxhighlight lang="maxima">
my_cartesian(lst1,lst2):=create_list([i,j],i,lst1,j,lst2);
n_ary_cartesian(singleargument):=block(lreduce(my_cartesian,singleargument),map(flatten,%%));
 
[[1776,1789],[7,12],[4,14,23],[0,1]]$
n_ary_cartesian(%);
/* [[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]] */
 
[[1,2,3],[30],[500,100]]$
n_ary_cartesian(%);
/* [[1,30,500],[1,30,100],[2,30,500],[2,30,100],[3,30,500],[3,30,100]] */
 
[[1,2,3],[],[500,100]]$
n_ary_cartesian(%);
/* [] */
</syntaxhighlight>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE CartesianProduct;
Line 5,101 ⟶ 5,176:
{{trans|Kotlin}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
 
var prod2 = Fn.new { |l1, l2|
Line 5,196 ⟶ 5,271:
]
</pre>
 
=={{header|zkl}}==
Cartesian product is build into iterators or can be done with nested
990

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.